#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(10,8); #define pinSW 9 int menu = 1; int pos = 0; void setup() { U8G2.begin(); initArrays(); pinMode (pinSW,INPUT); encoder.setPosition(1); Serial.begin (2000000); updateMenu(); } void loop() { updateEncoder(); } void updateEncoder(){ encoder.tick(); menu = encoder.getPosition(); if(pos != menu){ Serial.print("pos:"); Serial.print(menu); Serial.print(" dir:"); Serial.println((int)(encoder.getDirection())); pos = menu; updateMenu(); } if(!digitalRead(pinSW)){ executeAction(); Serial.println("button pressed"); delay(200); updateMenu(); } } void graph(){ U8G2.drawVLine(0, 0, 64); // draw Y-axis U8G2.drawHLine(0, 63, 128); // draw X-axis if(!digitalRead(pinSW)){ menu = 0; updateMenu(); } 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] = values[119]; } else { buffer[x] = values[x-1]; } } } 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 S1"); 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 S1"); U8G2.sendBuffer(); break; case 3: U8G2.clearDisplay(); U8G2.setFont(u8g2_font_ncenB14_tr); U8G2.setCursor(0, 25); U8G2.print(">Calibrate S2"); U8G2.setCursor(0, 55); U8G2.print(" Calibrate S3"); U8G2.sendBuffer(); break; case 4: U8G2.clearDisplay(); U8G2.setFont(u8g2_font_ncenB14_tr); U8G2.setCursor(0, 25); U8G2.print(" Calibrate S2"); U8G2.setCursor(0, 55); U8G2.print(">Calibrate S3"); 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)){ U8G2.setFont(u8g2_font_ncenB14_tr); U8G2.setCursor(0, 25); U8G2.print("Calibration"); U8G2.setCursor(0, 55); U8G2.print("Sensor 1"); U8G2.sendBuffer(); } break; case 3: U8G2.clearDisplay(); delay(500); while(digitalRead(pinSW)){ U8G2.setFont(u8g2_font_ncenB14_tr); U8G2.setCursor(0, 25); U8G2.print("Calibration"); U8G2.setCursor(0, 55); U8G2.print("Sensor 2"); U8G2.sendBuffer(); } 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; } }