Change Led by pushing button...????

Hi every body!

I desperately need help!

I have an horizontal line of 4 Leds and 2 buttons. What I'm trying to do is to change led everytime i push one of the button...the trickiest part is that i want to change led from left to right when pushing the right button, and from right to left when pushing the left button...

I've been trying the whole day but i simply don't know how! Right now the only thing i managed to do is to turn a led on or off!!!

Thank you for your help!

Below is the code I used. datamx are the leds btn are the buttons

//interi datamatrix int datamx0 = 10; int datamx1 = 8; int datamx2 = 3; int datamx3 = 2;

//interi bottoni di scorrimento int btnSX = 12; int btnDX = 11;

int val = 0;

void setup() { pinMode (datamx0 , OUTPUT); pinMode (datamx1 , OUTPUT); pinMode (datamx2 , OUTPUT); pinMode (datamx3 , OUTPUT);

pinMode (btnSX, INPUT); pinMode (btnDX, INPUT); }

void loop() { //btnSX { val = digitalRead(btnSX); // leggo lo stato del bottone if (val == HIGH) { // se il bottone da come valore HIGH digitalWrite(datamx0,LOW); // il led è spento } else { digitalWrite(datamx0, HIGH); // altrimenti è acceso } } //btnDX { val = digitalRead(btnDX); if (val == HIGH) { digitalWrite(datamx3,LOW); } else { digitalWrite(datamx3, HIGH); }

} }

I'm not sure I understand. Does the above code work? Are you asking how to modify it to accomplish something similar but different, or are you asking how to make it work as is? On the surface it looks fine to me, but it's not clear to me exactly how you want to change it.

  • Ben

sanspot, I think you want to do something like this:

//interi datamatrix 
#define LED_COUNT 4
int datamx[LED_COUNT] = {10,8,3,2} ;  // datamx is now an array with arduino pin numbers

//interi bottoni di scorrimento 
int btnSX = 12; 
int btnDX = 11; 

int val = 0; 
int ledIndex = 0;

void setup() 
{ 
  for(int i=0; i < LED_COUNT; i++){
    pinMode (datamx[i], OUTPUT);  // this sets each of the led pins to outputs
  }
  pinMode (btnSX, INPUT); 
  pinMode (btnDX, INPUT); 
} 

void loop() { 
  //btnSX 
  val = digitalRead(btnSX); // leggo lo stato del bottone 
  if (val == HIGH) { // se il bottone da come valore HIGH 
    if(ledIndex < LED_COUNT-1){
      digitalWrite(datamx[ledIndex],LOW);   // turn the old led off
      ledIndex++; 
      digitalWrite(datamx[ledIndex],HIGH);   // turn the new led on
    }
  }

  //btnDX 
  val = digitalRead(btnDX); 
  if (val == HIGH) { 
    if(ledIndex > 0){
      digitalWrite(datamx[ledIndex],LOW);   // turn the old led off
      ledIndex--;
      digitalWrite(datamx[ledIndex],HIGH);   // turn the new led on
    }
  } 
}

Note that this code does not handle switch bounce. There are a number of arduino tutorials covering this and its left as a fun little exercise for you to add if necessary.

@ bens

Sorry i was not clear...That code works, no problem at all...but with that code one button turns on only the led i specify in the if/else code...for example btnSX turns on only datamx0...what im trying to do is to "scroll" Led from right to left with the left button and viceversa... I have a of 4 ledsand i want two scroll them with to button, one tha thato goes from left to right and the other that goes in the other side.

@ mem i will try immediately the code and let you know if it Works!

Thanks a lot everybody

haven't seen that mem have just given the right code (and real code) I don't delete this post just for the way to make the leds rotate, ie go back to the first one when the last (right) one is HIGH and right is pressed.

Why don't you just connect the led to four consecutives pins, set an int to the first one and do something like :

Half-pseudo code, sorry I'm too tired ^^

#define FIRST_LED  2;               // Leds are connected to 2, 3, 4, 5

(in setup)

int currentLed = FIRST_LED;   
// pinMode OUTPUT for each,etc. A for loop will do the trick.

(in loop)

// Add some code to wait for a button press.

if (right button pressed)  {
   digitalWrite(currentLed, LOW);
   currentLed = ((currentLed == firstLed + 3) ? firstLed : currentLed + 1);
   digitalWrite(currentLed, HIGH);
   }   
   
if (left button pressed)  {
   digitalWrite(currentLed, LOW);
   currentLed = ((currentLed == firstLed) ? firstLed +3 : currentLed - 1);
   digitalWrite(currentLed, HIGH);
   }

If you don't want to use consecutive pins, you can do almost the same thing with an array.