Welcome, %1$s. Please login or register. - Did you miss your activation email?

Login with username, password and session length
 

Author Topic: Arduino: Playmates TNG Cobra phaser mod: Electronics  (Read 591 times)

Offline Jimmypop57

  • Admiral
  • *****
  • Posts: 935
  • Scifi scale model enthusiast
    • Youtube Channel
Arduino: Playmates TNG Cobra phaser mod: Electronics
« on: October 17, 2016, 07:36:28 am »
Hey folks! Taking a short interlude on the dreadnought build, and putting something together for a Halloween costume (if I can get it done in time LOL). I picked up a broken playmates phaser for a few bucks, then went hunting for ways to make it "work" >8^)

I came across an Arduino sketch to do just that! I needed to tweak a few things, but BulldogLowell on the arduino forums was doing stuff I don't know how to do. Basically I just made the sound work via trial and error.

Hopefully I'll be able to get the electronics in, and the phaser a bit more screen accurate for a reveal. Take a look, see what you think. Code is below.

https://www.youtube.com/watch?v=fEZ3hKYNJEo

Code: [Select]
/*
Compliments of BulldogLowell - Ardunio Forums. Mad props for setting this up DOOD!
 
Press pushButton powerUP whilst in PHASER mode
Press pushButton2 powerDown whilst in PHASER mode
Long Press pushButton Toggle DIAGNOSTIC/PHASER mode
Long Press pushButton2 whilst DIAGNOSTIC switches to OVERLOAD mode
OVERLOAD mode you will see a short stall of the LEDs and it will begin a 10s self destruct sequence.
*/
#define PHASER 0
#define DIAGNOSTIC 1
#define OVERLOAD 2
#define DEAD_STATE 3

const int ledPin[10] = {
  13,11,10,9,8,6,5,4,3,2};
const int scanArray[5] = {
  0b1000000001, 0b0100000010, 0b0010000100, 0b0001001000, 0b0000110000};
const byte pushButton  = 7;
const byte pushButton2 = 12;
int spkr = A0;
byte lastVal;
byte lastVal2;
int index = 0;
byte state = PHASER;// initialises in phaser state
unsigned long timeStamp;
unsigned long timeStamp2;
unsigned long timeStamp3;

void setup()
{
  Serial.begin(115200);
  for (int i = 0; i < 11; i++)
  {
    pinMode(ledPin[i],OUTPUT);
  }
  pinMode(pushButton,INPUT_PULLUP);
  pinMode(pushButton2,INPUT_PULLUP);
}
void loop()
{
  byte val=digitalRead(pushButton);
  byte val2 = digitalRead(pushButton2);
  if (val != lastVal || val2 != lastVal2) delay(50);
  if(val==LOW && lastVal == HIGH && state == PHASER)
  {
    timeStamp = millis();
    index <<= 1;
    index |= 1;
    if (index > 0b1111111111) index = 0b1111111111;
    for (int i = 0; i < 10; i++)
    {
      digitalWrite(ledPin[i], bitRead(index, i));
        tone(spkr, 8000, 50);
    }
  }
  else if (val==HIGH && lastVal == LOW && state == PHASER)
  {
    if (millis() - timeStamp >= 2000UL)
    {
      state = DIAGNOSTIC;
      Serial.println(state);
       tone(spkr, 2000, 50);
    }
  }
  else if (val==HIGH && lastVal == LOW && state == DIAGNOSTIC)
  {
    if (millis() - timeStamp >= 2000UL)
    {
      state = PHASER;
      Serial.println(state);
       tone(spkr, 8000, 50);
    }
  }
  lastVal = val;
  if (val2 == LOW && lastVal2 == HIGH && state == PHASER)
  {
    timeStamp2 = millis();
    index >>= 1;
    Serial.print("index = ");
    Serial.println(index, BIN);
    tone(spkr, 8000, 50);
  }
  else if (val2 == LOW && lastVal2 == HIGH && state == DIAGNOSTIC)
  {
    timeStamp2 = millis();
  }
  else if (val2 == LOW && lastVal2 == LOW && state == DIAGNOSTIC)
  {
    if (millis() - timeStamp2 >= 2000UL)
    {
      delay(250);
      state = OVERLOAD;
      timeStamp3 = millis();
      Serial.println("overload");
       tone(spkr, 4000, 50);
    }
  }
  lastVal2 = val2;
  //
  if (state == PHASER)
  {
    for (int i = 0; i < 10; i++)
    {
      digitalWrite(ledPin[i], bitRead(index, i));
    }
  }
  else if (state == DIAGNOSTIC)
  {
    diagnogsticScan(false);
  }
  else if (state == OVERLOAD)
  {
    if (millis() - timeStamp3 > 10000UL)
    {
      diagnogsticScan(true);
    }
    else
    {
      diagnogsticScan(false);
    }
  }
else if (state == DEAD_STATE)
  {
     if (digitalRead(pushButton) == LOW && digitalRead(pushButton2) == LOW)
     {
       index = 0;//<<<<<<<<<<<<<<<<<<<<<<< added this, that is why i needed to add the curly braces, so both lines will execute after the two button press
       state = PHASER;
     }
  }
}
//
void diagnogsticScan(boolean faster)
{
  static unsigned long scanInterval = 80UL;
  static unsigned long scanStart;
  static byte scanIndex = 0;
  static byte increment = 1;
  if (millis() - scanStart >= scanInterval)
  {
    scanStart += scanInterval;
    for (int i = 0; i < 10; i++)
    {
      digitalWrite(ledPin[i], bitRead(scanArray[scanIndex], i));
    }
    scanIndex += increment;
    if (scanIndex == 0 || scanIndex == 4)
    {
      increment = -increment;
    }
    if (faster)
    {
      scanInterval--;
      if (scanInterval <= 0)
      {
        scanInterval = 80UL;
        state = DEAD_STATE;
        for (int i = 0; i < 10; i++)
        {
          digitalWrite(ledPin[i], HIGH);
        }
        delay(250);
         tone(spkr, 550, 50);
        for (int i = 0; i < 10; i++)
        {
          digitalWrite(ledPin[i], LOW);
        }
      }
    }
    Serial.println(scanInterval);
  }
}
« Last Edit: October 17, 2016, 07:39:00 am by Jimmypop57 »
I play, I putty, I spray, and pray! Putty, sand, repeat...
All putty and no spray makes TOS models green and gray... All putty and no spray makes TOS models green and gray... All putty and no spray makes TOS models green and gray... All putty and no spray makes TOS models green and gray...

 




Enterprise © Bloc