Arduino + DMSG-020 = Blow
// C++ , Arduino , DMSG-020 , SSD1306 , Blow
#include <EEPROM.h> const int EEPROM_ADDR = 0; // Строка должна быть ТУТ, до setup! bool License = false; // И эта строка тоже тут int runCount = 0; // ШАГ 1: Перенесли переменную сюда и сделали тип int, чтобы вмещалось до 9999 #include <SPI.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define RELAY_PIN A0 #define Sensor_PIN A1 // Коричневый провод датчика #define Sensor_GND A2 // Синий провод датчика (делаем из него минус) #define switch_Delay_1 2 #define switch_Delay_2 3 #define switch_Delay_3 4 #define switch_Delay_4 5 #define switch_Delay_5 6 #define switch_Blow_1 7 #define switch_Blow_2 8 #define switch_Blow_3 9 #define switch_Blow_4 10 #define switch_Blow_5 11 #define switch_ON 12 bool RePlay = false; bool Sensor = false; bool Relay = false; float Delay = 0.0; float Blow = 0.0; // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) // The pins for I2C are defined by the Wire-library. // On an arduino UNO: A4(SDA), A5(SCL) // On an arduino MEGA 2560: 20(SDA), 21(SCL) // On an arduino LEONARDO: 2(SDA), 3(SCL), ... //#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 Adafruit_SSD1306 display(128, 64, &Wire, -1); byte displayAddress = 0x00; void setup() { delay(1000); Serial.begin(9600); Wire.begin(); Wire.setWireTimeout(3000, true); // Защита от зависания шины (таймаут 3 мс) delay(1000); EEPROM.get(EEPROM_ADDR, runCount); if (runCount > 0) { License = true; runCount--; EEPROM.put(EEPROM_ADDR, runCount); Serial.println("EEPROM=" + String(runCount)); } else { License = false; Serial.println("EEPROM=" + String(runCount)); } bool displayFound = false; // Флаг: найден ли экран displayAddress = 0x00; // Сюда запишем найденный адрес for (byte addr = 1; addr < 127; addr++) { Wire.beginTransmission(addr); byte error = Wire.endTransmission(); if (error == 0) { displayFound = true; // Отмечаем, что экран найден displayAddress = addr; // Запоминаем адрес устройства Serial.println("i2c : 0x" + String(displayAddress, HEX)); break; // ПРЕРЫВАЕМ ЦИКЛ, так как экран нашли! } } if (displayAddress == 0x00) { Serial.println("i2c : 0x" + String(displayAddress, HEX)); } else { if(!display.begin(SSD1306_SWITCHCAPVCC, displayAddress)) { Serial.println(F("Display allocation failed, Out of RAM")); displayAddress = 0x00; // Сбрасываем в 0, чтобы код знал, что экрана нет } else { display.clearDisplay(); //display.drawPixel(0, 0, SSD1306_WHITE); display.setTextSize(1); // Размер шрифта: 1 — мелкий (высота 8 пикселей), 2 — крупнее и т.д. display.setTextColor(SSD1306_WHITE); // Цвет текста (белый) display.setCursor(0, 0); // Самый верхний левый угол display.println("EEPROM=" + String(runCount)); display.display(); delay(1000); } } digitalWrite(RELAY_PIN, HIGH); pinMode(RELAY_PIN, OUTPUT); pinMode(Sensor_GND, OUTPUT); // 1. Делаем из пина А2 искусственный GND (минус) digitalWrite(Sensor_GND, LOW); // На пине А2 теперь жесткий ноль (Земля) pinMode(Sensor_PIN, INPUT_PULLUP); // 2. Включаем пин А1 на вход с безопасной внутренней подтяжкой pinMode(switch_Delay_1, INPUT_PULLUP); pinMode(switch_Delay_2, INPUT_PULLUP); pinMode(switch_Delay_3, INPUT_PULLUP); pinMode(switch_Delay_4, INPUT_PULLUP); pinMode(switch_Delay_5, INPUT_PULLUP); pinMode(switch_Blow_1, INPUT_PULLUP); pinMode(switch_Blow_2, INPUT_PULLUP); pinMode(switch_Blow_3, INPUT_PULLUP); pinMode(switch_Blow_4, INPUT_PULLUP); pinMode(switch_Blow_5, INPUT_PULLUP); pinMode(switch_ON, INPUT_PULLUP); } void loop() { drawMenu(); if (Serial.available() > 0) { String input = Serial.readStringUntil('\n'); input.trim(); if (input.startsWith("eeprom=")) { int value = input.substring(7).toInt(); if (value >= 0 && value <= 32767) // -32768 32767 { EEPROM.put(EEPROM_ADDR, value); // Запись двухбайтового числа в память runCount = value; // Обновляем текущую переменную, чтобы она применилась сразу License = (value > 0); // Активируем лицензию, если введено число больше 0 Serial.println("EEPROM=" + String(value)); delay(1000); ((void (*)())(0))(); } else { Serial.println("Ошибка: Число должно быть от 0 до 9999"); } } } if (analogRead(Sensor_PIN) < 700) { if (Sensor == false) Sensor = true; // Serial.print(sensorValue); } else { if (Sensor == true) Sensor = false; // Serial.print(sensorValue); } drawMenu(); Delay = 0.0; if (digitalRead(switch_Delay_1) == LOW) Delay += 0.1; if (digitalRead(switch_Delay_2) == LOW) Delay += 0.2; if (digitalRead(switch_Delay_3) == LOW) Delay += 0.3; if (digitalRead(switch_Delay_4) == LOW) Delay += 0.4; if (digitalRead(switch_Delay_5) == LOW) Delay += 1.0; Blow = 0.0; if (digitalRead(switch_Blow_1) == LOW) Blow += 0.1; if (digitalRead(switch_Blow_2) == LOW) Blow += 0.2; if (digitalRead(switch_Blow_3) == LOW) Blow += 0.3; if (digitalRead(switch_Blow_4) == LOW) Blow += 0.4; if (digitalRead(switch_Blow_5) == LOW) Blow += 1.0; drawMenu(); if (Sensor == false) RePlay = false; if (digitalRead(switch_ON) == LOW && Sensor == true && RePlay == false && License == true) { RePlay = true; if (displayAddress != 0x00) display.invertDisplay(true); delay((unsigned long)(Delay * 1000)); digitalWrite(RELAY_PIN, LOW); // ВКЛЮЧИТЬ реле (подать 5V) Relay = true; drawMenu(); delay((unsigned long)(Blow * 1000)); digitalWrite(RELAY_PIN, HIGH); // ВЫКЛЮЧИТЬ реле (подать 0V) if (displayAddress != 0x00) display.invertDisplay(false); Relay = false; } drawMenu(); } void drawMenu() { if (displayAddress == 0x00) return; display.clearDisplay(); display.setTextSize(2); // Размер шрифта: 1 — мелкий (высота 8 пикселей), 2 — крупнее и т.д. display.setTextColor(SSD1306_WHITE); // Цвет текста (белый) display.setCursor(0, 0); // Самый верхний левый угол display.print("Sensor "); if (Sensor == true) display.println(" ON"); else display.println("OFF"); display.print("Relay "); if (Relay == true) display.println(" ON"); else (display.println("OFF")); display.println("Delay " + String(Delay, 1)); display.println("Blow " + String(Blow, 1)); display.display(); }