ARDUINO Реле автоматической подачи школьного звонка.

Реле автоматической подачи школьного звонка.
Доброй ночи, дорогие форумчане!
Еще 3 недели назад пришла идеальная идея в голову: т.к. я заканчиваю 11 класс, решил подарить школе "умную систему".
В Arduino ничего не соображал, собрал схему на nano и часах реального времени с реле. Начал пробывать писать код, кое в чем разобрался.
Всё заработало! Браво!
Думаю, так можно добавить туда ещё и сокращённое расписание. Поставил тумблер, разобрался в switch case .
И у меня уже работает реле для школы. Зачем время тянуть? На выходных его подсоединил, все в восторге.
Понедельник.. Первый день новых технологий. Первый, второй, третий, четвёртый звонки подавались сами, я был доволен.
(Раньше надо было спускаться на 1ый этаж и нажимать выключатель, чтобы подать звонок)
Хоп! Пятого звонка не прозвенело. Я пулей побежал на первый этаж, подал звонок, отключил и включил на Arduino
o питание и следующие 4 звонка опять прозвенели, а пятого не прозвучало.. Решил убрать switch case (расписание сокращённых уроков удалил). На следующий день проверил, опять такая же ерунда! В общем уже 2 недели я в школе страдаю фигнёй, пытаюсь исправить код, уже просто устал. Что только не пробывал.
Мне написали другой, якобы "лучший код", но он так же не работает.. Дорогие форумчане, я у Вас прошу помощи, что может быть не так?
Мой код, который работает, но ардуино с ним засыпает.:
#include <RTClib.h>
#include <Time.h>
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int relay = 2;      //выход на реле
#define   switchPin 3
uint8_t   switchPinState;

enum switchProg : uint8_t {
  PROG_ONE = 0,           // Если переключатель в положении GND
  PROG_TWO = 1            // Если переключатель в положении VCC
};
void setup()
{
  Serial.begin(9600);
  pinMode(relay, OUTPUT); //выход на реле
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(switchPin, INPUT);
  digitalWrite(relay, HIGH);
  if (rtc.begin()){
  Serial.println("Couldn't find RTC");

  }}
void loop()
{
    DateTime now = rtc.now();

    Serial.println("Current Date & Time: ");
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
delay(1000);

switchPinState = digitalRead(switchPin);

  switch (switchPinState) {
    case switchProg::PROG_ONE:

digitalWrite(4, HIGH);
digitalWrite(5, LOW);

if (now.hour() == 8 && now.minute() == 30 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (relay, LOW);
}
if (now.hour() == 8 && now.minute() == 30 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (relay, HIGH);
}
if (now.hour() == 9 && now.minute() == 15 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 9 && now.minute() == 15 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}
if (now.hour() == 9 && now.minute() == 25 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 9 && now.minute() == 25 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}
if (now.hour() == 10 && now.minute() == 10 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 10 && now.minute() == 10 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}
if (now.hour() == 10 && now.minute() == 30 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 10 && now.minute() == 30 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}
if (now.hour() == 11 && now.minute() == 15 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 11 && now.minute() == 15 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}
if (now.hour() == 11 && now.minute() == 35 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 11 && now.minute() == 35 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}
if (now.hour() == 12 && now.minute() == 20 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 12 && now.minute() == 20 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}
if (now.hour() == 12 && now.minute() == 30 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 12 && now.minute() == 30 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}
if (now.hour() == 13 && now.minute() == 15 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 13 && now.minute() == 15 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}
if (now.hour() == 13 && now.minute() == 25 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 13 && now.minute() == 25 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}
if (now.hour() == 14 && now.minute() == 10 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 14 && now.minute() == 10 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}
if (now.hour() == 14 && now.minute() == 20 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 14 && now.minute() == 20 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}
if (now.hour() == 15 && now.minute() == 05 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 15 && now.minute() == 05 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}
if (now.hour() == 15 && now.minute() == 10 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 15 && now.minute() == 10 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}
if (now.hour() == 15 && now.minute() == 55 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 15 && now.minute() == 55 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}

break;

   
case switchProg::PROG_TWO:

digitalWrite(5, HIGH);
digitalWrite(4, LOW);

if (now.hour() == 8 && now.minute() == 30 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (relay, LOW);
}
if (now.hour() == 8 && now.minute() == 30 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (relay, HIGH);
}
if (now.hour() == 9 && now.minute() == 10 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 9 && now.minute() == 10 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}
if (now.hour() == 9 && now.minute() == 45 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 9 && now.minute() == 45 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}
if (now.hour() == 10 && now.minute() == 00 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 10 && now.minute() == 00 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}
if (now.hour() == 10 && now.minute() == 35 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 10 && now.minute() == 35 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}
if (now.hour() == 10 && now.minute() == 50 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 10 && now.minute() == 50 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}
if (now.hour() == 11 && now.minute() == 25 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 11 && now.minute() == 25 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}
if (now.hour() == 11 && now.minute() == 30 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 11 && now.minute() == 30 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}
if (now.hour() == 12 && now.minute() == 05 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 12 && now.minute() == 05 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}
if (now.hour() == 12 && now.minute() == 10 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 12 && now.minute() == 10 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}
if (now.hour() == 12 && now.minute() == 45 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 12 && now.minute() == 45 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}
if (now.hour() == 12 && now.minute() == 50 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 12&& now.minute() == 50 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}
if (now.hour() == 13 && now.minute() == 25 && now.second() == 00 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, LOW);
}
if (now.hour() == 13 && now.minute() == 25 && now.second() == 07 && (now.dayOfTheWeek() != 6 & now.dayOfTheWeek() != 0))
{
digitalWrite (2, HIGH);
}
      break;
     default:
      break;
  }        }
1713123632535.png
Код, который мне написали, но он не работает.:
#include <RTClib.h>       // https://github.com/adafruit/RTClib

#define   relePin   2
#define   ON        LOW
#define   OFF       HIGH
#define   LED       13

#define   SUNDAY    0
#define   SATURDAY  6

RTC_DS3231 rtc;

char daysOfTheWeek[7][10] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

DateTime now = rtc.now();

uint32_t  prevMillis;

void setup() {

  pinMode(relePin, OUTPUT);       // выход на реле
  pinMode(LED, OUTPUT);           // Встроенный светодиод, например на Nano имеется...
 
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  digitalWrite(4, HIGH);

  digitalWrite(relePin, HIGH);
  digitalWrite(LED,     LOW);

  Serial.begin(9600);             // Скорость Serial-порта

  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1) {                               // Бесконечный цикл, так как RTC не найден.
      digitalWrite(LED, !digitalRead(LED));   // И мигаем при этом встроенным светодиодом, сообщая о проблеме...
      delay(1000);
    }
  }
  prevMillis = millis();
}

void loop() {

  uint16_t  m_year          = now.year();
  uint8_t   m_month         = now.month();
  uint8_t   m_day           = now.day();
  uint8_t   m_dayOfTheWeek  = now.dayOfTheWeek();
  uint8_t   m_hour          = now.hour();
  uint8_t   m_minute        = now.minute();
  int8_t    m_second        = now.second();

  if (millis() - prevMillis >= 1000) {
    prevMillis = millis();
    Serial.println("Current Date & Time: ");
    Serial.print(m_year,  DEC);
    Serial.print('/');
    Serial.print(m_month,  DEC);
    Serial.print('/');
    Serial.print(m_day,    DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[m_dayOfTheWeek]);
    Serial.print(") ");
    Serial.print(m_hour,   DEC);
    Serial.print(':');
    Serial.print(m_minute, DEC);
    Serial.print(':');
    Serial.print(m_second, DEC);
    Serial.println();
  }
 digitalWrite(5, HIGH);
  if (m_dayOfTheWeek != SUNDAY && m_dayOfTheWeek != SATURDAY) {  // Если сегодня будний день (Пн...Пт), то...
    
    if (m_hour == 8 && m_minute == 30) {
      if (m_second <= 7) {
        digitalWrite (relePin, ON);
      } else {
        digitalWrite (relePin, OFF);
      }
    }

    if (m_hour == 9 && m_minute == 15) {
      if (m_second <= 7) {
        digitalWrite (relePin, ON);
      } else {
        digitalWrite (relePin, OFF);
      }
    }

    if (m_hour == 9 && m_minute == 25) {
      if (m_second <= 7) {
        digitalWrite (relePin, ON);
      } else {
        digitalWrite (relePin, OFF);
      }
    }

    if (m_hour == 10 && m_minute == 10) {
      if (m_second <= 7) {
        digitalWrite (relePin, ON);
      } else {
        digitalWrite (relePin, OFF);
      }
    }

    if (m_hour == 10 && m_minute == 30) {
      if (m_second <= 7) {
        digitalWrite (relePin, ON);
      } else {
        digitalWrite (relePin, OFF);
      }
    }
  }
}
 

Комментарии

vortigont

★★★★★★✩
24 Апр 2020
1,022
542
Saint-Petersburg, Russia
упрощать можно, но не хочется
упрощать не нужно, улучшать нужно :) Не думал что тема автоматизации школьных звонков настолько актуальная. И до сих пор не нашлось какой-нибудь конторы, котороая делала бы эти решения "под ключ" на каком-нибудь ПЛК типа простенького Овена.
Пройдитесь по этой теме, может почерпнёте для себе где и как можно оптимизировать код. Например убрать эту развесистую клюкву из ИФов.
 

Alexhhhhgf

✩✩✩✩✩✩✩
14 Апр 2024
42
0
Спасибо всем за подсказки, думаю, придётся попробовать с esp.
P.S вместо реле ставил светодиод, зависает схема, так же бывало пару раз зависание и звонок звенел, не выключался. Попробую отсоединить от часов data и clc.
 

Max965

✩✩✩✩✩✩✩
28 Окт 2023
16
2
@Alexhhhhgf, из личного опыта - единичный экземпляр Nano периодически зависал, просто заменил плату и всё отлично. Качество китайских дешёвых вещей бывает выборочным.
 
  • Лойс +1
Реакции: Alexhhhhgf