Hi,
First post need help understanding this sketch.
I want to build a guitar/amp/effect switch system. I make guitar pedals etc so Im ok with electronics a at basic level. But this code stuff is new.
I found this simple code which was meant for a RGB LED, I've made it work with four individul LEDs and it does exactly what it's asked to do. That is every time the button is pushed it illuminates the next LED, which then goes out when you release the button. I want the LED to stay on, when the button is pushed this LED goes out and the next one in the sequence is illuminated and so on through the sequence.
I must emphasise this is not my code credit to Barnacle Budd Nibbles and bits.
int greene=4; // Clear green lead goes to pin 4
int pink=5; // pink lead goes to pin 5
int green=6; // Green, goes to pin 6
int red=7; // red goes to 7
int color=red; // Start sequence with last color
// We will bounce it back to green
// the first time the button is pressed.
int button=11; // Hook this pin to one side of the button
// and a pull down resistor that goes to
// ground. Hook the other side of the
// button to 5v.
int val=0; // Stores the state of the button:
// 1 = ON 0 = OFF
boolean didThis=false; // We use this so that we only
// execute the working loop once
// per click.
void setup() {
pinMode (greene,OUTPUT); // We have stepped these values:
pinMode (pink,OUTPUT); // 5, 6, 7 so we can do some simple
pinMode (green,OUTPUT); // math with the value of 'color'
pinMode (red,OUTPUT);
pinMode (button, INPUT); // Connected with a pull down
// resistor to ground. When button
// is pressed the pin goes HIGH.
}
void loop() {
val=digitalRead(button); // What is the state of the button?
if (val==1) { // Button is down
lightMyFire(); // Go turn the LED on
}
else { // Button is up
allOff(); // Go turn the LED off
}
delay(20); // weed out any key bounce
}
void lightMyFire() {
if (didThis) return; // If we have already turned the LED
// on, we don't want to do this again.
// Go back to loop() and wait for
// something new to happen.
color++; // Bump the value up by one
// 5 - 6 - 7 ...
if (color>red) color=greene; // if it is 8, knock it down to 5
digitalWrite(color,1); // Send voltage to this pin on the LED
didThis=true; // Switch this value so we only do
// this stuff once per click.
}
void allOff() {
digitalWrite(greene,0); // We are going to turn ALL of them
digitalWrite(pink,0); // off, even though only one is on.
digitalWrite(green,0); // This makes it easy to experiment
digitalWrite(red,0); // with multi-colored options.
didThis=false; // Switch this value so we can do
// it all again with the next click.
}
Many thanks
Matt