Skill stop with LED

Hi, I am trying to program a skill stop function but can not get it working. I have 3 LEDs that i have programmed to go back and forth ( i.e. from 11 to 12 to 13 to 12 and loop back to 11) and what I want is when i push button 3 the LED will stop running and stop at the position when the button was pushed. I don't know what I am doing wrong here, is there someone who can help me out here?

My setup is:
3 LED
2 Pushbuttons

The program:

const int buttonPin2 = 2;
const int buttonPin3 = 3;
const int LedPin11 = 11;
const int LedPin12 = 12;
const int LedPin13 = 13;

boolean Led11Status = LOW;
boolean Led12Status = LOW;
boolean Led13Status = LOW;

void setup() {
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(LedPin11, OUTPUT);
pinMode(LedPin12, OUTPUT);
pinMode(LedPin13, OUTPUT);
}

void loop(){

if(digitalRead(buttonPin2)==LOW && digitalRead(buttonPin3)==HIGH)
{
digitalWrite(LedPin11, HIGH);
Led11Status = HIGH;
delay(100);
digitalWrite(LedPin11, LOW);
Led11Status = LOW;

digitalWrite(LedPin12, HIGH);
Led12Status = HIGH;
delay(100);
digitalWrite(LedPin12, LOW);
Led12Status = LOW;

digitalWrite(LedPin13, HIGH);
Led13Status = HIGH;
delay(100);
digitalWrite(LedPin13, LOW);
Led13Status = LOW;

digitalWrite(LedPin12, HIGH);
Led12Status = HIGH;
delay(100);
digitalWrite(LedPin12, LOW);
Led12Status = LOW;
}

else if(digitalRead(buttonPin2)==LOW && digitalRead(buttonPin3)==LOW)
{
if (Led11Status)
{
digitalWrite(LedPin11, HIGH);
digitalWrite(LedPin12, LOW);
digitalWrite(LedPin13, LOW);
}
else if (Led12Status)
{
digitalWrite(LedPin12, HIGH);
digitalWrite(LedPin11, LOW);
digitalWrite(LedPin13, LOW);
}
else if (Led13Status)
{
digitalWrite(LedPin13, HIGH);
digitalWrite(LedPin11, LOW);
digitalWrite(LedPin12, LOW);
}
}
}

  if(digitalRead(buttonPin2)==LOW && digitalRead(buttonPin3)==HIGH)
  {
    digitalWrite(LedPin11, HIGH);
    Led11Status = HIGH;
    delay(100);
    digitalWrite(LedPin11, LOW);
    Led11Status = LOW;
  
    digitalWrite(LedPin12, HIGH);
    Led12Status = HIGH;
    delay(100);
    digitalWrite(LedPin12, LOW);
    Led12Status = LOW;
  
    digitalWrite(LedPin13, HIGH);
    Led13Status = HIGH;
    delay(100);
    digitalWrite(LedPin13, LOW);
    Led13Status = LOW;
  
    digitalWrite(LedPin12, HIGH);
    Led12Status = HIGH;
    delay(100);
    digitalWrite(LedPin12, LOW);
    Led12Status = LOW;
  }

the way you have your loop set up, the only time the LED's will cycle is when button2 is low and button3 is high. which is opposite from what you want.

What is supposed to happen after button 3 is pressed? Does the program stop for a few second and continues or ... ?

The loop, as it is currently written, will:

  • Cycle the LEDs, lighting 11, then 12, then 13, then 12
  • Extinguish all the LEDs
  • Check to see if the button has been pressed

This is because you light those LEDs in order followed by delays, then at the end of that IF statement you have another IF statement that tests for the button being pushed.

Here is some pseudo-code to point you in the right direction of how to get this skill game working:

int running;
int lit_LED = D;

loop:
	if button2 is on and button3 is off
	  set the 'running' flag
	else
	  clear the 'running' flag
	
	if 'running'
	  if lit_LED == A
		extinguish LED A
		light LED B
		set lit_LED to B
	  if lit_LED == B
		extinguish LED B
		light LED C
		set lit_LED to C
	  if lit_LED == C
		extinguish LED C
		light LED B
		set lit_LED to D
	  if lit_LED = D
		extinguish LED B
		light LED A
		set lit_LED to A
	
	delay for a short period

This is a very basic "state machine" with lit_LED representing the "state" of the machine. Each cycle around the loop will change the state of the machine. Note that state "B" and "D" represent lighting LED B, one when going from A to C, the other when going from C to A.

There are better ways to do this, I'm sure, but in the meantime this should get you to a working game!

You also need to check up on switch debouncing otherwise you are likely going to have problems with false readings of the switches.

You also need to check up on switch debouncing otherwise you are likely going to have problems with false readings of the switches.

With 400 millisecond delays? Not likely...

To address the "debouncing" issue I would move the detection of the "competitor" button to an interrupt input rather than a digital input. In the interrupt handler, simply set the "button pressed" flag. Then in the main loop, rather than checking the digital input, check the "button pressed" flag, and clear it at the beginning of the loop when the "run game" button is released.

Hi all, thank you for your hints and tips, now my skill stop function is working :slight_smile:
I am so happy!!!!!