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.
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.