#include #include #include #include #define OLED_RESET 4 Adafruit_SSD1306 display(OLED_RESET); // these constants won't change: const int ledPin = 13; // LED connected to digital pin 13 const int knockSensor = A0; // the piezo is connected to analog pin 0 const int threshold = 100; // threshold value to decide when the detected sound is a knock or not // these variables will change: int sensorReading = 0; // variable to store the value read from the sensor pin int ledState = LOW; // variable used to store the last LED status, to toggle the light int buttonPin = 12; int counter = 0; int systemState; byte objA; byte objB; byte answer; void setup() { pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT pinMode(buttonPin, INPUT_PULLUP); Serial.begin(115200); // use the serial port randomSeed(analogRead(0) + analogRead(1) + analogRead(2) + analogRead(3) + analogRead(4) + analogRead(5)); // by default, we'll generate the high voltage from the 3.3v line internally! (neat!) display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32) // init done // Show image buffer on the display hardware. // Since the buffer is intialized with an Adafruit splashscreen // internally, this will display the splashscreen. display.display(); delay(2000); // Clear the buffer. display.clearDisplay(); display.setTextSize(3); display.setTextColor(WHITE); setSystemState(0); } void loop() { switch (systemState) { case 0: if (!digitalRead(buttonPin)) { setSystemState(1); } //end if break; case 1: // read the sensor and store it in the variable sensorReading: sensorReading = analogRead(knockSensor); // if the sensor reading is greater than the threshold: if (sensorReading >= threshold) { // toggle the status of the ledPin: ledState = !ledState; // update the LED pin itself: digitalWrite(ledPin, ledState); // send the string "Knock!" back to the computer, followed by newline Serial.println("Knock!"); counter++; display.setCursor(0, 0); display.println(counter); display.display(); delay(100); // delay to avoid overloading the serial port buffer display.clearDisplay(); } // end if if (!digitalRead(buttonPin)) { setSystemState(2); } //end if break; case 2: if (!digitalRead(buttonPin)) { setSystemState(1); } //end if break; default: break; } //end switch }//endloop void setSystemState(int newState) { switch (newState) { case 0: display.setCursor(0, 0); display.println("Press"); display.display(); delay(100); // delay to avoid overloading the serial port buffer display.clearDisplay(); break; case 1: objA = random(1, 9); objB = random(1, 9); answer = objA * objB; counter = 0; display.setCursor(0, 0); display.println(String(objA) + "X" + String(objB)); display.display(); delay(100); // delay to avoid overloading the serial port buffer display.clearDisplay(); delay(2000); break; case 2: display.setCursor(0, 0); if (counter == answer) { display.setCursor(0, 0); display.println(String(counter) + " V"); } else { display.println(String(counter) + " W " + String(answer)); }//end if display.display(); delay(100); // delay to avoid overloading the serial port buffer display.clearDisplay(); delay(2000); default: break; }// end switch systemState = newState; }//end setSystemState