2 LED Sequence

I want to do a project where i have 8 LED's First time button is pushed the first 2 LED's blink Second time button is pushed LED 3 & 4 blink Third time LED 5 & 6 blink Fourth time LED 7 & 8 blink and 5th time button is pressed, the first sequence where the first two leds blink start again etc...

I am very basic when it comes to programming. Please get me started and help me start off the code with switching sequences?

Take a look at the BlinkWithoutDelay for writing non-blocking blinking code.

Take a look at the StateChangeDetection example for handling button toggling.

Create a state machine in which the state is changed based on the momentary switch being pressed/released. The state will determine which LEDs blink.

Don't try to get everything working at once, get a small part working. Study it to the point where you understand it. Then, add another feature. Rinse and repeat.

Anyways the trouble for me is that i understand all the basics

That’s a problem?

I believe the detect state change should be used, but i need to use it so that it works when the button is pressed and released for 1, when its pressed and released again for 2, and pressed and released to play 1 again.

So, what’s the problem? Detecting that a transition occurred (from pressed to released or from released to pressed) is easy. Determining which transition occurred (based on the current state) is easy.

Increment a counter when the transition of interest occurs. This makes it your decision whether to increment on the transition to pressed or on the transition to released.

This is very confusing for me to understand, but i believe i need to use loop for the led code, and if statements for sequencing.

This implies that your first statement is false, and that, in fact, you do not understand the basics. There is no difference between the for loop incrementing a value and you incrementing a value. None.

Please let me know if you have any idea’s, I’ve been trying for the past few days and i’ve been getting nowhere when it comes to sequencing.

What I do when I’m having problems is I write the comments first. Then, I write the code, and more comments. If you do that, and get stuck, we can at least review the comments, and help you make sure that they are logical and describe the sequence of operations that are needed.

You are completely right. I have no idea what I'm doing to be honest. I don't know how to put everything together

Anyways something I got help starting off is to find the amount of times the button is clicked..I'm having trouble finishing it off, and I know it'll take a while..I am still trying to learn and find out how. What I'm trying to do is use the amount of times it is clicked as in the first time its clicked, the first sequence plays, then the second click the first sequence, the third click first sequence and so on.

const int ledPin1 = 13 const int ledPin2 = 12 const int pushButton = 2

void loop() { buttonsState = digitalRead (pushButton)); if (buttonState != lastButtonState) {

if (buttonState == HIGH) { buttonPushCounter ++ ; Serial.println ("ON"); Serial.print ("Amount of button clicks": "); Serial.println(buttonPushCounter); } else {

Serial.println ("off"); } } lastButtonState = buttonState lastButtonState = buttonState

Serial.print ("Amount of button clicks: "); *******************

Some things you should do from the start. Each { goes on a new line. Learn to use Tools + Auto Format often.

Every statement ends with a ;

You only need to assign a value to lastButtonState once.

Other than the missing ; on the last statement, the duplicate statements, and the missing } for the loop() function, what is the problem?

The code looks OK so far, with the exceptions mentioned above.

Took your advice used autoformat. I am making it much worse for myself I believe to be honest. I am trying, but having too much difficulty understanding what to do next or what to fix up. The code is pretty much chopped up and completely useless at this point. I am trying to get the following outputs to occur, the first one is what is already attached to the code, and the second one, I'm having trouble even thinking of how to make it a part of this. I am trying to learn, but having too much trouble and confusion.

const int ledPin1 = 13 const int ledPin2 = 12 const int pushButton = 2

void loop() { buttonsState = digitalRead (pushButton); if (buttonState != lastButtonState) {

if (buttonState == HIGH) { buttonPushCounter ++ ; Serial.println ("ON"); Serial.print ("Amount of button clicks: "); Serial.println(buttonPushCounter); } else { Serial.println ("off"); } } lastButtonState = buttonState }

void setup() { pinMode(ledPin1, OUTPUT); digitalWrite(ledPin1, HIGH); delay(200); digitalWrite(ledPin1, LOW); delay(100); digitalWrite(ledPin1, HIGH); delay(200); digitalWrite(ledPin1, LOW); delay(100); digitalWrite(ledPin1, HIGH); delay(200); digitalWrite(ledPin1, LOW); delay(200); digitalWrite(ledPin1, HIGH); delay(500); digitalWrite(ledPin1, LOW); delay(200); digitalWrite(ledPin1, HIGH); delay(500); digitalWrite(ledPin1, LOW); delay(200); digitalWrite(ledPin1, HIGH); delay(500); digitalWrite(ledPin1, LOW); delay(200); digitalWrite(ledPin1, HIGH); delay(100); digitalWrite(ledPin1, LOW); delay(100); digitalWrite(ledPin1, HIGH); delay(100); digitalWrite(ledPin1, LOW); delay(100); }

void setup() { pinMode(ledPin2, ledPin2, OUTPUT); } void loop() { digitalWrite(ledPin1, HIGH); delay(200); digitalWrite(ledPin1, LOW); delay(100); digitalWrite(ledPin1, HIGH); delay(200); digitalWrite(ledPin2, HIGH); delay(200); digitalWrite(ledPin2, LOW); delay(100); digitalWrite(ledPin2, HIGH); delay(200); }

const int ledPin1 = 13
const int ledPin2 = 12
const int pushButton = 2

All statements end with ;

You can only have one setup() function and one loop() function.

Ignoring for the moment what you want to have happen when the switch has been pressed n times, are you correctly counting switch presses?

Try this:

const int pushButtonPin = 2;

int pressCount = 0;

int prevState = LOW;
int currState;

void setup()
{
   pinMode(pushButtonPin, INPUT);
   digitalWrite(pushButtonPin, HIGH); // Turn on pullup resistor
   Serial.begin(115200);
}

void loop()
{
  currState = digitalRead(pushButtonPin);
  if (currState != prevState)
  {
    if (currState == LOW)
    {
      pressCount++;
      Serial.print("Press count:   ");
      Serial.println(pressCount);
    }
    delay(10);
  }
  prevState = currState;
}

Connect one side of the switch to pin 2. Connect the other side to ground. No external resistors are needed. Open the Serial Monitor. Do you see "Press count: 1", "Press count: 2", etc, with increasing values each time you press the switch? Does the value increase only when you press the switch?

The biggest problem that you are having right now is trying to learn about the hardware and the software at the same time, without understanding either. Start with very basic sketches like this to prove that the hardware (and minimal software) is working. Then, add more hardware that does not get used, to verify that the new hardware does not cause problems. Then, add software to make the new hardware do something. One step at a time.

Would this be anywhere close to what i'm working towards? im uncertain of the button controls still and theres an error in declaration i can't figure out

const int analogPin = 2; // Pushbutton pin const int ledPin1 = 13; // RED LED const int ledPin2 = 12; // GREEN LED const int threshold = 400; // threshold in rance of output

void setup() { pinMode(ledPin1, ledPin2, OUTPUT); //red and green LED's introduced as output Serial.begin(9600); //initialize serial communications }

void loop() { int analogValue = analogRead(analogPin); //Read value of button

// if the analog value is high enough, turn on the LED: if (analogValue > threshold) { digitalWrite(ledPin1, HIGH); delay(200); digitalWrite(ledPin1, LOW); delay(100); digitalWrite(ledPin1, HIGH); delay(200); digitalWrite(ledPin1, LOW); delay(100); digitalWrite(ledPin1, HIGH); delay(200); digitalWrite(ledPin1, LOW); delay(100); digitalWrite(ledPin1, HIGH); delay(1000); digitalWrite(ledPin1, LOW); delay(500); digitalWrite(ledPin1, HIGH); delay(1000); digitalWrite(ledPin1, LOW); delay(500); digitalWrite(ledPin1, HIGH); delay(1000); digitalWrite(ledPin1, LOW); delay(500); digitalWrite(ledPin1, HIGH); delay(200); digitalWrite(ledPin1, LOW); delay(100); digitalWrite(ledPin1, HIGH); delay(200); digitalWrite(ledPin1, LOW); delay(100); digitalWrite(ledPin1, HIGH); delay(200); digitalWrite(ledPin1, LOW); delay(100); } else { digitalWrite(ledPin1, HIGH); delay(1000); digitalWrite(ledPin1, LOW); delay(500); digitalWrite(ledPin2, HIGH); delay(1000); digitalWrite(ledPin2, LOW); delay(500); digitalWrite(ledPin1, HIGH); delay(1000); digitalWrite(ledPin1, LOW); delay(500); digitalWrite(ledPin2, HIGH); delay(1000); digitalWrite(ledPin2, LOW); delay(500); digitalWrite(ledPin1, HIGH); delay(1000); digitalWrite(ledPin1, LOW); delay(500); digitalWrite(ledPin2, HIGH); delay(1000); digitalWrite(ledPin2, LOW); delay(500); }

// print the analog value: Serial.println(analogValue); delay(1); // delay in between reads for stability }

Sequence one: SOS Sequence two: Vacation (green and red flashing one after another)

Sequence one must start once button is pressed, sequence two when button is pressed again, and each sequence continues until button is pressed. I can only use one loop per program... I have some what of a code lay out, I don't know where I'm going with this, all I understand is that I would need to use If conditional statements. What I Have so far:

const int analogPin = 2; // Pushbutton pin const int ledPin1 = 13; // RED LED const int ledPin2 = 12; // GREEN LED const int threshold = 400; // threshold in rance of output

void setup() { pinMode(ledPin1, ledPin2, OUTPUT); //red and green LED's introduced as output Serial.begin(9600); //initialize serial communications }

void loop() { int analogValue = analogRead(analogPin); //Read value of button

// if the analog value is high enough, turn on the LED: if (analogValue > threshold) { digitalWrite(ledPin1, HIGH); delay(200); digitalWrite(ledPin1, LOW); delay(100); digitalWrite(ledPin1, HIGH); delay(200); digitalWrite(ledPin1, LOW); delay(100); digitalWrite(ledPin1, HIGH); delay(200); digitalWrite(ledPin1, LOW); delay(100); digitalWrite(ledPin1, HIGH); delay(1000); digitalWrite(ledPin1, LOW); delay(500); digitalWrite(ledPin1, HIGH); delay(1000); digitalWrite(ledPin1, LOW); delay(500); digitalWrite(ledPin1, HIGH); delay(1000); digitalWrite(ledPin1, LOW); delay(500); digitalWrite(ledPin1, HIGH); delay(200); digitalWrite(ledPin1, LOW); delay(100); digitalWrite(ledPin1, HIGH); delay(200); digitalWrite(ledPin1, LOW); delay(100); digitalWrite(ledPin1, HIGH); delay(200); digitalWrite(ledPin1, LOW); delay(100); } else { digitalWrite(ledPin1, HIGH); delay(1000); digitalWrite(ledPin1, LOW); delay(500); digitalWrite(ledPin2, HIGH); delay(1000); digitalWrite(ledPin2, LOW); delay(500); digitalWrite(ledPin1, HIGH); delay(1000); digitalWrite(ledPin1, LOW); delay(500); digitalWrite(ledPin2, HIGH); delay(1000); digitalWrite(ledPin2, LOW); delay(500); digitalWrite(ledPin1, HIGH); delay(1000); digitalWrite(ledPin1, LOW); delay(500); digitalWrite(ledPin2, HIGH); delay(1000); digitalWrite(ledPin2, LOW); delay(500); }

// print the analog value: Serial.println(analogValue); delay(1); // delay in between reads for stability }

First read how to use this forum and post the code correctly.

Next that is not the way to do anything. Look at the blink without delay example in the IDE.

What was wrong with your other thread?

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Threads merged.

How to use this forum

Read this before posting a programming question

Code tags, please. How many times do we need to ask?

  • Moderator

I had trouble finding it, sorry!

Could someone please help me? I had gone over the basics and about 4 hours of trial and error got me here

// constant integer variables, will not change const int buttonPin = 2; // the pin that the pushbutton is attached to const int greenLedPin = 12; // the pin that the LED is attached to const int redLedPin = 13;

// integer variables, will be altered int buttonPushCounter = 0; // counter for the number of button presses int buttonState = 0; // current state of the button int lastButtonState = 0; // previous state of the button

void setup() { // initialize the button pin as a input: pinMode(buttonPin, INPUT); // initialize the RED LED as an output: pinMode(redLedPin, OUTPUT); // initialize the GREEN LED as an output: pinMode(greenLedPin, OUTPUT); // initialize serial communication: Serial.begin(9600); }

void loop() { // read the pushbutton input pin: buttonState = digitalRead(buttonPin);

// compare the buttonState to its previous state if (buttonState != lastButtonState) { // if the state has changed, increment the counter if (buttonState == HIGH) { // if the current state is HIGH then the button // went from off to on: buttonPushCounter++; } } // save the current state as the last state, //for next time through the loop lastButtonState = buttonState;

// call displaySequence method with buttonPushCounter as parameter displaySequence(buttonPushCounter); }

void displaySequence(int buttonPushCounter) { // check the number of button pressed and follow sequence according to it // If buttonPushCounter is pressed the first time, blink Red LED in the sequence // If buttonPushCounter is pressed the second time, blink Red and Green as part of th sequence // If buttonPushCounter is pressed the third time, reset the counter back to 0 so that the sqeuence can be played again if (buttonPushCounter == 1) { digitalWrite(redLedPin, HIGH); delay(100); digitalWrite(redLedPin, LOW); delay(100); digitalWrite(redLedPin, HIGH); delay(100); digitalWrite(redLedPin, LOW); delay(100); digitalWrite(redLedPin, HIGH); delay(100); digitalWrite(redLedPin, LOW); delay(200); digitalWrite(redLedPin, HIGH); delay(400); digitalWrite(redLedPin, LOW); delay(200); digitalWrite(redLedPin, HIGH); delay(400); digitalWrite(redLedPin, LOW); delay(200); digitalWrite(redLedPin, HIGH); delay(400); digitalWrite(redLedPin, LOW); delay(200); digitalWrite(redLedPin, HIGH); delay(100); digitalWrite(redLedPin, LOW); delay(100); digitalWrite(redLedPin, HIGH); delay(100); digitalWrite(redLedPin, LOW); delay(100); digitalWrite(redLedPin, HIGH); delay(100); digitalWrite(redLedPin, LOW); delay(500); } else if(buttonPushCounter == 2) { digitalWrite(redLedPin, HIGH); delay(200); digitalWrite(redLedPin, LOW); delay(400); digitalWrite(greenLedPin, HIGH); delay(200); digitalWrite(greenLedPin, LOW); delay(400); digitalWrite(redLedPin, HIGH); delay(200); digitalWrite(redLedPin, LOW); delay(400); digitalWrite(greenLedPin, HIGH); delay(200); digitalWrite(greenLedPin, LOW); delay(100); digitalWrite(redLedPin, HIGH); delay(200); digitalWrite(redLedPin, LOW); delay(400); digitalWrite(greenLedPin, HIGH); delay(200); digitalWrite(greenLedPin, LOW); delay(400); } else if(buttonPushCounter == 3) { buttonPushCounter = 0; } }

This code basically starts the first sequence when the button is pressed, and when the button is pressed and held down it changes the sequence once the first one is over and then when its pressed and held again, it stops. My goal is to press button once sequence 1 starts, press button again to start sequence 2, press again to start sequence 1, press again to start seq 2 again, etc. Anyways does anyone see anything i could do to make it work?

Get rid of delay() calls

fdchvekuu8: Could someone please help me? I had gone over the basics and about 4 hours of trial and error got me here ... Anyways does anyone see anything i could do to make it work?

See reply #12.

In particular:

http://arduino.cc/forum/index.php/topic,148850.msg1118324.html#post_codetags

If you choose to ignore me, again, I will lock the thread. If you start another thread, there will be trouble.

Thank you baby :blush:

fdchvekuu8: My goal is to press button once sequence 1 starts, press button again to start sequence 2, press again to start sequence 1, press again to start seq 2 again, etc. Anyways does anyone see anything i could do to make it work?

Sounds like a job for a Finite State Machine to me. :)