#include #include #include #include U8G2_SH1106_128X64_NONAME_F_HW_I2C U8G2(U8G2_R0, 5, 4, U8X8_PIN_NONE); int values[120]; // array for sine values int buffer[120]; // buffer array used to shift sine values void initArrays(void){ // fill arrays for (int i = 0; i < 120; i++){ float angle = i * 3; // 3 * 120 = 360 values[i] = (32 + (sin(angle * (M_PI / 180)) * 30)); // values shifted upwards by 32 for easier visualization buffer[i] = values[i]; // initialize buffer with the same values } } RotaryEncoder encoder(D9,D8); #define pinSW D7 int menu = 1; int pos = 0; int16_t value; int16_t sens1 = 0; int16_t sens2 = 0; int16_t sens3 = 0; int min_s1 = 530; int max_s1 = 240; int min_s2 = 530; int max_s2 = 240; int min_s3 = 530; int max_s3 = 240; void setup() { Wire.begin(); U8G2.begin(); initArrays(); pinMode (pinSW,INPUT); pinMode (D8,INPUT); pinMode (D9,INPUT); encoder.setPosition(1); Serial.begin(9600); updateMenu(); } void loop() { updateEncoder(); executeSerial(); } void updateEncoder(){ encoder.tick(); menu = encoder.getPosition(); if(pos != menu){ pos = menu; updateMenu(); } if(!digitalRead(pinSW)){ executeAction(); delay(200); updateMenu(); } } int getValue(int sens){ byte a,b; Wire.requestFrom(sens, 2); a = Wire.read(); b = Wire.read(); value = a; value = value << 8 | b; return value; } void graph(){ U8G2.drawVLine(0, 0, 64); // draw Y-axis U8G2.drawHLine(0, 63, 128); // draw X-axis if(!digitalRead(pinSW)){ menu = 0; updateMenu(); } executeSerial(); sens1 = map(getValue(2),240,550,60,0); for (int j = 0 ; j < 120; j++){ values[j] = buffer[j]; // transfer values from buffer to output array U8G2.drawPixel(j+4, values[j]); // draw the sine wave with values from the array shifted 4 pixels to the right } U8G2.sendBuffer(); U8G2.clearBuffer(); for (int x = 0 ; x < 120; x++){ // loop array via buffer if(x-1 < 0){ buffer[x] = sens1; } else { buffer[x] = values[x-1]; } } } void displaySensors() { if(!digitalRead(pinSW)){ menu = 0; updateMenu(); } executeSerial(); U8G2.setFont(u8g2_font_t0_11_tr); U8G2.setCursor(0, 20); U8G2.print("Sensor 1: "); U8G2.setCursor(64, 20); U8G2.print(getValue(2)); U8G2.setCursor(0, 40); U8G2.print("Sensor 2: "); U8G2.setCursor(64, 40); U8G2.print(getValue(3)); U8G2.setCursor(0, 60); U8G2.print("Sensor 3: "); U8G2.setCursor(64, 60); U8G2.print(getValue(4)); U8G2.sendBuffer(); U8G2.clearBuffer(); } void executeSerial(){ char c = Serial.read(); if(c == 'g') { menu = 1; updateMenu(); executeAction(); } if(c == 'c') { menu = 2; executeAction(); } if(c == 'v') { menu = 3; executeAction(); } if(c == 'm') { menu = 0; updateMenu(); } } void calibrate() { if(!digitalRead(pinSW)){ menu = 0; updateMenu(); } executeSerial(); do { U8G2.setFont(u8g2_font_t0_11_tr); U8G2.setFontPosCenter(); U8G2.setCursor(10, 10); U8G2.print("CALIBRATE SENSOR 1"); U8G2.setFont(u8g2_font_t0_22_mf); U8G2.setFontPosBaseline(); U8G2.drawButtonUTF8(15, 50, U8G2_BTN_INV, 0, 2, 2, "MIN" ); U8G2.setCursor(65, 50); U8G2.print(getValue(2)); U8G2.sendBuffer(); U8G2.clearBuffer(); executeSerial(); } while(digitalRead(pinSW)); min_s1 = getValue(2); delay(500); executeSerial(); do{ U8G2.setFont(u8g2_font_t0_11_tr); U8G2.setFontPosCenter(); U8G2.setCursor(10, 10); U8G2.print("CALIBRATE SENSOR 1"); U8G2.setFont(u8g2_font_t0_22_mf); U8G2.setFontPosBaseline(); U8G2.drawButtonUTF8(15, 50, U8G2_BTN_INV, 0, 2, 2, "MAX" ); U8G2.setCursor(65, 50); U8G2.print(getValue(2)); U8G2.sendBuffer(); U8G2.clearBuffer(); executeSerial(); } while(digitalRead(pinSW)); max_s1 = getValue(2); delay(500); executeSerial(); do { U8G2.setFont(u8g2_font_t0_11_tr); U8G2.setFontPosCenter(); U8G2.setCursor(10, 10); U8G2.print("CALIBRATE SENSOR 2"); U8G2.setFont(u8g2_font_t0_22_mf); U8G2.setFontPosBaseline(); U8G2.drawButtonUTF8(15, 50, U8G2_BTN_INV, 0, 2, 2, "MIN" ); U8G2.setCursor(65, 50); U8G2.print(getValue(3)); U8G2.sendBuffer(); U8G2.clearBuffer(); executeSerial(); } while(digitalRead(pinSW)); min_s2 = getValue(3); delay(500); executeSerial(); do{ U8G2.setFont(u8g2_font_t0_11_tr); U8G2.setFontPosCenter(); U8G2.setCursor(10, 10); U8G2.print("CALIBRATE SENSOR 2"); U8G2.setFont(u8g2_font_t0_22_mf); U8G2.setFontPosBaseline(); U8G2.drawButtonUTF8(15, 50, U8G2_BTN_INV, 0, 2, 2, "MAX" ); U8G2.setCursor(65, 50); U8G2.print(getValue(3)); U8G2.sendBuffer(); U8G2.clearBuffer(); executeSerial(); } while(digitalRead(pinSW)); max_s2 = getValue(3); delay(500); executeSerial(); do { U8G2.setFont(u8g2_font_t0_11_tr); U8G2.setFontPosCenter(); U8G2.setCursor(10, 10); U8G2.print("CALIBRATE SENSOR 3"); U8G2.setFont(u8g2_font_t0_22_mf); U8G2.setFontPosBaseline(); U8G2.drawButtonUTF8(15, 50, U8G2_BTN_INV, 0, 2, 2, "MIN" ); U8G2.setCursor(65, 50); U8G2.print(getValue(4)); U8G2.sendBuffer(); U8G2.clearBuffer(); executeSerial(); } while(digitalRead(pinSW)); min_s3 = getValue(4); delay(500); executeSerial(); do{ U8G2.setFont(u8g2_font_t0_11_tr); U8G2.setFontPosCenter(); U8G2.setCursor(10, 10); U8G2.print("CALIBRATE SENSOR 3"); U8G2.setFont(u8g2_font_t0_22_mf); U8G2.setFontPosBaseline(); U8G2.drawButtonUTF8(15, 50, U8G2_BTN_INV, 0, 2, 2, "MAX" ); U8G2.setCursor(65, 50); U8G2.print(getValue(4)); U8G2.sendBuffer(); U8G2.clearBuffer(); executeSerial(); } while(digitalRead(pinSW)); max_s3 = getValue(4); executeSerial(); } void updateMenu() { switch (menu) { case 0: delay(200); menu = 1; encoder.setPosition(1); break; case 1: U8G2.clearDisplay(); U8G2.setFont(u8g2_font_ncenB14_tr); U8G2.setCursor(0, 25); U8G2.print(">Graph"); U8G2.setCursor(0, 55); U8G2.print(" Calibrate"); U8G2.sendBuffer(); break; case 2: U8G2.clearDisplay(); U8G2.setFont(u8g2_font_ncenB14_tr); U8G2.setCursor(0, 25); U8G2.print(" Graph"); U8G2.setCursor(0, 55); U8G2.print(">Calibrate"); U8G2.sendBuffer(); break; case 3: U8G2.clearDisplay(); U8G2.setFont(u8g2_font_ncenB14_tr); U8G2.setCursor(0, 25); U8G2.print(">Display Sensors"); U8G2.setCursor(0, 55); U8G2.print(" Easteregg"); U8G2.sendBuffer(); break; case 4: U8G2.clearDisplay(); U8G2.setFont(u8g2_font_ncenB14_tr); U8G2.setCursor(0, 25); U8G2.print(" Display Sensors"); U8G2.setCursor(0, 55); U8G2.print(">Easteregg"); U8G2.sendBuffer(); break; case 5: menu = 4; encoder.setPosition(4); break; } } void executeAction() { switch (menu) { case 1: U8G2.clearDisplay(); delay(500); while(digitalRead(pinSW)) { graph(); } break; case 2: U8G2.clearDisplay(); delay(500); while(digitalRead(pinSW)){ calibrate(); } break; case 3: U8G2.clearDisplay(); delay(500); while(digitalRead(pinSW)){ displaySensors(); } break; case 4: U8G2.clearDisplay(); delay(500); while(digitalRead(pinSW)){ U8G2.setFont(u8g2_font_t0_11b_tr); U8G2.setCursor(0, 10); U8G2.print("Hollow Knight"); U8G2.setCursor(0, 36); U8G2.print("Silksong"); U8G2.setCursor(0, 62); U8G2.print("THIS YEAR!!!"); U8G2.sendBuffer(); } break; } }