Guest User

Untitled

a guest
Sep 24th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. //Stopgame
  2. //Ethan Cook
  3.  
  4.  
  5. int LEDcontrolpin[5] = {2, 3, 4, 5, 6}; //Declares the array of LED pins that will be used
  6. int buttonPin = 7; //Our button
  7. int x; // have to declare globaly because its used in the loop and function
  8. int i; // ^^^^
  9. int levelTime; //Using int because the max value it will be in 1000
  10.  
  11. void setup() {
  12. // put your setup code here, to run once:
  13. Serial.begin(9600);
  14. pinMode(2, OUTPUT); //Setting up pins
  15. pinMode(3, OUTPUT);
  16. pinMode(4, OUTPUT);
  17. pinMode(5, OUTPUT);
  18. pinMode(6, OUTPUT);
  19. pinMode(buttonPin, INPUT);
  20. x = 0;
  21. i = 0; //We start at level 0
  22. y = 0;
  23. levelTime = 1000; //This will be the first delay between the LEDs as well as out base at level 0
  24. }
  25.  
  26.  
  27. void loop() {
  28. // put your main code here, to run repeatedly:
  29. int buttonState = digitalRead(buttonPin); //Checks button for power
  30. if (buttonState == 0) {
  31. LEDsequence(); //Calls functuion
  32. }
  33. //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
  34. if (buttonState == 1 && x == 3) {
  35. i++; //We add 1 to i to up the level by one
  36. Serial.print("Current Level: ");
  37. Serial.println(i); // i keeps track of the levels
  38. levelTime = (1000 - (i * 100)); //we get 10 levels this way each level getting harder by 100 milliseconds
  39. delay(1000);
  40. } else {
  41. //If you push the button on the wrong LED this if statement will trigger
  42. if(buttonState == 1 && x != 3) {
  43. Serial.print("Level Reached: ");
  44. Serial.println(i); //Tells you what level you hit
  45. i = 0; // Restarts the levels
  46. levelTime = 1000; //Resets the delay inbetween flashes
  47. delay(1000);
  48. }
  49. }
  50. }
  51.  
  52. void LEDsequence() {
  53. int buttonState = digitalRead(buttonPin);
  54. //We have it checking if x is less than or equal to 4 because thats the maximum places that our array holds
  55. if (x <= 4 && buttonState == 0) {
  56. digitalWrite(LEDcontrolpin[x], HIGH); //Turns on the pin in the x place of the array
  57. delay(levelTime); //Delay between LED is decided based on the level
  58. digitalWrite(LEDcontrolpin[x], LOW);
  59. x++; //Add one to x allowing us to go down our list of LEDs
  60. } else {
  61. x = 0; //When x is higher then 4 it is reset so the if statement above will run
  62. }
  63. }
Add Comment
Please, Sign In to add comment