LED Bar graph and Button

The tone worked...
How about button 1 for the reset?
Thanks
Michael

replace this:

else if (state == DEAD_STATE)
  {
    // nothing to do here... I'm dead so hit reset to start over
  }

with this:

else if (state == DEAD_STATE)
  {
     if (digitalRead(pushButton) == LOW && digitalRead(pushButton2) == LOW) state = PHASER;
  }

and press BOTH buttons to exit dead state

can we get a new video?

I'm getting an error when I verify the code

Thanks
Michael

looks like you are missing a close curly brace at the end of loop()

you probably cut it out...

post the whole program and I'll look

Here is the code...

/*
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, 4000, 50);
    }
  }
  else if (val==HIGH && lastVal == LOW && state == PHASER)
  {
    if (millis() - timeStamp >= 2000UL)
    {
      state = DIAGNOSTIC;
      Serial.println(state);
    }
  }
  else if (val==HIGH && lastVal == LOW && state == DIAGNOSTIC)
  {
    if (millis() - timeStamp >= 2000UL)
    {
      state = PHASER;
      Serial.println(state);
    }
  }
  lastVal = val;
  if (val2 == LOW && lastVal2 == HIGH && state == PHASER)
  {
    timeStamp2 = millis();
    index >>= 1;
    tone(spkr, 4000, 50);//<<<<<<<<<<<<<<<<<<<<<<<<<< TONE ADDED
    Serial.print("index = "); 
    Serial.println(index, BIN);
  }
  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");
    }
  }
  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)
  {
    // nothing to do here... I'm dead so hit reset to start over
  }
}
//
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);
        for (int i = 0; i < 10; i++)
        {
          digitalWrite(ledPin[i], LOW);
        }
      }
    }
    Serial.println(scanInterval);
  }
}

Thank You
Michael

no, that compiles and does not include your edits

Here is a video clip...

Is there a way when you reset it the LEDs will all be off instead of all on???
Also is it possible to get a tone when I push the Diagnostic and Overload buttons???
Thanks
Michael

since you put everything together, with the tones working, post that code and I can show you where to insert the sounds

this should get you back to no lights on in PHASER state after you reset during DEAD_STATE:

replace this:

else if (state == DEAD_STATE)
  {
     if (digitalRead(pushButton) == LOW && digitalRead(pushButton2) == LOW) state = PHASER;
  }

with this:

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;
     }
  }

careful cutting and pasting!! :blush:

Here is the code...

/*
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, 4000, 50);
    }
  }
  else if (val==HIGH && lastVal == LOW && state == PHASER)
  {
    if (millis() - timeStamp >= 2000UL)
    {
      state = DIAGNOSTIC;
      Serial.println(state);
    }
  }
  else if (val==HIGH && lastVal == LOW && state == DIAGNOSTIC)
  {
    if (millis() - timeStamp >= 2000UL)
    {
      state = PHASER;
      Serial.println(state);
    }
  }
  lastVal = val;
  if (val2 == LOW && lastVal2 == HIGH && state == PHASER)
  {
    timeStamp2 = millis();
    index >>= 1;
    tone(spkr, 4000, 50);//<<<<<<<<<<<<<<<<<<<<<<<<<< TONE ADDED
    Serial.print("index = "); 
    Serial.println(index, BIN);
  }
  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");
    }
  }
  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);
        for (int i = 0; i < 10; i++)
        {
          digitalWrite(ledPin[i], LOW);
        }
      }
    }
    Serial.println(scanInterval);
  }
}

Thanks
Michael

you can try to put them in to these locations....

/*
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, 4000, 50);
    }
  }
  else if (val==HIGH && lastVal == LOW && state == PHASER)
  {
    if (millis() - timeStamp >= 2000UL)
    {
      state = DIAGNOSTIC;
      Serial.println(state);
      //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Tone here... toggle phaser to diagnostic
    }
  }
  else if (val==HIGH && lastVal == LOW && state == DIAGNOSTIC)
  {
    if (millis() - timeStamp >= 2000UL)
    {
      state = PHASER;
      Serial.println(state);
      //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Tone here... toggle diagnostic to phaser
    }
  }
  lastVal = val;
  if (val2 == LOW && lastVal2 == HIGH && state == PHASER)
  {
    timeStamp2 = millis();
    index >>= 1;
    tone(spkr, 4000, 50);//<<<<<<<<<<<<<<<<<<<<<<<<<< TONE ADDED
    Serial.print("index = "); 
    Serial.println(index, BIN);
  }
  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");
      //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Tones here for overload
    }
  }
  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 here for overload to dead state
        for (int i = 0; i < 10; i++)
        {
          digitalWrite(ledPin[i], LOW);
        }
      }
    }
    Serial.println(scanInterval);
  }
}

This is a clip of sound module that I want to use in the Phaser...
Sound Module
The sounds are not for this project it is just to show you the sound module...
It has it's own power source 3 button cells 4.5v
Is there a way when I hit the overload button that it will activate the and play a sound from the module???
Thanks
Michael

jameskirk:
This is a clip of sound module that I want to use in the Phaser...
Sound Module
The sounds are not for this project it is just to show you the sound module...
It has it's own power source 3 button cells 4.5v
Is there a way when I hit the overload button that it will activate the and play a sound from the module???
Thanks
Michael

do you have the specs on that module? it looks quite simple.

Here is a link to the site...
5 Button Sound Module
Thanks

so, you need 5 outputs... Do you have 5 pins to use (not including 0 or 1)?

hint... you can move some of those ledPins over to A1-A5.

const int ledPin[10] = {A5,A4,A3,A2,A1,6,5,4,3,2};

and put those switches to digital pins available...

common grounds... remember.

I would like to use only 4 of the buttons on the sound module???
Also is it possible to when I push the buttons for the LEDs like 5 then push the fire button
for a sound then 7 hit the fire button for a different sound and then 10 for another sound???
I have A1-A7 pins available...
Thanks

sure,

you have to determine if the buttons are closing the circuit or opening the circuit. So, simply use your multimeter to check for a voltage across the two posts of the switches. Hopefully you have the same voltage across each of them.

Let us know.

The voltage is the same the buttons are N/O...
It took me a while to figure it out i'm not to good with a multi-meter...
Thanks

so what was the voltage?

They are 4.27v...
Thanks

I'm thinking that you can power the sound module from the arduino (instead of its batteries) and just attach the switch pins to available outputs pins. One power supply, easy.

That's assuming your module is friendly to 5V.