Go Down

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

jameskirk

I've looked and looked I can't seem to find anything to tell me how I can use a LED Bar graph and a button to advance
from one light to the next...
Here is what I'm trying to accomplish...
http://vid1078.photobucket.com/albums/w495/jamestkirk1864/Misc/DustbusterPhaser1_zps5c3f3cb9.mp4
Any help would be much appreciated...
Thanks
Michael

cjdelphi

How have you wired it up yourself?

jameskirk

That is not my video...
I want to replicate what is in the video but I have to use a regular LED Bar graph
I don't have SMD LED's...
I want 2 buttons one to go up and one to go down and the other modes...
I can use a Arduino Nano but I don't know how to use it for the buttons...
Thanks
Michael

runaway_pancake

There are plenty of examples out there for how to use pushbutton switches.

You could use one to increment and one to decrement a counter and print the result using Serial Monitor for starters.
"Who is like unto the beast? who is able to make war with him?"
When all else fails, check your wiring!

jameskirk

#4
Oct 05, 2014, 04:44 am Last Edit: Oct 09, 2014, 04:15 pm by jameskirk Reason: 1
I've been looking for examples...
I have one on my breadboard now I just can't get it to go more than 2 LED's...
And what you said about serial monitors is way over my head...
Here is the example I was using...

Code: [Select]
/* YourDuinoStarter Example: Button Input
- Reads state of Pushbutton, changes state of 2 LEDS
- SEE the comments after "//" on each line below
- CONNECTIONS:
- Pushbutton Switch from +5 to Pin 3
- 10K Resistor from Pin 3 to ground
- LED and 220 ohm resistor in series from pins 10 and 11 to Gnd
- V1.00 09/17/12
Questions: terry@yourduino.com */

/*-----( Declare Constants and Pin Numbers )-----*/
#define buttonPin  3    // Pins to connect to
#define ledPin1    10
#define ledPin2    11

void setup()   /****** SETUP: RUNS ONCE ******/
{
 pinMode (ledPin1,OUTPUT) ;        // PIN 10 is an OUTPUT    
 pinMode (ledPin2, OUTPUT) ;       // PIN 11 is an OUPUT
 pinMode (buttonPin, INPUT) ;      // PIN 3  is an INPUT
}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
 if  (digitalRead(buttonPin)==HIGH)  // IF button is pushed
 {
   digitalWrite(ledPin1, HIGH) ;         // LED1 ON
   digitalWrite(ledPin2, LOW) ;          // LED2 OFF
 }  
 else
 {
   digitalWrite(ledPin1, LOW) ;          // LED1 OFF
   digitalWrite(ledPin2, HIGH) ;         // LED2 ON
 }
 delay(50);  // Switch may be "bouncing". Wait a bit

}//--(end main loop )---

I just don't know what i'm doing with this...

runaway_pancake

#5
Oct 05, 2014, 05:56 am Last Edit: Oct 05, 2014, 05:57 am by Runaway Pancake Reason: 1

And what you said about serial monitors is way over my head...


http://www.instructables.com/id/HOW-TO-use-the-ARDUINO-SERIAL-MONITOR/

> > > Adding --
http://www.newtechandme.com/arduino/arduino-tutorials-lesson-2-3-counter-using-increment-and-decrement-buttons/
"Who is like unto the beast? who is able to make war with him?"
When all else fails, check your wiring!

jameskirk

#6
Oct 09, 2014, 06:38 am Last Edit: Oct 09, 2014, 03:57 pm by jameskirk Reason: 1
Ok this is what I have so far...
I'm starting out with 1 switch to see how that works...
Here is the sketch I'm using...
Code: [Select]
const int LED1=12;
const int LED2=11;
const int LED3=10;
const int LED4=9;
const int LED5=8;
const int LED6=6;
const int LED7=5;
const int LED8=4;
const int LED9=3;
const int LED10=2;
const int SW=7;
boolean state = true; // declare variable state as boolean
int val=0;

void setup()
{
 pinMode(LED1,OUTPUT);
 pinMode(LED2,OUTPUT);
 pinMode(LED3,OUTPUT);
 pinMode(LED4,OUTPUT);
 pinMode(LED5,OUTPUT);
 pinMode(LED6,OUTPUT);
 pinMode(LED7,OUTPUT);
 pinMode(LED8,OUTPUT);
 pinMode(LED9,OUTPUT);
 pinMode(LED10,OUTPUT);
 pinMode(SW,INPUT);
}
void loop()
{
 val=digitalRead(SW);
 delay(100); // Software debouncing

if(val==HIGH)

{state=!state; // Complimenting the status of LED
 digitalWrite(LED1,state);
 digitalWrite(LED2,!state);
 digitalWrite(LED3,!state);
 digitalWrite(LED4,!state);
 digitalWrite(LED5,!state);
 digitalWrite(LED6,!state);
 digitalWrite(LED7,!state);
 digitalWrite(LED8,!state);
 digitalWrite(LED9,!state);
 digitalWrite(LED10,!state);
}
}


I can't get it to advance from one LED to the next...
There is a problem with the state lines at the bottom...
I don't know how to correct it any thoughts???
here is 3 pics of the layout...




Thanks
Michael

PaulRB

Hi again Michael,

Your sketch only has one "state" variable, which can only be HIGH or LOW. All 10 of your LEDS are tied to that one variable. 9 of them are on when state is LOW and 1 is on when state is HIGH. The 9 that are on when state is LOW will always be either on or off together.

To have individual control over 10 leds, you need either 10 "state" variables (these could be in an array), or a number variable that can change value from 0 to 10 (rather than just HIGH or LOW) to indicate how many leds should be on.

Paul

PS. CODE TAGS, C'MON MICHAEL, CODE TAGS PLEASE!


BulldogLowell

Try something like this... compiles but is untested.

note the use of the array... much simpler.

Code: [Select]
int ledPins[10] = {12,11,10,9,8,6,5,4,3,2};
const int pushButton=7;
int lastVal;
int index;
int increment = 1;

void setup()
{
  for (int i = 0; i < 10; i++)
  {
    pinMode(ledPins[i],OUTPUT);
  }
  pinMode(pushButton,INPUT);
}
void loop()
{
  int val=digitalRead(pushButton);
  delay(50); // Software debouncing
  if(val==HIGH && lastVal == LOW)
  {
    index += increment;
    if (index <=0 || index >= 9) increment = -increment;
    for (int i = 0; i < 10; i++)
    {
      if (i <= index) digitalWrite(ledPins[i], HIGH);
      else digitalWrite(ledPins[i], LOW);
    }
  }
  lastVal = val; 
}

jameskirk

PaulRB Sorry for the way I posted my code...
I didn't know any better I had to Google code tags so now I know
I corrected the code postings...

BulldogLowell Thanks for the code I will try it...
Thanks
Michael

PaulRB


I didn't know any better I had to Google code tags so now I know


Thanks for correcting your posts Michael. I did explain about code tags before when we were working on your Klingon photon collector, glad you finally understand!

PS. That Star Trek movie was on TV here a couple of weeks ago. The photon collector is not Klingon. It was made by Spock and Scotty from parts found on the Klingon ship I think...

Paul

jameskirk

Yes Paul you are correct...

jameskirk

BulldogLowell I tried the code that you posted...
I tried editing it to no avail...
Is there a way when it counts up and it comes back down it goes completely off instead of just to one???
Also I would like to use 2 buttons one to count from 0 up to 10 and the other to count from 10 down to 0...
Here is a video of what I have now
http://vid1078.photobucket.com/albums/w495/jamestkirk1864/Misc/20141009_191559_zpsqhaozkgo.mp4
Thanks
Michael

BulldogLowell

#13
Oct 10, 2014, 04:52 am Last Edit: Oct 10, 2014, 04:55 am by BulldogLowell Reason: 1
another way would be to use bitRead()

I compiled but couldn't test this, try it and let me know!

Code: [Select]
//
int ledPins[10] = {12,11,10,9,8,6,5,4,3,2};
int ledArray[11] = {0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF};
const int pushButton=7;
int lastVal;
int index = 0;
int increment = 1;

void setup()
{
 Serial.begin(115200);
 for (int i = 0; i < 11; i++)
 {
   Serial.println(ledArray[i], BIN);
 }
 for (int i = 0; i < 11; i++)
 {
   pinMode(ledPins[i],OUTPUT);
 }
 
 pinMode(pushButton,INPUT);
}
void loop()
{
 int val=digitalRead(pushButton);
 delay(50); // Software debouncing
 if(val==HIGH && lastVal == LOW)
 {
   index += increment;
   if (index <=0 || index >= 10) increment = -increment;
   Serial.print("index = "); Serial.println(index);
   for (int i = 0; i < 10; i++)
   {
     digitalWrite(ledPins[i], bitRead(ledArray[index], i));
   }
 }
 lastVal = val;  
}

BulldogLowell

#14
Oct 10, 2014, 06:03 am Last Edit: Oct 10, 2014, 06:11 am by BulldogLowell Reason: 1
two buttons for up and down... using bitShift()...

not tested...

Code: [Select]
int ledPin[10] = {12,11,10,9,8,6,5,4,3,2};
const int pushButton  = 7;
const int pushButton2 = 13;
int lastVal;
int lastVal2;
int index = 0;

void setup()
{
 Serial.begin(115200);
 for (int i = 0; i < 11; i++)
 {
   pinMode(ledPin[i],OUTPUT);
 }
 pinMode(pushButton,INPUT);
 pinMode(pushButton2,INPUT);
}
void loop()
{
 int val=digitalRead(pushButton);
 delay(50); // Software debouncing
 if(val==HIGH && lastVal == LOW)
 {
   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)
 {
   index >>= 1;
   Serial.print("index = "); Serial.println(index, BIN);
   for (int i = 0; i < 10; i++)
   {
     digitalWrite(ledPin[i], bitRead(index, i));
   }
 }
 lastVal2 = val2;
}

Go Up