const int buttonPinUp = 12;
const int buttonPinLeft = 11;
const int buttonPinRight = 10;
const int ledPinye1 = 5;
const int ledPingre1 = 4;
const int ledPinye2 = 3;
const int ledPingre2 = 2;
const int ledPinblue1 = 6;
const int ledPinblue2 = 7;
const int ledPinred1 = 8;
const int ledPinred2 = 9;
const int ledPinred3 = 13;
int LastButtonUpState = HIGH;
int LastButtonLeftState = HIGH;
int LastButtonRightState = HIGH;
int LedState = 0;
/* In the beginning, we set the variable LedState to 0.
Here's the programming logic of this game:
if the button of the correct direction was clicked when LedState is 0,
LedState became 1. With "if loop", we configurate the state of all Leds:
which are on and which are off in condition that LedState is 1.
if the button of the correct direction was clicked when LedState is 1,
LedState became 2. With "if loop", we configurate the state of all Leds:
which are on and which are off in condition that LedState is 2.
if...
...*/
//LedState can be imagined as a configuration.
//All the "configurations" are setted up below.
//LedState = 0 -->configuation number 0: the first led on the way is lit
//LedState = 1 -->configuation number 1: the second led on the way is lit
void setup(){
Serial.begin(9600);
pinMode(buttonPinUp,INPUT_PULLUP); /*INPUT_PULLUP turns on the internal
resistor which has the guarantied resistance between 20kΩ and 50kΩ.
For more information, consult Arduino official website's digital pins
section: https://docs.arduino.cc/learn/microcontrollers/digital-pins */
pinMode(buttonPinLeft, INPUT_PULLUP);
pinMode(buttonPinRight, INPUT_PULLUP);
pinMode(ledPinye1, OUTPUT);
pinMode(ledPinye2, OUTPUT);
pinMode(ledPingre1, OUTPUT);
pinMode(ledPingre2, OUTPUT);
pinMode(ledPinblue1, OUTPUT);
pinMode(ledPinblue2, OUTPUT);
pinMode(ledPinred1, OUTPUT);
pinMode(ledPinred2, OUTPUT);
pinMode(ledPinred3, OUTPUT);
digitalWrite(ledPinye1, HIGH); /*When arduino is powered,ledPinyel pin
is on to lighten the LED which shows the player's initial position.*/
digitalWrite(ledPingre1, LOW);
digitalWrite(ledPinye2, LOW);
digitalWrite(ledPingre2, LOW);
digitalWrite(ledPinblue1, LOW);
digitalWrite(ledPinblue2, LOW);
digitalWrite(ledPinred1, LOW);
digitalWrite(ledPinred2, LOW);
digitalWrite(ledPinred3, LOW);
}
void loop(){
int buttonstate = 0; /*create a new variable "buttonstate
and set its initial value to 0*/
buttonstate = digitalRead(buttonPinUp); /*get how many time the up-button
was clicked*/
if(buttonstate == LOW &&
buttonstate != LastButtonUpState){ /*If the buttonstate is bigger than
the before(the up-button was clicked)*/
if(LedState == 7) /*If when the up-button i clicked, LedState is 7
(we are at step 7/the configuration 7)*/
LedState = 8; /*LedState will be 8(the configuration of each LedState
are below)*/
if(LedState == 5)
LedState = 6;
else if(LedState == 3)
LedState = 4;
else if(LedState == 0)
LedState = 1;
}
LastButtonUpState = buttonstate; /*updat the value of LastButtonUpState in
order to make them egal to each other before the new click of button*/
buttonstate = digitalRead(buttonPinLeft); /*get how many time the
left-button was clicked*/
if(buttonstate == LOW &&
buttonstate != LastButtonLeftState){ /*If the buttonstate is bigger than
the before(the left-button was clicked)*/
if(LedState == 4)
LedState = 5;
else if(LedState == 2)
LedState = 3;
else if(LedState == 1)
LedState = 2;
}
LastButtonLeftState = buttonstate;
buttonstate = digitalRead(buttonPinRight);
if(buttonstate == LOW &&
buttonstate != LastButtonRightState){
if(LedState == 6)
LedState = 7;
}
LastButtonRightState = buttonstate;
//print constantly the present value of each variable on the serial monitor
Serial.print(LedState);
Serial.print(" ");
Serial.print(LastButtonUpState);
Serial.print(" ");
Serial.print(LastButtonLeftState);
Serial.print(" ");
Serial.println(LastButtonRightState);
//////////////////Here comes the "configurations" of when LedState = 0 to 8/
///////////////////////////////////////////////////////////////////////////
if(LedState == 8){ //configuation number 8
digitalWrite(ledPinye1, LOW);
digitalWrite(ledPingre1, LOW);
digitalWrite(ledPinye2, LOW);
digitalWrite(ledPingre2, LOW);
digitalWrite(ledPinblue1, LOW);
digitalWrite(ledPinblue2, LOW);
digitalWrite(ledPinred1, LOW);
digitalWrite(ledPinred2, LOW);
digitalWrite(ledPinred3, HIGH);
}
if(LedState == 7){ //configuration number 7
digitalWrite(ledPinye1, LOW);
digitalWrite(ledPingre1, LOW);
digitalWrite(ledPinye2, LOW);
digitalWrite(ledPingre2, LOW);
digitalWrite(ledPinblue1, LOW);
digitalWrite(ledPinblue2, LOW);
digitalWrite(ledPinred1, LOW);
digitalWrite(ledPinred2, HIGH);
digitalWrite(ledPinred3, LOW);
}
if(LedState == 6){ //configuration number 6
digitalWrite(ledPinye1, LOW);
digitalWrite(ledPingre1, LOW);
digitalWrite(ledPinye2, LOW);
digitalWrite(ledPingre2, LOW);
digitalWrite(ledPinblue1, LOW);
digitalWrite(ledPinblue2, LOW);
digitalWrite(ledPinred1, HIGH);
digitalWrite(ledPinred2, LOW);
digitalWrite(ledPinred3, LOW);
}
if(LedState == 5){ //configuration number 5
digitalWrite(ledPinye1, LOW);
digitalWrite(ledPingre1, LOW);
digitalWrite(ledPinye2, LOW);
digitalWrite(ledPingre2, LOW);
digitalWrite(ledPinblue1, LOW);
digitalWrite(ledPinblue2, HIGH);
digitalWrite(ledPinred1, LOW);
digitalWrite(ledPinred2, LOW);
digitalWrite(ledPinred3, LOW);
}
if(LedState == 4){ //configuration number 4
digitalWrite(ledPinye1, LOW);
digitalWrite(ledPingre1, LOW);
digitalWrite(ledPinye2, LOW);
digitalWrite(ledPingre2, LOW);
digitalWrite(ledPinblue1, HIGH);
digitalWrite(ledPinblue2, LOW);
digitalWrite(ledPinred1, LOW);
digitalWrite(ledPinred2, LOW);
digitalWrite(ledPinred3, LOW);
}
if(LedState == 3) { //configuration number 3
digitalWrite(ledPinye1, LOW);
digitalWrite(ledPingre1, LOW);
digitalWrite(ledPinye2, LOW);
digitalWrite(ledPingre2, HIGH);
digitalWrite(ledPinblue1, LOW);
digitalWrite(ledPinblue2, LOW);
digitalWrite(ledPinred1, LOW);
digitalWrite(ledPinred2, LOW);
digitalWrite(ledPinred3, LOW);
}
if(LedState == 2) { //configuration number 2
digitalWrite(ledPinye1, LOW);
digitalWrite(ledPingre1, LOW);
digitalWrite(ledPinye2, HIGH);
digitalWrite(ledPingre2, LOW);
digitalWrite(ledPinblue1, LOW);
digitalWrite(ledPinblue2, LOW);
digitalWrite(ledPinred1, LOW);
digitalWrite(ledPinred2, LOW);
digitalWrite(ledPinred3, LOW);
}
if(LedState == 1) { //configuration number 1
digitalWrite(ledPinye1, LOW);
digitalWrite(ledPingre1, HIGH);
digitalWrite(ledPinye2, LOW);
digitalWrite(ledPingre2, LOW);
digitalWrite(ledPinblue1, LOW);
digitalWrite(ledPinblue2, LOW);
digitalWrite(ledPinred1, LOW);
digitalWrite(ledPinred2, LOW);
digitalWrite(ledPinred3, LOW);
}
if(LedState == 0) { //the initial configuration
digitalWrite(ledPinye1, HIGH);
digitalWrite(ledPingre1, LOW);
digitalWrite(ledPinye2, LOW);
digitalWrite(ledPingre2, LOW);
digitalWrite(ledPinblue1, LOW);
digitalWrite(ledPinblue2, LOW);
digitalWrite(ledPinred1, LOW);
digitalWrite(ledPinred2, LOW);
digitalWrite(ledPinred3, LOW);
}
}
Comments
Please log in or sign up to comment.