Pages: [1]   Go Down
Author Topic: 8 LED / 8 Switches - 1 on at once  (Read 286 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
Arduino newbie slowly working thro the simple example projects but i have an intended use for the board and could do with a bit of direction on the flow of the coding.

I need to have 8 press switches and 8 leds. When pressing SW3 for example, LED03 lights but then if another Switch is pressed the appropriatte led lights and LED03 is extinguished. The idea eventually is to have one of 8 pre stored MIDI codes sent out dependent on the number switch pressed. With an LED indicating which switch (midi command) has been sent last)

I'm still not sure on  how to accomplish the switching OFF of the led that is already lit when a different switch is pressed.

Any pointers would be appreciated.

Thanks,

Mike
UK
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50881
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Any pointers would be appreciated.
Since you asked:
Code:
char *help = "On the way
";

An array of switch pins and an array of LED pins would be a good start:
Code:
byte ledPins[] = { 2, 3, 4, 5, 6, 7, 8, 9 };
byte swiPins[] = { 10, 11, 12, 13, 14, 15, 16, 17 }; // 14 to 17 are analog pins 0 to 3

Then, read the switch states in a loop:
Code:
for(byte i=0; i<8; i++)
{
   int state = digitalRead(swiPins[i]);
   if(state == LOW) // Assumes pullup resistor
   {
      // Turn all the LEDs off
      for(byte j=0; j<8; j++)
      {
         digitalWrite(ledPins[j], LOW);
      }

      // Turn on the one correct LED
      digitalWrite(ledPins[i], HIGH);
   }
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the quick reply.......

that brilliant, i can now see how to read the switch inputs and switch the appropriatte LED.

i.ll try a test run.

I also presume i could use another array for my midi commands which correspond to the switch number variable (i) to make selecting those easier.  I think....

many thanks for that, i.ll post what happens.

Mike
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes thats sorted that i think, had a quick test and seems to work ok.

Only prob now is i will have to allocate an input for midi from the other device so the arduiono leds correspond to the scene settings if they are changed on the other unit.

presume i can set an input on a pin and monitor midi signals coming in then assign that
to an LED switching loop much the same as in the switch sensing routine to switch the approprate LED on.??????



Mike

 
Logged

Pages: [1]   Go Up
Jump to: