LCD RTC Alarm Clock Toggle Alarm Not Working

#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
RTC_DS1307 RTC;
#define buz 11
const int buzzer = 11;

int sharkLight = A2;
int button = 12;

int buttonState = 0;
int OnOff = 1;

void setup () {
    Serial.begin(9600);
    Wire.begin();
    RTC.begin();
    lcd.begin(16, 2);
    
    pinMode(button, INPUT);
    pinMode(buzzer, OUTPUT);
    pinMode(sharkLight, OUTPUT);
    
  if (! RTC.isrunning()) {
   Serial.println("RTC is NOT running!");
  }
}

void loop () {
    DateTime now = RTC.now();
    lcd.setCursor(0, 0);
    lcd.print(now.month(), DEC);
    lcd.print('/');
    lcd.print(now.day(), DEC);
    lcd.print('/');
    lcd.print(now.year(), DEC);
    lcd.print(' ');
    lcd.setCursor(0, 2);
     if (now.hour()<10)
    lcd.print('0');
    lcd.print(now.hour(), DEC);
    lcd.print(':');
     if (now.minute()<10)
    lcd.print('0');
    lcd.print(now.minute(), DEC);
    lcd.print(':');
    if (now.second()<10)
    lcd.print('0');
    lcd.print(now.second(), DEC);
    lcd.setCursor(12, 0);
      ///////
    buttonState = digitalRead(button);
  
    if(buttonState == HIGH)
    {
      if (OnOff == 1)
      {
       OnOff == 0;
      }
      else
      {
       OnOff == 1;
      }
    }
    ///////
    if (OnOff == 1) {
      lcd.setCursor(14,2);
      lcd.print("ON");
      
      if (now.hour() == 6 && now.minute() == 0){
        Buzz();
      }
      
      if (now.hour() >= 22) {
        digitalWrite(sharkLight, HIGH);
      }
      else if (now.hour() <= 5) {
        digitalWrite(sharkLight, HIGH);
      }
      else digitalWrite(sharkLight, LOW);
    }
    else if (OnOff == 0) {
      lcd.setCursor(13,2);
      lcd.print("OFF");
    }
}

void alarmButton() {
  
}

void Buzz() {
    tone(buzzer, 1000); // Send 1KHz sound signal...
    delay(500);        // ...for 1 sec
    noTone(buzzer);     // Stop sound...
    delay(500);        // ...for 1sec
  }

 void printTime() {
  int dayofweek;
   switch(dayofweek){
     case 1: 
     lcd.print("Mon");
     break;
     case 2:
     lcd.print("Tue");
     break;
     case 3:
     lcd.print("Wed");
     break;
     case 4:
     lcd.print("Thu");
     break;
     case 5:
     lcd.print("Fri");
     break;
     case 6:
     lcd.print("Sat");
     break;
     case 0:
     lcd.print("Sun");
     break;
    delay(1000);

   }
}`Preformatted text`

I forgot to mention, the problem is that OnOff can't be changed, and stays as whatever I set it to when I make it. I've tested the button and it works fine, so I know it's a programming error.

It would be useful to know how everything is connected, and what it is supposed to do, and what it actually does.

What does that mean?

== :thinking:

1 Like

I just figured that out myself, thanks. Spent an hour and a half on it, only to realize I had "==" instead of "=".

OnOff is a boolean, so when I pressed the button it should've been changing the boolean to true or false with every press, but as @LarryD discovered, I had == when it should've been =.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.