Go Down

Topic: 8 LED / 8 Switches - 1 on at once (Read 297 times) previous topic - next topic

lifescape

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

PaulS

Quote
Any pointers would be appreciated.

Since you asked:
Code: [Select]
char *help = "On the way";

An array of switch pins and an array of LED pins would be a good start:
Code: [Select]
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: [Select]
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);
  }
}

lifescape

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

lifescape

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


Go Up