Beginner Bool problem

Hi Guys,

I'm trying to make a simple controller for a terrarium. The program will run a daymode and a nightmode. in loop I want it to check a boolian to see if its day or night an then trigger the corresponding function.
I can’t get it to work though. The current version triggers neither day or nightmode. Not by the alarm, not with the button. My guess is im doing something wrong with the bool. So if you'd be so kind to have a look at my beautiful mess I’d be grateful!
You will find the if statements where I think the fault lies at the very bottom of the code.

#include <TimeLib.h>
#include <TimeAlarms.h>
#include <DS1307RTC.h>
// #include <Time.h>
#include <OneWire.h>
#include <DallasTemperature.h>
//#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

int LED = 13;             // testled, later relay
const int buttonPin = 9;  // jump start button
int buttonState = 0;      // buttonstate
int daghighTemp = 28;        // max temoperatuur
int daglowTemp = 23;         // min temperatuur
int nachthighTemp = 24;        // max temoperatuur
int nachtlowTemp = 19;         // min temperatuur
int currentTemp;          // laatste gemeten temperatuur

int relay1 = 1;   //lights en ventilatie
int relay2 = 2;   // sproeiers
int relay3 = 3;   // verwarming



bool dagState = false; 

AlarmId id;
#define ONE_WIRE_BUS 8    // temperatuur sensor op pin 8
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);



void printDigits(int digits) {
  Serial.print(":");
  if (digits < 10)
    display.print('0');
  display.print(digits);
}

void flip(){
  dagState = !dagState;
}

void dagMode(){
    Serial.println("dagmode aan");
    digitalWrite (relay1, HIGH);
      
  if (currentTemp > daghighTemp)
     digitalWrite (relay3, LOW);
   

  if (currentTemp < daglowTemp)
      digitalWrite (relay3, HIGH);
  
  if (currentTemp > daglowTemp and currentTemp < daghighTemp){
  Serial.println("ALARM: NICE AND SNUG");
 }
}

void nachtMode(){
    Serial.println("nachtmode aan");
    digitalWrite (relay1, LOW);

   if (currentTemp > nachthighTemp)
      digitalWrite (relay3, LOW);
   

  if (currentTemp < nachtlowTemp)
      digitalWrite (relay3, HIGH);
 
  if (currentTemp > daglowTemp and currentTemp < daghighTemp){
  Serial.println("ALARM: NICE AND SNUG");
 }
}

void sproeier(){
   Serial.println("sproeier aan");
   delay(45000);
   Serial.println("sproeier uit");
}

void setup() {

pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);

Serial.begin(9600); // debug mode on
while (!Serial);
    
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }

display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 10);
  // Display static text
  display.println("systems booting..");
display.display();
  delay(600);
     
sensors.begin(); // start sensor

    setSyncProvider(RTC.get);
  Alarm.alarmRepeat(9,30,0,flip);  // relais 1 - daglicht
  Alarm.alarmRepeat(20,34,0,flip);  

  Alarm.alarmRepeat(9,30,0,sproeier);  // relais 1 - daglicht
  Alarm.alarmRepeat(22,30,0,sproeier);  
   
  pinMode(LED, OUTPUT);       //pinmode led outpup
  pinMode(buttonPin, INPUT);  // pinmode button input
    
  setSyncProvider(RTC.get);
}

void loop() {
  // put your main code here, to run repeatedly:
tmElements_t tm;                //RTC
  RTC.read(tm);


display.clearDisplay();           //show time
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(5,8);
    printDigits(tm.Hour);
    display.write(':');
    printDigits(tm.Minute);
    display.setTextSize(1);
    display.setCursor(67,8);
     printDigits(tm.Second);
     
     display.setTextSize(2);
     display.setCursor(96,8);
     display.print(currentTemp);
     display.setTextSize(1);
     display.setCursor(120,4);
     display.print('o');
display.display();
     
 buttonState = digitalRead(buttonPin);  // jump-start Button
  if (buttonState == HIGH) {
  dagState = !dagState;
}

 sensors.requestTemperatures(); 
  currentTemp = (sensors.getTempCByIndex(0));
  
  Serial.print("Current Temperature: ");
  Serial.println(currentTemp);
  
if(dagState == true)
    dagMode();

if(dagState == false)
    nachtMode; 


  Alarm.delay(1000);
}

Thanks for respecting the forum guidelines and using code tags. You mean the function call with no parentheses?

if(dagState == true)
    dagMode();

if(dagState == false)
    nachtMode;

Also, are you familiar with the "principle of exclusion"? Put simply, it means that if something is true, it can't be false. So you can (should) use an 'else'.

if(dagState == true)
    dagMode();
    else
    nachtMode();
if(dagState == true)[color=#222222][/color]
    dagMode();[color=#222222][/color]
[color=#222222][/color]
if(dagState == false)[color=#222222][/color]
    nachtMode(); <<-- make sure you include the () when calling functions

Same here, you forgot the () on flip and spro...
Alarm.alarmRepeat(9,30,0,flip);  // relais 1 - daglicht[color=#222222][/color]
  Alarm.alarmRepeat(20,34,0,flip); [color=#222222][/color]
[color=#222222][/color]
  Alarm.alarmRepeat(9,30,0,sproeier);  // relais 1 - daglicht[color=#222222][/color]
  Alarm.alarmRepeat(22,30,0,sproeier);

...and here

    setSyncProvider(RTC.get);

Thx for the help!

The () seemed to solved the problem. The code works so far. I have some futher questions though:

@aarg: Can you please explain whats wrong with this line? It came from an example and I'm not even sure what is does.

  setSyncProvider(RTC.get);

@hzrnbgy: If I understand correctly, you want me to change the line to this:

  Alarm.alarmRepeat(22,30,0,sproeier());

but when I do I get this:

C:\Users\Tim\Documents\Arduino\Projects\Palu Control\sketch_jan15a\sketch_jan15a.ino: In function 'void setup()':
sketch_jan15a:114:34: error: invalid use of void expression
   Alarm.alarmRepeat(9,30,0,flip());  // relais 1 - daglicht
                                  ^
sketch_jan15a:115:35: error: invalid use of void expression
   Alarm.alarmRepeat(20,34,0,flip());
                                   ^
sketch_jan15a:117:38: error: invalid use of void expression
   Alarm.alarmRepeat(9,30,0,sproeier());  // relais 1 - daglicht
                                      ^
sketch_jan15a:118:39: error: invalid use of void expression
   Alarm.alarmRepeat(22,30,0,sproeier());
                                       ^
exit status 1
invalid use of void expression

  setSyncProvider(RTC.get);Oh, wait... that one is correct. It's a relatively rare use of function call address. Sorry. Not the others, though...

It instructs the Time library to synchronize its software clock to the RTC.

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