talofer99

hackaton

Nov 4th, 2018
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Wire.h>
  3. #include <Adafruit_GFX.h>
  4. #include <Adafruit_SSD1306.h>
  5.  
  6. #define OLED_RESET 4
  7. Adafruit_SSD1306 display(OLED_RESET);
  8.  
  9. // these constants won't change:
  10. const int ledPin = 13; // LED connected to digital pin 13
  11. const int knockSensor = A0; // the piezo is connected to analog pin 0
  12. const int threshold = 100; // threshold value to decide when the detected sound is a knock or not
  13.  
  14.  
  15. // these variables will change:
  16. int sensorReading = 0; // variable to store the value read from the sensor pin
  17. int ledState = LOW; // variable used to store the last LED status, to toggle the light
  18.  
  19. int buttonPin = 12;
  20. int counter = 0;
  21. int systemState;
  22.  
  23. byte objA;
  24. byte objB;
  25. byte answer;
  26.  
  27.  
  28. void setup() {
  29. pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
  30. pinMode(buttonPin, INPUT_PULLUP);
  31. Serial.begin(115200); // use the serial port
  32. randomSeed(analogRead(0) + analogRead(1) + analogRead(2) + analogRead(3) + analogRead(4) + analogRead(5));
  33. // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  34. display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)
  35. // init done
  36.  
  37. // Show image buffer on the display hardware.
  38. // Since the buffer is intialized with an Adafruit splashscreen
  39. // internally, this will display the splashscreen.
  40. display.display();
  41. delay(2000);
  42.  
  43. // Clear the buffer.
  44. display.clearDisplay();
  45. display.setTextSize(3);
  46. display.setTextColor(WHITE);
  47. setSystemState(0);
  48. }
  49.  
  50.  
  51. void loop() {
  52.  
  53. switch (systemState) {
  54. case 0:
  55. if (!digitalRead(buttonPin)) {
  56. setSystemState(1);
  57. } //end if
  58. break;
  59. case 1:
  60. // read the sensor and store it in the variable sensorReading:
  61. sensorReading = analogRead(knockSensor);
  62.  
  63. // if the sensor reading is greater than the threshold:
  64. if (sensorReading >= threshold) {
  65. // toggle the status of the ledPin:
  66. ledState = !ledState;
  67. // update the LED pin itself:
  68. digitalWrite(ledPin, ledState);
  69. // send the string "Knock!" back to the computer, followed by newline
  70. Serial.println("Knock!");
  71. counter++;
  72.  
  73. display.setCursor(0, 0);
  74. display.println(counter);
  75. display.display();
  76. delay(100); // delay to avoid overloading the serial port buffer
  77. display.clearDisplay();
  78. } // end if
  79.  
  80.  
  81. if (!digitalRead(buttonPin)) {
  82. setSystemState(2);
  83. } //end if
  84. break;
  85. case 2:
  86. if (!digitalRead(buttonPin)) {
  87. setSystemState(1);
  88.  
  89. } //end if
  90. break;
  91. default:
  92. break;
  93.  
  94.  
  95. } //end switch
  96.  
  97.  
  98.  
  99. }//endloop
  100.  
  101. void setSystemState(int newState) {
  102. switch (newState) {
  103. case 0:
  104. display.setCursor(0, 0);
  105. display.println("Press");
  106. display.display();
  107. delay(100); // delay to avoid overloading the serial port buffer
  108. display.clearDisplay();
  109. break;
  110. case 1:
  111. objA = random(1, 9);
  112. objB = random(1, 9);
  113. answer = objA * objB;
  114. counter = 0;
  115. display.setCursor(0, 0);
  116. display.println(String(objA) + "X" + String(objB));
  117. display.display();
  118. delay(100); // delay to avoid overloading the serial port buffer
  119. display.clearDisplay();
  120. delay(2000);
  121. break;
  122. case 2:
  123. display.setCursor(0, 0);
  124. if (counter == answer) {
  125. display.setCursor(0, 0);
  126. display.println(String(counter) + " V");
  127. } else {
  128. display.println(String(counter) + " W " + String(answer));
  129. }//end if
  130.  
  131. display.display();
  132. delay(100); // delay to avoid overloading the serial port buffer
  133. display.clearDisplay();
  134. delay(2000);
  135. default:
  136. break;
  137.  
  138. }// end switch
  139. systemState = newState;
  140. }//end setSystemState
Add Comment
Please, Sign In to add comment