Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. const int buttonPin = 2; // the pin that the pushbutton is attached to
  2. // the pin that the LED is attached to
  3. const int led1Pin = 00;
  4. const int led2Pin = 00;
  5. const int led3Pin = 00;
  6. const int led4Pin = 00;
  7.  
  8. // keep track if the led is high or low
  9. int led1State = LOW;
  10. int led2State = LOW;
  11. int led3State = LOW;
  12. int led4State = LOW;
  13.  
  14. // Variables will change:
  15. int buttonPushCounter = 0; // counter for the number of button presses
  16. int buttonState = 0; // current state of the button
  17. int lastButtonState = 0; // previous state of the button
  18.  
  19. int numToGuess = 1; // number to guess
  20.  
  21. void setup() {
  22. // initialize the button pin as a input:
  23. pinMode(buttonPin, INPUT);
  24. // initialize the LED as an output:
  25. pinMode(led1Pin, OUTPUT);
  26. pinMode(led2Pin, OUTPUT);
  27. pinMode(led3Pin, OUTPUT);
  28. pinMode(led4Pin, OUTPUT);
  29. // initialize serial communication:
  30. Serial.begin(9600);
  31.  
  32. decToBin();
  33. }
  34.  
  35. void decToBin() {
  36. // Set every led to LOW
  37. digitalWrite(led1Pin, LOW);
  38. digitalWrite(led2Pin, LOW);
  39. digitalWrite(led3Pin, LOW);
  40. digitalWrite(led4Pin, LOW);
  41.  
  42. // Make a copy of the number to guess.
  43. int copy = numToGuess;
  44. Serial.print("Number to guess: ");
  45. Serial.println(copy);
  46.  
  47. if ((copy - 8) > -1) {
  48. copy = copy - 8;
  49. led1State = HIGH;
  50. digitalWrite(led1Pin, led1State);
  51. }
  52. if ((copy - 4) > -1) {
  53. copy = copy - 4;
  54. led2State = HIGH;
  55. digitalWrite(led2Pin, led2State);
  56. }
  57. if ((copy - 2) > -1) {
  58. copy = copy - 2;
  59. led3State = HIGH;
  60. digitalWrite(led3Pin, led3State);
  61. }
  62. if ((copy - 1) > -1) {
  63. copy = copy - 1;
  64. led4State = HIGH;
  65. digitalWrite(led4Pin, led4State);
  66. }
  67. }
  68.  
  69. void loop() {
  70. // read the pushbutton input pin:
  71. buttonState = digitalRead(buttonPin);
  72.  
  73. // compare the buttonState to its previous state
  74. if (buttonState != lastButtonState) {
  75. // if the state has changed, increment the counter
  76. if (buttonState == HIGH) {
  77. // if the current state is HIGH then the button
  78. // wend from off to on:
  79. buttonPushCounter++;
  80. Serial.println("on");
  81. Serial.print("number of button pushes: ");
  82. Serial.println(buttonPushCounter);
  83.  
  84. checkIfNumIsGuessed();
  85. } else {
  86. // if the current state is LOW then the button
  87. // wend from on to off:
  88. Serial.println("off");
  89. }
  90. // Delay a little bit to avoid bouncing
  91. delay(50);
  92. }
  93. // save the current state as the last state,
  94. //for next time through the loop
  95. lastButtonState = buttonState;
  96. }
  97.  
  98. void checkIfNumIsGuessed() {
  99. if (buttonPushCounter == numToGuess) {
  100. Serial.println("You guessed the number right! Next number...");
  101. buttonPushCounter = 0;
  102. numToGuess++;
  103. if (numToGuess > 15) {
  104. numToGuess = 1;
  105. }
  106. decToBin();
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement