Fun with Arduino 10 Show the Day-Night Cycle Status on the User Interface

Now that our code works well, thanks to using the unsigned long data type or by typecasting an int to an unsigned long where needed, we can now continue with our User Interface. We like to see the current status of the day-night cycle on the PC screen and dynamically update it. The plan is to see these messages:

  • cycle is started
  • cycle is stopped
  • day time xxx seconds
  • night time xxx seconds

Read on below the video …

 

 

Let’s first add the ‘started’ and ‘stopped’ print statements (shown in red) and test them.

#define SWITCH_PIN 2         // input for toggle switch
#define LIGHTS_PIN 8         // output to FET or relay module
#define LED_PIN A5           // output to LED on switch panel
unsigned int cycle_time = 1; // [s] 180 = 3 minutes

void setup() {
  pinMode(SWITCH_PIN, INPUT_PULLUP);
  pinMode(LIGHTS_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Day/Night cycle system is ready");
  Serial.println(); // This prints an empty new line
  Serial.print("Cycle time: ");
  Serial.print(cycle_time);
  Serial.println(" seconds");
  Serial.println();
}

void loop() {
  if (digitalRead(SWITCH_PIN) == LOW) {
    Serial.println("cycle started");
    digitalWrite(LED_PIN, HIGH);
    digitalWrite(LIGHTS_PIN, HIGH);
    delay((unsigned long)cycle_time * 1000); 
    digitalWrite(LIGHTS_PIN, LOW);
    delay((unsigned long)cycle_time * 1000);
  }
  else {
    digitalWrite(LED_PIN, LOW);
    Serial.println("cycle stopped");
  }
}

printtoomuchOops … that is not what we want … they keep being printed every time the loop is executed. We want it to print just once after the toggle switch has changed state. Hm, let’s think … we can detect if the switch changed state if we’d compare the new state with the old state. Yes, let’s introduce two variables, switch_new and switch_old, they can be bytes cause we only store 0 or 1 in them. We compare the two in an ‘if’ statement, and only print the new state if there was a change:

#define SWITCH_PIN 2          // input for toggle switch
#define LIGHTS_PIN 8          // output to FET or relay module
#define LED_PIN A5            // output to LED on switch panel
byte switch_new, switch_old;  // status of the toggle switch
unsigned int cycle_time = 1;  // [s] 180 = 3 minutes

void setup() {
  pinMode(SWITCH_PIN, INPUT_PULLUP);
  pinMode(LIGHTS_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Day/Night cycle system is ready");
  Serial.println();
  Serial.print("Cycle time: ");
  Serial.print(cycle_time);
  Serial.println(" seconds");
  Serial.println();
}

void loop() {
  switch_new = digitalRead(SWITCH_PIN);
  if (switch_new != switch_old) { // state changed, print once
    if (switch_new == LOW)
      Serial.println("cycle started");
    else
      Serial.println("cycle stopped");
    switch_old = switch_new;
  }
  if (switch_new == LOW) {
    digitalWrite(LED_PIN, HIGH);
    digitalWrite(LIGHTS_PIN, HIGH);
    delay((unsigned long)cycle_time * 1000); 
    digitalWrite(LIGHTS_PIN, LOW);
    delay((unsigned long)cycle_time * 1000);
  }
  else {
    digitalWrite(LED_PIN, LOW);
  }
}

printokLet’s test this … yes, this works fine.

Note how byte switch_new, switch_old; are declared on one line, comma separated. Yes, that is allowed. Also note the comparison in if (switch_new != switch_old) … ‘not equals’ is written as !=. With Arduino code, in general ‘!’ stands for ‘not’. More comparison- and mathematics notations can be found in the Arduino code reference.

We can now add the day & night status, including the current cycle time:

#define SWITCH_PIN 2         // input for toggle switch
#define LIGHTS_PIN 8         // output to FET or relay module
#define LED_PIN A5           // output to LED on switch panel
byte switch_new, switch_old; // status of the toggle switch
unsigned int cycle_time = 1; // [s] 180 = 3 minutes

void setup() {
  pinMode(SWITCH_PIN, INPUT_PULLUP);
  pinMode(LIGHTS_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Day/Night cycle system is ready");
  Serial.println();
  Serial.print("Cycle time: ");
  Serial.print(cycle_time);
  Serial.println(" seconds");
  Serial.println();
}

void loop() {
  switch_new = digitalRead(SWITCH_PIN);
  if (switch_new != switch_old) {
    if (switch_new == LOW)
      Serial.println("cycle started");
    else
      Serial.println("cycle stopped");
    switch_old = switch_new;
  }
  if (switch_new == LOW) {
    digitalWrite(LED_PIN, HIGH);
    digitalWrite(LIGHTS_PIN, HIGH);
    Serial.print("night time = ");
    Serial.print(cycle_time);
    Serial.println(" seconds");
    delay((unsigned long)cycle_time * 1000); 
    digitalWrite(LIGHTS_PIN, LOW);
    Serial.print("day time = ");
    Serial.print(cycle_time);
    Serial.println(" seconds");
    delay((unsigned long)cycle_time * 1000);
  }
  else {
    digitalWrite(LED_PIN, LOW);
  }
}

printstatusYes … this looks good. Although we could add 2 spaces with day time to better vertically align.

I think it a bit boring that the day and night times always exactly equal the cycle_time. I like to introduce some variation. Let’s create a day_time and a night_time variable and give them random values, based on cycle_time. We have used the random(x,y) statement before, we know how to do this. For test purposes let’s just add a small amount of random seconds. Later, with the definitive cycle time in the minutes range, we can add more seconds. The definitive code now looks like this:

#define SWITCH_PIN 2         // input for toggle switch
#define LIGHTS_PIN 8         // output to FET or relay module
#define LED_PIN A5           // output to LED on switch panel
#define T_MIN 1              // min random time to add to cycle_time
#define T_MAX 5              // max random time to add to cycle_time
byte switch_new, switch_old; // status of the toggle switch
unsigned int cycle_time = 1; // [s] 180 = 3 minutes
unsigned int day_time, night_time;

void setup() {
  pinMode(SWITCH_PIN, INPUT_PULLUP);
  pinMode(LIGHTS_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Day/Night cycle system is ready");
  Serial.println();
  Serial.print("Cycle time: ");
  Serial.print(cycle_time);
  Serial.println(" seconds");
  Serial.println();
}

void loop() {
  switch_new = digitalRead(SWITCH_PIN);
  if (switch_new != switch_old) {
    if (switch_new == LOW) Serial.println("cycle started");
    else Serial.println("cycle stopped");
    switch_old = switch_new;
  }
  if (switch_new == LOW) {
    digitalWrite(LED_PIN, HIGH);
    digitalWrite(LIGHTS_PIN, HIGH);
    night_time = cycle_time + random(T_MIN, T_MAX);
    Serial.print("night time = ");
    Serial.print(night_time);
    Serial.println(" seconds");
    delay((unsigned long)night_time * 1000); 
    digitalWrite(LIGHTS_PIN, LOW);
    day_time = cycle_time + random(T_MIN, T_MAX);
    Serial.print("day time   = ");
    Serial.print(day_time);
    Serial.println(" seconds");
    delay((unsigned long)day_time * 1000);
  }
  else {
    digitalWrite(LED_PIN, LOW);
  }
}

printcycleThis looks good. Thanks to this user interface we exactly know what the current status is and and what the day and night times are, nicely vertical aligned. We also have some variation in the day – night times thanks to adding some second with the random() statement.

We have accomplished a lot! On to the next challenge … we have this variable cycle_time that we never used as such yet … in the next video the goal is to be able to change the cycle_time on the fly, using the PC keyboard. There is a function we can use for that: Serial.read().

— 0 —

6 thoughts on “Fun with Arduino 10 Show the Day-Night Cycle Status on the User Interface

  1. Hi Rudy
    Having great fun following your arduino videos.

    All fine up to this one. (10)

    Have the following problem –

    Cant get cycle stopped to stop repeating – cycle started holds at one ( does not keep repeating) until switched off.

    Have gone through my code many times just cant see the error

    any suggestions?

    Mike

    Like

    • Hi Mike, I could not tell … not without seeing your code. Have you done a copy – paste from the code here on the blog? That should work … unless maybe somehow a typo crept in …

      Like

  2. Hi

    Have copied my code

    Thank you for your swift response

    Mike

    #define SWITCH_PIN 2
    #define LIGHTS_PIN 8
    #define LED_PIN A5
    #define T_MIN 1
    #define T_MAX 5
    byte switch_new, switch_old ;
    unsigned int cycle_time = 3; // [s] 180 = 3 minutes
    unsigned int day_time, night_time;

    void setup() {
    pinMode(SWITCH_PIN,INPUT_PULLUP);
    pinMode(LIGHTS_PIN,OUTPUT);
    pinMode(LED_PIN,OUTPUT);
    Serial.begin(9600);
    Serial.println(“Day/Night cycle system is ready”);
    Serial.println();
    Serial.print(“Cycle time: “);
    Serial.print(cycle_time);
    Serial.println(“seconds”);

    }

    void loop() {
    switch_new = digitalRead(SWITCH_PIN);
    if (switch_new != switch_old) {
    if (switch_new == LOW) Serial.println (“cycle Started”);
    else Serial.println (“cycle Stopped”);
    switch_old = switch_new;
    }
    if (switch_new == LOW){
    digitalWrite(LED_PIN,HIGH);
    digitalWrite(LIGHTS_PIN,HIGH);
    night_time = cycle_time + random(T_MIN,T_MAX);
    Serial.print(“night time = “);
    Serial.print(night_time);
    Serial.println(“seconds”);
    delay((unsigned long) cycle_time * 1000);
    digitalWrite(LIGHTS_PIN,LOW);
    day_time = cycle_time + random(T_MIN, T_MAX);
    Serial.print(“day time = “);
    Serial.print(day_time);
    Serial.println(“seconds”);
    delay((unsigned long)cycle_time * 1000);

    }
    else {
    digitalWrite(LED_PIN,LOW);
    Serial.println (“cycle stopped”);
    }
    }

    Like

  3. Ok thank you,

    will also printout your code and my code so can do a further search to find my error.

    thanks for your response and keep up the good work, loving these arduino videos

    Mike

    Like

Leave a Reply to RudyB Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s