Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <EEPROM.h>
- #include <LiquidCrystal.h>
- int PUL = 2;
- int interruptPin = 3; // Пин для прерывания
- volatile unsigned int coinCount = 0; // Счётчик монет
- unsigned long previousMicros = 0; // Переменная для хранения времени последнего изменения состояния
- unsigned int highDuration = 50; // Продолжительность высокого уровня в микросекундах
- unsigned int lowDuration = 1000; // Продолжительность низкого уровня в микросекундах
- unsigned int lastRPM = 0; // Последнее отображённое значение RPM
- unsigned int targetRPM = 60; // Целевое количество оборотов в минуту
- unsigned int lastCoinCount = 0; // Последнее отображённое значение coinCount
- bool isHigh = false; // Текущее состояние сигнала
- LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // Настройка пинов для 1602 LCD
- int lcdKey = 0;
- int lastKey = -1;
- const int keyPin = A0;
- const int eepromAddress = 0; // Адрес для сохранения targetRPM
- void setup() {
- pinMode(PUL, OUTPUT);
- pinMode(interruptPin, INPUT_PULLUP);
- attachInterrupt(digitalPinToInterrupt(interruptPin), countCoins, FALLING);
- lcd.begin(16, 2);
- // Загружаем значение targetRPM из EEPROM
- EEPROM.get(eepromAddress, targetRPM);
- if (targetRPM < 2 || targetRPM > 500) { // Проверка диапазона
- targetRPM = 60; // Значение по умолчанию
- }
- // Рассчитываем начальное значение lowDuration
- calculateLowDuration();
- lastRPM = targetRPM;
- lastCoinCount = coinCount;
- // Отображение начальных данных на экране
- updateLCD();
- }
- void loop() {
- unsigned long currentMicros = micros();
- // Определяем, нужно ли переключить состояние
- if (isHigh && (currentMicros - previousMicros >= highDuration)) {
- digitalWrite(PUL, LOW);
- isHigh = false;
- previousMicros = currentMicros;
- } else if (!isHigh && (currentMicros - previousMicros >= lowDuration)) {
- digitalWrite(PUL, HIGH);
- isHigh = true;
- previousMicros = currentMicros;
- }
- // Обработка кнопок на LCD Shield
- lcdKey = readLCDButtons();
- if (lcdKey != lastKey) {
- lastKey = lcdKey;
- handleLCDMenu(lcdKey);
- }
- // Обновление экрана только при изменении данных
- if (targetRPM != lastRPM || coinCount != lastCoinCount) {
- updateLCD();
- lastRPM = targetRPM;
- lastCoinCount = coinCount;
- }
- }
- int readLCDButtons() {
- int adcKeyValue = analogRead(keyPin);
- if (adcKeyValue < 50) return 0; // RIGHT
- if (adcKeyValue < 195) return 1; // UP
- if (adcKeyValue < 380) return 2; // DOWN
- if (adcKeyValue < 555) return 3; // LEFT
- if (adcKeyValue < 790) return 4; // SELECT
- return -1; // НИКАКАЯ
- }
- void handleLCDMenu(int key) {
- switch (key) {
- case 1: // UP
- targetRPM += 2;
- if (targetRPM > 500) targetRPM = 500; // Ограничиваем максимальное значение
- calculateLowDuration();
- break;
- case 2: // DOWN
- if (targetRPM > 2) {
- targetRPM -= 2;
- calculateLowDuration();
- }
- break;
- case 4: // SELECT
- // Сохраняем значение targetRPM в памяти
- EEPROM.put(eepromAddress, targetRPM);
- break;
- }
- }
- void calculateLowDuration() {
- float stepsPerRevolution = 800; //360.0 / 0.45; // Шагов на полный оборот
- lowDuration = 1000000.0 / (targetRPM * stepsPerRevolution / 60.0);
- }
- void updateLCD() {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Coins:");
- lcd.setCursor(7, 0);
- lcd.print(coinCount);
- lcd.setCursor(0, 1);
- lcd.print("RPM:");
- lcd.setCursor(5, 1);
- lcd.print(targetRPM);
- }
- void countCoins() {
- coinCount++;
- }
Advertisement
Add Comment
Please, Sign In to add comment