Go Down

Topic: LED Bar graph and Button (Read 19050 times) previous topic - next topic

jameskirk

Here is a video of how it turned out...
http://vid1078.photobucket.com/albums/w495/jamestkirk1864/Misc/20141010_105847_zps3gnhxlub.mp4
Now all I need is a tone with each touch of the buttons...
Thanks
Michael

BulldogLowell

works great! 8)

did you use my latest post?

jameskirk

Yes I did works GREAT...
Now I just need to add a tone to the buttons...
Thanks
Michael

jameskirk


jameskirk

#19
Oct 13, 2014, 01:17 am Last Edit: Oct 17, 2014, 04:20 am by jameskirk Reason: 1
I would like to add 2 other sequences to the sketch if it is possible...
It will be like a diagnostic mode and a overload mode...
I found this sketch that I would like to put in when
all the LEDs are lit up so if I push the button 11 times it will put it in diagnostic mode...
If I would push the buttons like on the video it would put it in overload mode...
Here is the video...
http://vid1078.photobucket.com/albums/w495/jamestkirk1864/Misc/DustbusterPhaser1_zps5c3f3cb9.mp4

Here is the code I found...
Code: [Select]

/*
* inAndOut() - This will turn on the two middle LEDs then the next two out
* making an in and out look
*/
void inAndOut(){
 int delayTime = 100; //the time (in milliseconds) to pause between LEDs
                      //make smaller for quicker switching and larger for slower
 
 //runs the LEDs out from the middle
 for(int i = 0; i <= 3; i++){
   int offLED = i - 1;  //Calculate which LED was turned on last time through
   if(i == 0) {         //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will
     offLED = 3;        //turn on LED 2 and off LED 1)
   }                    //however if i = 0 we don't want to turn of led -1 (doesn't exist)
                        //instead we turn off LED 7, (looping around)
   int onLED1 = 3 - i;       //this is the first LED to go on ie. LED #3 when i = 0 and LED
                            //#0 when i = 3
   int onLED2 = 4 + i;       //this is the first LED to go on ie. LED #4 when i = 0 and LED
                            //#7 when i = 3
   int offLED1 = 3 - offLED; //turns off the LED we turned on last time
   int offLED2 = 4 + offLED; //turns off the LED we turned on last time
   
   digitalWrite(ledPins[onLED1], HIGH);
   digitalWrite(ledPins[onLED2], HIGH);    
   digitalWrite(ledPins[offLED1], LOW);    
   digitalWrite(ledPins[offLED2], LOW);        
   button_delay(delayTime, buttonPin);
 }

 //runs the LEDs into the middle
 for(int i = 3; i >= 0; i--){
   int offLED = i + 1;  //Calculate which LED was turned on last time through
   if(i == 3) {         //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will
     offLED = 0;        //turn on LED 2 and off LED 1)
   }                    //however if i = 0 we don't want to turn of led -1 (doesn't exist)
                        //instead we turn off LED 7, (looping around)
   int onLED1 = 3 - i;       //this is the first LED to go on ie. LED #3 when i = 0 and LED
                            //#0 when i = 3
   int onLED2 = 4 + i;       //this is the first LED to go on ie. LED #4 when i = 0 and LED
                            //#7 when i = 3
   int offLED1 = 3 - offLED; //turns off the LED we turned on last time
   int offLED2 = 4 + offLED; //turns off the LED we turned on last time
   
   digitalWrite(ledPins[onLED1], HIGH);
   digitalWrite(ledPins[onLED2], HIGH);    
   digitalWrite(ledPins[offLED1], LOW);    
   digitalWrite(ledPins[offLED2], LOW);        
   button_delay(delayTime,buttonPin);
 }
}]

Any help would great...
Thanks
Michael

BulldogLowell

#20
Oct 13, 2014, 02:08 am Last Edit: Oct 13, 2014, 02:20 am by BulldogLowell Reason: 1
you can try this which I compiled, but could not test.

I added another buttonPin to A0, but you have to try that to make sure it works for you.

something like this, which I added to your existing sketch...

Code: [Select]


#define PHASER 0
#define DIAGNOSTIC 1

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

void setup()
{
  Serial.begin(115200);
  for (int i = 0; i < 11; i++)
  {
    pinMode(ledPin[i],OUTPUT);
  }
  pinMode(pushButton,INPUT);
  pinMode(pushButton2,INPUT);
  pinMode(pushButton3,INPUT);
}
void loop()
{
  int val=digitalRead(pushButton);
  delay(50); // Software debouncing
  if(val==HIGH && lastVal == LOW && state == PHASER)
  {
    index <<= 1;
    index |= 1;
    if (index > 0b1111111111) index = 0b1111111111;
    Serial.print("index = "); Serial.println(index, BIN);
    for (int i = 0; i < 10; i++)
    {
      digitalWrite(ledPin[i], bitRead(index, i));
    }
  }
  lastVal = val;
  int val2 = digitalRead(pushButton2);
  delay(50); // Software debouncing
  if(val2 == HIGH && lastVal2 == LOW && state == PHASER)
  {
    index >>= 1;
    Serial.print("index = "); Serial.println(index, BIN);
    for (int i = 0; i < 10; i++)
    {
      digitalWrite(ledPin[i], bitRead(index, i));
    }
  }
  lastVal2 = val2;
  int val3 = digitalRead(pushButton3);
  delay(50);
  if (val3 == HIGH && lastVal3 == LOW)
  {
    if (state == DIAGNOSTIC) state = PHASER;
    else state = DIAGNOSTIC;
  }
  if (state == DIAGNOSTIC)
  {
    diagnogsticScan();
    // other diagnostic stuff here...
  }
}

void diagnogsticScan()
{
  static unsigned long scanStart;
  static byte scanIndex = 0;
  static byte increment = 1;
  if (millis() - scanStart >= 75UL)
  {
    Serial.println(index);
    for (int i = 0; i < 10; i++)
    {
      digitalWrite(ledPin[i], bitRead(scanArray[scanIndex], i));
    }
    index += increment;
    if (index == 0 || index == 4)
    {
      increment = -increment;
    }
    scanStart = millis();
  }
}


I goofed a bit... corrected above

jameskirk

I have 2 problems with the code...
1. I have the piezo speaker connected to A0
2. Is it possible to use the up button to switch the different sequences???
Thanks
Michael

BulldogLowell


I have 2 problems with the code...
1. I have the piezo speaker connected to A0
2. Is it possible to use the up button to switch the different sequences???
Thanks
Michael


try A1 yet?

yes, it is possible thought

jameskirk

I uploaded the sketch and added a 3rd switch...
This was the result...


It only turns on the 2 end LEDs...
Thanks
Michael

BulldogLowell

I messed up, used a variable name already in use...

try this and let me know

Code: [Select]

#define PHASER 0
#define DIAGNOSTIC 1

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

void setup()
{
  Serial.begin(115200);
  for (int i = 0; i < 11; i++)
  {
    pinMode(ledPin[i],OUTPUT);
  }
  pinMode(pushButton,INPUT);
  pinMode(pushButton2,INPUT);
  pinMode(pushButton3,INPUT);
}
void loop()
{
  int val=digitalRead(pushButton);
  delay(50); // Software debouncing
  if(val==HIGH && lastVal == LOW && state == PHASER)
  {
    timeStamp = millis();
    index <<= 1;
    index |= 1;
    if (index > 0b1111111111) index = 0b1111111111;
    Serial.print("index = "); Serial.println(index, BIN);
    for (int i = 0; i < 10; i++)
    {
      digitalWrite(ledPin[i], bitRead(index, i));
    }
  }
  /*
  else if (val==LOW && lastVal == HIGH)
  {
    if (millis() - timeStamp >= 2000UL)
    {
      state = DIAGNOSTIC;
    }
  }
  */
  lastVal = val;
  int val2 = digitalRead(pushButton2);
  delay(50); // Software debouncing
  if(val2 == HIGH && lastVal2 == LOW && state == PHASER)
  {
    index >>= 1;
    Serial.print("index = "); Serial.println(index, BIN);
    for (int i = 0; i < 10; i++)
    {
      digitalWrite(ledPin[i], bitRead(index, i));
    }
  }
  lastVal2 = val2;
  int val3 = digitalRead(pushButton3);
  delay(50);
  if (val3 == HIGH && lastVal3 == LOW)
  {
    if (state == DIAGNOSTIC) state = PHASER;
    else state = DIAGNOSTIC;
  }
  if (state == DIAGNOSTIC)
  {
    diagnogsticScan();
    // other diagnostic stuff here...
  }
}

void diagnogsticScan()
{
  static unsigned long scanStart;
  static byte scanIndex = 0;
  static byte increment = 1;
  if (millis() - scanStart >= 75UL)
  {
    Serial.println(scanIndex);
    for (int i = 0; i < 10; i++)
    {
      digitalWrite(ledPin[i], bitRead(scanArray[scanIndex], i));
      Serial.println(i);
    }
    scanIndex += increment;
    if (scanIndex == 0 || scanIndex == 4)
    {
      increment = -increment;
    }
    scanStart = millis();
  }
}

jameskirk

Here is the new results...
http://vid1078.photobucket.com/albums/w495/jamestkirk1864/Misc/20141013_000046_zpsyvjsk9kx.mp4
So is it possible to move the diagnostic button to the up button???
I only have 2 buttons for the Bargraph and 1 button to fire the phaser...
Thanks
Michael

BulldogLowell

OK,

I added a little piece that cleans up the LEDs after you put it back to PHASER mode.

Then, I added a long button press of 2 seconds (you can of course change that) on pushButton to put the unit into DIAGNOSTIC mode.  Another  press of pushButton should bring it back to PHASER mode.

check it out and let me know if it is working.   I kept A1 in there while you mess with getting the long presses right.

I also sped up the diagnostic scan a bit... you can tweak the 60UL value (e.g. 50UL) until it scans to your satisfaction.

looks good!

compiled but not tested:

Code: [Select]


#define PHASER 0
#define DIAGNOSTIC 1

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

void setup()
{
 Serial.begin(115200);
 for (int i = 0; i < 11; i++)
 {
   pinMode(ledPin[i],OUTPUT);
 }
 pinMode(pushButton,INPUT);
 pinMode(pushButton2,INPUT);
 pinMode(pushButton3,INPUT);
}
void loop()
{
 //
 int val=digitalRead(pushButton);
 delay(50); // Software debouncing
 if(val==HIGH && lastVal == LOW && state == PHASER)
 {
   timeStamp = millis();
   index <<= 1;
   index |= 1;
   if (index > 0b1111111111) index = 0b1111111111;
   Serial.print("index = "); Serial.println(index, BIN);
   for (int i = 0; i < 10; i++)
   {
     digitalWrite(ledPin[i], bitRead(index, i));
   }
 }
 else if (val==LOW && lastVal == HIGH)
 {
   if (millis() - timeStamp >= 2000UL)
   {
     for (int i = 0; i < 10; i++)
   {
     digitalWrite (ledPin[i], LOW);
   }
   if (state == DIAGNOSTIC) state = PHASER;
   else state = DIAGNOSTIC;
   }
 }
 /*
 else if (val==HIGH && lastVal == LOW && state == DIAGNOSTIC)
 {
   for (int i = 0; i < 10; i++)
   {
     digitalWrite (ledPin[i], LOW);
   }
   state = PHASER;
 }*/
 lastVal = val;
 //
 int val2 = digitalRead(pushButton2);
 delay(50); // Software debouncing
 if(val2 == HIGH && lastVal2 == LOW && state == PHASER)
 {
   index >>= 1;
   Serial.print("index = "); Serial.println(index, BIN);
   for (int i = 0; i < 10; i++)
   {
     digitalWrite(ledPin[i], bitRead(index, i));
   }
 }
 lastVal2 = val2;
 // Keep this here for now................
 int val3 = digitalRead(pushButton3);
 delay(50);
 if (val3 == HIGH && lastVal3 == LOW)
 {
   for (int i = 0; i < 10; i++)
   {
     digitalWrite (ledPin[i], LOW);
   }
   if (state == DIAGNOSTIC) state = PHASER;
   else state = DIAGNOSTIC;
 }
 if (state == DIAGNOSTIC)
 {
   diagnogsticScan();
   // other diagnostic stuff here...
 }
}

void diagnogsticScan()
{
 static unsigned long scanStart;
 static byte scanIndex = 0;
 static byte increment = 1;
 if (millis() - scanStart >= 60UL)
 {
   Serial.println(scanIndex);
   for (int i = 0; i < 10; i++)
   {
     digitalWrite(ledPin[i], bitRead(scanArray[scanIndex], i));
     Serial.println(i);
   }
   scanIndex += increment;
   if (scanIndex == 0 || scanIndex == 4)
   {
     increment = -increment;
   }
   scanStart = millis();
 }
}


it is great that you show the photos.

jameskirk

I'm getting a random light blink without touching the buttons...
The sketch is the clean one that you posted I took out the tone it
is just the original sketch...
Here is a video...http://vid1078.photobucket.com/albums/w495/jamestkirk1864/Misc/20141013_192124_zps446ecsh2.mp4
Here is a video of how the sketch looks so far...
http://vid1078.photobucket.com/albums/w495/jamestkirk1864/Misc/20141013_200522_zpsbq4f0jxe.mp4
Thanks
Michael

BulldogLowell

in the 2nd video, it appears to work like I thought you wanted it to...is that not the case?


I'm getting a random light blink without touching the buttons...


the first video, in that code you have this line in your initialization? 

Code: [Select]
byte state = PHASER;// initialises in phaser state

is this occurring at startup or randomly?

jameskirk

#29
Oct 14, 2014, 05:04 pm Last Edit: Oct 14, 2014, 05:15 pm by jameskirk Reason: 1
Yes the second video is the way it is supposed to be and is...
The first video shows that when I plug in the arduino and the lights stop blinking I don't touch anything
and after approx. 2 1/2 min. the LEDs come on from the center and out blink not touching anything...
Here is a 2 sec. clip...
http://vid1078.photobucket.com/albums/w495/jamestkirk1864/Misc/20141014_105528_zpsjesi5rvs.mp4
It did it's chase back and forth about 50 times then stopped and then a little after that did it again keeps repeating...
Anything you would like me to try???
Thanks
Michael

Go Up