Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Stopgame
- //Ethan Cook
- int LEDcontrolpin[5] = {2, 3, 4, 5, 6}; //Declares the array of LED pins that will be used
- int buttonPin = 7; //Our button
- int x; // have to declare globaly because its used in the loop and function
- int i; // ^^^^
- int levelTime; //Using int because the max value it will be in 1000
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(9600);
- pinMode(2, OUTPUT); //Setting up pins
- pinMode(3, OUTPUT);
- pinMode(4, OUTPUT);
- pinMode(5, OUTPUT);
- pinMode(6, OUTPUT);
- pinMode(buttonPin, INPUT);
- x = 0;
- i = 0; //We start at level 0
- y = 0;
- levelTime = 1000; //This will be the first delay between the LEDs as well as out base at level 0
- }
- void loop() {
- // put your main code here, to run repeatedly:
- int buttonState = digitalRead(buttonPin); //Checks button for power
- if (buttonState == 0) {
- LEDsequence(); //Calls functuion
- }
- //Checks if the button has power and x is 3 if x is three then the target LED will be lit so the user will have passed the level
- if (buttonState == 1 && x == 3) {
- i++; //We add 1 to i to up the level by one
- Serial.print("Current Level: ");
- Serial.println(i); // i keeps track of the levels
- levelTime = (1000 - (i * 100)); //we get 10 levels this way each level getting harder by 100 milliseconds
- delay(1000);
- } else {
- //If you push the button on the wrong LED this if statement will trigger
- if(buttonState == 1 && x != 3) {
- Serial.print("Level Reached: ");
- Serial.println(i); //Tells you what level you hit
- i = 0; // Restarts the levels
- levelTime = 1000; //Resets the delay inbetween flashes
- delay(1000);
- }
- }
- }
- void LEDsequence() {
- int buttonState = digitalRead(buttonPin);
- //We have it checking if x is less than or equal to 4 because thats the maximum places that our array holds
- if (x <= 4 && buttonState == 0) {
- digitalWrite(LEDcontrolpin[x], HIGH); //Turns on the pin in the x place of the array
- delay(levelTime); //Delay between LED is decided based on the level
- digitalWrite(LEDcontrolpin[x], LOW);
- x++; //Add one to x allowing us to go down our list of LEDs
- } else {
- x = 0; //When x is higher then 4 it is reset so the if statement above will run
- }
- }
Add Comment
Please, Sign In to add comment