1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| #include <U8g2lib.h> U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, SCL, SDA); int gear = 0; int analogMax = 1008,analogMin = 14; int R_I=220; void setup() { Serial.begin(9600); pinMode(A0, INPUT_PULLUP); pinMode(2, INPUT_PULLUP); u8g2.begin(); } void loop() { double a = Voltage_detect(); switch1(); displayMenu(a); int analogdate = analogRead(A0); } void switch1() { int key = digitalRead(2); if (key == 0) { delay(20); if (key == 0) { gear++; if (gear > 3) { gear = 0; } } while (!digitalRead(2)) { } } } void displayMenu(double a) { u8g2.clearBuffer(); u8g2.setFont(u8g2_font_ncenB08_tr); u8g2.setCursor(0, 14); switch (gear) { case 0: face(); break; case 1: u8g2.print("Voltage"); u8g2.setCursor(20, 34); u8g2.println(a); break; case 2: u8g2.print("Current"); break; case 3: u8g2.print("Resistance"); break; } u8g2.sendBuffer(); } void face() { u8g2.clearBuffer(); u8g2.drawCircle(56,40,8,U8G2_DRAW_LOWER_LEFT); u8g2.drawCircle(56,40,8,U8G2_DRAW_LOWER_RIGHT); u8g2.drawCircle(72,40,8,U8G2_DRAW_LOWER_LEFT); u8g2.drawCircle(72,40,8,U8G2_DRAW_LOWER_RIGHT); u8g2.drawCircle(56,41,8,U8G2_DRAW_LOWER_LEFT); u8g2.drawCircle(56,41,8,U8G2_DRAW_LOWER_RIGHT); u8g2.drawCircle(72,41,8,U8G2_DRAW_LOWER_LEFT); u8g2.drawCircle(72,41,8,U8G2_DRAW_LOWER_RIGHT); u8g2.drawLine(40,18,20,30); u8g2.drawLine(88,18,108,30); u8g2.drawLine(40,17,20,29); u8g2.drawLine(88,17,108,29); u8g2.sendBuffer(); } float Voltage_detect(){ int analogdate = analogRead(A0); float Voltage = analogdate*5.0/1023.0; if(Voltage>=4.92){ Serial.println("out"); } else Serial.println(Voltage); return Voltage; }
|