Go Down

Topic: 6 buttons to toggle 6 LEDs individually (Read 151 times) previous topic - next topic

grimat

Hi.

I'm all new to c++ and arduino, and working on a project to build a MIDI-controller for my guitar effects, the MIDI code and controller works just fine, however i'm struggling with making LEDs toggle on/off with the switches.

The wirings:

- Arduino Micro
- SPST buttons to digital input 2-9  and ground. (8 switches in total) - Internal resistors pulled up.
- 6 LEDs with 330ohm resistors to digital inputs 10-12+14-16(A0-A2) and ground. (6 LEDs in total)

Ive tested alle the LEDS and all the MIDI stuff and it works, but the LED-toggle I don't know how to solve.

I want the LEDS to be controlled by swithes 1-6, i.e push switch 1 to turn led 1 on, and push once again to turn off.

So far (by stealing code here and there) I've only managed to toggle all LED's simotaneously, and not individually with the different switches.


Here's the code i've tried so far, its originally ment for just 1 switch and LED, so what i did was just to duplicate it, but this did not work.

Code: [Select]

int switch1 = 2;
int switch2 = 3;
int ledpin1 = 10;
int ledpin2 = 11;
boolean flag = true;

void setup()
{
  pinMode (ledpin1,OUTPUT);
  pinMode (ledpin2,OUTPUT);
  pinMode (switch1,INPUT_PULLUP);
  pinMode (switch2,INPUT_PULLUP);

}

void loop()
{
  if (digitalRead(switch1)==LOW){ // check the state of switch1 every time we run through the main loop 
  delay(5); // I don't REALLY know why this delay helps, but it does.
      flipflop(); // hops out of main loop and runs the flipflop function
  }// end of check for button press.
 
  if (digitalRead(switch2)==LOW){ // check the state of switch1 every time we run through the main loop 
  delay(5); // I don't REALLY know why this delay helps, but it does.
      flipflop(); // hops out of main loop and runs the flipflop function
  }// end of check for button press.

  // other sketch code here

} // end of main loop.

void flipflop(){  //funtion flipflop
  flag = !flag;  // since we are here, the switch was pressed So FLIP the boolian "flag" state (we don't even care if switch was released yet)
 

  if (flag == HIGH){  // Use the value of the flag var to change the state of the pin
    digitalWrite(ledpin1,HIGH ); // if the flag var is HIGH turn the pin on
  }
  if (flag == LOW) {
    digitalWrite(ledpin1,LOW); // if the flag var is LOW turn the pin off
  }
  while(digitalRead(switch1)==LOW); // for "slow" button release, keeps us in the function until button is UN-pressed
  // If you take out this "while" the function becomes a flipflop oscillator if the button is held down.
  delay(50); // OPTIONAL - play with this value.  It is probably short enough to not cause problems. deals with very quick switch press.

 
  if (flag == HIGH){  // Use the value of the flag var to change the state of the pin
    digitalWrite(ledpin2,HIGH ); // if the flag var is HIGH turn the pin on
  }
  if (flag == LOW) {
    digitalWrite(ledpin2,LOW); // if the flag var is LOW turn the pin off
  }
  while(digitalRead(switch2)==LOW);
 
 
 
}
 


Any suggestions how to do this? I can post my entire code (incl. midi) as well but i thought if i figured this out I could just implement it.

slipstick

#1
Nov 11, 2017, 02:56 pm Last Edit: Nov 11, 2017, 02:57 pm by slipstick
You may need to duplicate and modify a bit more. You can't use just one "flag" variable to control two different LEDs independently.

For now perhaps add a parameter to flipflop() to tell it WHICH of the switches it's reacting to and then flip a flag1 or flag2 and go from there?

But really if you're going to extend it to 6 LEDs each reacting to their own switch you should also start looking at using arrays for both. That will make the code much neater if you have something like (not real code, you'll need to look that up yourself):
Code: [Select]

for i from 1 to 6
if button[i] is pressed
switch LED[i] on
next i


Steve

grimat

An array, definitely. That would be great, I'll investigate that.

Go Up