# Help LED sequencing sketch

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

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.

To have a momentary contact switch turn an LED on, and have it stay on when the switch is released is trivial. When the switch reads HIGH, turn the LED on. When it reads LOW, don't turn the LED off. Just remove the else block containing the call to allOff().

Thanks for quick reply.

I removed this:
else { // Button is up
allOff(); // Go turn the LED off

Now I push the button once and the first LED illuminates and stays on, push button again no response?

What did I miss?

Matt

What did I miss?

Serial.begin() in setup() and Serial.print() statements in loop() and lightMyFire(). For you to debug the code is going to be orders of magnitude faster than for us to do it.

The code doesn't need debugging, it does what it was written to do.
I want change the code to so the circuit functions as descibed.

The code doesn't need debugging, it does what it was written to do.

directly conflicts with

I removed this:
else { // Button is up
allOff(); // Go turn the LED off

Now I push the button once and the first LED illuminates and stays on, push button again no response?

What did I miss?

You made a change. It did not have the desired affect. Why not? Well, that involves debugging.

The code is doing something. Just not what you want it to do. Sometimes, what the code is doing is obvious. LEDs blinking at the wrong speed, going off when they should be going on, etc.

Sometimes, what the code is doing is not so obvious. Especially when it appears to be doing nothing, as in your case.

So, we can suggest several things for you to try, one or more of which might actually work, over the next three days. Or, you can add some serial print statements, and see for yourself what the program is doing, and fix it in a few minutes (or hours). Your choice.

Hi Paul,

Ok I see what your saying about debugging. I'm a complete newbie to this side of things, so the obvious is not so obvious at the moment. I understand that as such this back and forth is time consuming and tedious, but trying to find just the basics rescources of programming is not easy. I want to understand what I'm doing so I can figure it out for my self and use it in the other projects /ideas I've got lined up.

So maybe my approach is wrong, I've taken someone elses code/project and tried to make it fit my project, because I dont fully understand whats going on in the code I dont know how to modify it.

You mentioned serial print statements, this sounds useful in understanding in whats going on in the code. How do I implement this?

Thanks

Matt

trying to find just the basics rescources of programming is not easy

Actually, it is. There are plenty of places where you can find basic C programming information. Look on Google or even your local library. Try some of the example sketches. The DigitalReadSerial example will show you how to do Serial.print().

Replace the call to alloff(); with didThis=false; before color++, execute digitalWrite(color,0); // turn off current pin

Thanks wildbill I couldn't get that to work (user error I know).

I did get it work as I wanted, LED staying on until button pushed etc. by using this:

void loop()
{
int buttonState = digitalRead(button); // What is the state of the button?

if ((buttonState != val) // only look at state changes
&& (buttonState == 1)) // Button just pressed
{
allOff(); // Go turn the LED off
lightMyFire(); // Go turn the LED on
}

val = buttonState; // remember button state for next time
delay(20); // weed out any key bounce
}

I had to extend the delay as it cycled to quickly.

Thanks for your help

Matt

You need a latch circuit, maybe something with Boolean in it (true/false), turn on the led then keep it there unless the button equals the opposite state than the one it is on, thinking of the code myself!!

rgds

That sounds like what I need. This project is only a stepping stone, I want to make a mutli-channel switching pedal for guitars/amps using relays to switch channels, hence the need for the LED in this case to stay on until switched.

Matt

You could attach an interrupt using attachInterrupt()

if you are using an uno attach your pedal to digital input 2, use a function to cycle through numbers of your choice, (the amount of LED's you have), call this thing something like - int PushCounter -.

Then use if, else if statements to activate the right light depending on the number you get from your function. once you get to the last LED, reset the counter. Do this by saying something like,

if (PushCounter == 5)
{
PushCounter = 0;
}

this however is a linear sequence, not a matrix switcher, but think it should be ok

read up on interrupts from the reference page of the arduino website.

//Try this
// Best for modifying

int LEDOne = 3;
int LEDTwo = 4;
int LEDThree = 5;
int LEDFour = 6;
int LEDFive = 7;

int Counter = 0;

void setup()

{
pinMode(LEDOne,OUTPUT);
digitalWrite(LEDOne,LOW);

pinMode(LEDTwo,OUTPUT);
digitalWrite(LEDTwo,LOW);

pinMode(LEDThree,OUTPUT);
digitalWrite(LEDThree,LOW);

pinMode(LEDFour,OUTPUT);
digitalWrite(LEDFour,LOW);

pinMode(LEDFive,OUTPUT);
digitalWrite(LEDFive,LOW);

attachInterrupt(0, PedalPress, RISING);
}

void loop()

{

if(Counter == 1)
{
digitalWrite(LEDOne,HIGH);
digitalWrite(LEDTwo,LOW);
digitalWrite(LEDThree,LOW);
digitalWrite(LEDFour,LOW);
digitalWrite(LEDFive,LOW);
}

else if(Counter == 2)
{
digitalWrite(LEDOne,LOW);
digitalWrite(LEDTwo,HIGH);
digitalWrite(LEDThree,LOW);
digitalWrite(LEDFour,LOW);
digitalWrite(LEDFive,LOW);
}

else if(Counter == 3)
{
digitalWrite(LEDOne,LOW);
digitalWrite(LEDTwo,LOW);
digitalWrite(LEDThree,HIGH);
digitalWrite(LEDFour,LOW);
digitalWrite(LEDFive,LOW);
}

else if(Counter == 4)
{
digitalWrite(LEDOne,LOW);
digitalWrite(LEDTwo,LOW);
digitalWrite(LEDThree,LOW);
digitalWrite(LEDFour,HIGH);
digitalWrite(LEDFive,LOW);
}

else if(Counter == 5)
{
digitalWrite(LEDOne,LOW);
digitalWrite(LEDTwo,LOW);
digitalWrite(LEDThree,LOW);
digitalWrite(LEDFour,LOW);
digitalWrite(LEDFive,HIGH);
}

else
{
digitalWrite(LEDOne,LOW);
digitalWrite(LEDTwo,LOW);
digitalWrite(LEDThree,LOW);
digitalWrite(LEDFour,LOW);
digitalWrite(LEDFive,LOW);
}

if (Counter > 5)
{
Counter = 0;
}

}

void PedalPress()

{
Counter++;
}