Time function in arduino

hello, i am currently working on a school project, but i seem to struggle on how i am going to properly use the weekday() function from the arduino time library (Arduino Playground - Time)

the project should be related to a smart house and i and my group decided to design a smart function where the the house will be wamer when a person wakes up and coma back from work and lower the temperature/not heating up while the person is asleep or away

NOTE: there may be other flaws in this code as well

i would appreciate any help that might solve this problem. thanks!

#include <time.h>
//#define weekday(_time_)  ((( _time_ / SECS_PER_DAY + 4)  % DAYS_PER_WEEK)+1) // 1 = Sunday
const int sensorPin = A0;//temp sensor
int day, hour_start, hour_stop;
int day_of_week = weekday();

/*
tmElements_t tm;   // declare a time structure element
RTC.read(tm); // Read the current date & time as TimeElements variable
tm.[color=blue]Hour[/color]++;  // access the hour element and increase it
if (tm.Hour  > 23) tm.Hour  = 0; // check for overflow, after 23h goes to 0h
RTC.write(tm); // write it back
*/

void setup() {
  // put your setup code here, to run once:
  
  Serial.begin(9600);
  int pinNumber = 2;    //pin 2 is connected to LED, simulates heating element
  pinMode(2, OUTPUT);   //-||-
  digitalWrite(2, LOW); //-||-
}



void loop() {
  // put your main code here, to run repeatedly:
  int phone_signal = false;

  
  int sensorVal = analogRead(sensorPin);//sensorval is bewteen 0 and 1023, value from SensorPin A0
  float voltage = (sensorVal/1024.0)*5.0; //determine voltage from 0-5
  float temperature = (voltage - .5)*100; //10 mV equals 1 degree celcius of change
  
  while (phone_signal == false){
    default_schedule();
  }
  while (phone_signal == true){
    exception_time(3, 8, 15);//tuesday, warm from 8 to 15
  }

}

int default_schedule(){
  while (day_of_week == 2){//monday
    while ((hour >= 0 and hour < 7) or (hour >=8 and hour < 15) or (hour >=22 and hour <=23)){
      while (temperature < 17){
        digitalWrite(2,HIGH);//simulates heating element
      }
    }
    while ((hour >= 7 and hour < 8) or (hour >=15 and hour < 22)){
      while (temperature < 21){
        digitalWrite(2,HIGH);//simulates heating element
      }
    }
    digitalWrite(2,LOW);//simulates heating element
  }

   while (day_of_week== 3){//tuesday
    while ((hour >= 0 and hour < 7) or (hour >=8 and hour < 15) or (hour >=22 and hour <=23)){
      while (temperature < 17){
        digitalWrite(2,HIGH);//simulates heating element
      }
    }
    while ((hour >= 7 and hour < 8) or (hour >=15 and hour < 22)){
      while (temperature < 21){
        digitalWrite(2,HIGH);//simulates heating element
      }
    }
    digitalWrite(2,LOW);//simulates heating element
  }

  while (day_of_week== 4){//wednesday
    while ((hour >= 0 and hour < 7) or (hour >=8 and hour < 15) or (hour >=22 and hour <=23)){
      while (temperature < 17){
        digitalWrite(2,HIGH);//simulates heating element
      }
    }
    while ((hour >= 7 and hour < 8) or (hour >=15 and hour < 22)){
      while (temperature < 21){
        digitalWrite(2,HIGH);//simulates heating element
      }
    }
    digitalWrite(2,LOW);//simulates heating element
  }

  while (day_of_week== 5){//thursday
    while ((hour >= 0 and hour < 7) or (hour >=8 and hour < 15) or (hour >=22 and hour <=23)){
      while (temperature < 17){
        digitalWrite(2,HIGH);//simulates heating element
      }
    }
    while ((hour >= 7 and hour < 8) or (hour >=15 and hour < 22)){
      while (temperature < 21){
        digitalWrite(2,HIGH);//simulates heating element
      }
    }
    digitalWrite(2,LOW);//simulates heating element
  }

  while (day_of_week== 6){//friday
    while ((hour >= 0 and hour < 7) or (hour >=8 and hour < 15) or (hour >=22 and hour <=23)){
      while (temperature < 17){
        digitalWrite(2,HIGH);//simulates heating element
      }
    }
    while ((hour >= 7 and hour < 8) or (hour >=15 and hour < 22)){
      while (temperature < 21){
        digitalWrite(2,HIGH);//simulates heating element
      }
    }
    digitalWrite(2,LOW);//simulates heating element
  }

  while (day_of_week== 7){//saturday
    while ((hour >= 0 and hour < 7) or (hour >=22 and hour <= 23)){
      while (temperature < 17){
        digitalWrite(2,HIGH);//simulates heating element
      }
    }
    while ((hour >= 7 and hour < 22)){
      while (temperature < 21){
        digitalWrite(2,HIGH);//simulates heating element
      }
    }
    digitalWrite(2,LOW);//simulates heating element
  }

  while (day_of_week== 1){//sunday
    while ((hour >= 0 and hour < 7) or (hour >=22 and hour <= 23)){
      while (temperature < 17){
        digitalWrite(2,HIGH);//simulates heating element
      }
    }
    while ((hour >= 7 and hour < 22)){
      while (temperature < 21){
        digitalWrite(2,HIGH);//simulates heating element
      }
    }
    digitalWrite(2,LOW);//simulates heating element
  }
}
int exception_time(int day, int hour_start, int hour_stop){
  /*int exception_day = day;
  int exception_hours_on_start = hour_start;
  int exception_hours_on_stop = hour_stop;
  */
  while (day == day_of_week){
    while (hour >= hour_start and hour < hour_stop){
      while (temperature < 17){
        digitalWrite(2,HIGH);//simulates heating element
      }
    }
    digitalWrite(2,LOW);//simulates heating element
  }
  default_schedule()
  }
  
}```

That variable will be initialised, but will not be visible in any other function.
This is called a scope issue, and scope is a very important topic you should investigate before you go much further.

That's a convoluted way of doing it. Hard coding the details of the days and times throughout the sketch will make it hard to maintain too.

I notice that the weekdays are all the same - are you keeping the ability to vary them or can you just have weekday or weekend settings?

If I were doing this, I'd lose all the while loops and have one routine that decides the temperature based on day and time and another that controls the heating. The time/day data should probably live in an array of structs.

Yes, odd. I only read the code to see how it relates to your flowchart or whatever you call the diagram.

Looks like almost every while statement should be an if statement.

loop runs over and over, very fast if you let it. Every time through the loop, decide IF it is time to do something. If it is time and temperature to change something.

Your while statements look like you could get stuck in them… trace it with your finger and see.

a7

ability to vary them

i updated that part, thanks

...and now my comment makes no sense.

You forgot about that one.

seems like it shoud be inside void setup i used an example from the starter kit project book

I don't own a starter kit book, so I can't see how the output pins are used later in the code, because you posted a picture and not actual code.

However, you seem to be comfortable with magic numbers elsewhere in your code, so ask your teacher why they're a bad idea.

#include <TimeLib.h>
//#define weekday(_time_)  ((( _time_ / SECS_PER_DAY + 4)  % DAYS_PER_WEEK)+1) // 1 = Sunday
const int sensorPin = A0; //temp sensor
int temperature;
/*
 tmElements_t tm;   // declare a time structure element
 RTC.read(tm); // Read the current date & time as TimeElements variable
tm.[color=blue]Hour[/color]++;  // access the hour element and increase it
 if (tm.Hour  > 23) tm.Hour  = 0; // check for overflow, after 23h goes to 0h
 RTC.write(tm); // write it back
 */

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);
  int pinNumber = 2;    //pin 2 is connected to LED, simulates heating element
  pinMode(2, OUTPUT);   //-||-
  digitalWrite(2, LOW); //-||-
}

void loop() {
  // put your main code here, to run repeatedly:
  int phone_signal = false;

  int sensorVal = analogRead(sensorPin); //sensorval is bewteen 0 and 1023, value from SensorPin A0
  float voltage = (sensorVal / 1024.0) * 5.0; //determine voltage from 0-5
  float temperature = (voltage - .5) * 100; //10 mV equals 1 degree celcius of change

  while (phone_signal == false) {
    default_schedule();
  }
  while (phone_signal == true) {
    exception_time(3, 8, 15); //tuesday, warm from 8 to 15
  }

}

int default_schedule() {
  while (weekday() == 2) { //monday
    while ((hour() >= 0 and hour() < 7) or (hour() >= 8 and hour() < 15)  or (hour() >= 22 and hour() <= 23)) {
      while (temperature < 17) {
        digitalWrite(2, HIGH); //simulates heating element
      }
    }
    while ((hour() >= 7 and hour() < 8) or (hour() >= 15 and hour() < 22)) {
      while (temperature < 21) {
        digitalWrite(2, HIGH); //simulates heating element
      }
    }
    digitalWrite(2, LOW); //simulates heating element
  }

  while (weekday() == 3) { //tuesday
    while ((hour() >= 0 and hour() < 7) or (hour() >= 8 and hour() < 15)
        or (hour() >= 22 and hour() <= 23)) {
      while (temperature < 17) {
        digitalWrite(2, HIGH); //simulates heating element
      }
    }
    while ((hour() >= 7 and hour() < 8) or (hour() >= 15 and hour() < 22)) {
      while (temperature < 21) {
        digitalWrite(2, HIGH); //simulates heating element
      }
    }
    digitalWrite(2, LOW); //simulates heating element
  }

  while (weekday() == 4) { //wednesday
    while ((hour() >= 0 and hour() < 7) or (hour() >= 8 and hour() < 15)
        or (hour() >= 22 and hour() <= 23)) {
      while (temperature < 17) {
        digitalWrite(2, HIGH); //simulates heating element
      }
    }
    while ((hour() >= 7 and hour() < 8) or (hour() >= 15 and hour() < 22)) {
      while (temperature < 21) {
        digitalWrite(2, HIGH); //simulates heating element
      }
    }
    digitalWrite(2, LOW); //simulates heating element
  }

  while (weekday() == 5) { //thursday
    while ((hour() >= 0 and hour() < 7) or (hour() >= 8 and hour() < 15)
        or (hour() >= 22 and hour() <= 23)) {
      while (temperature < 17) {
        digitalWrite(2, HIGH); //simulates heating element
      }
    }
    while ((hour() >= 7 and hour() < 8) or (hour() >= 15 and hour() < 22)) {
      while (temperature < 21) {
        digitalWrite(2, HIGH); //simulates heating element
      }
    }
    digitalWrite(2, LOW); //simulates heating element
  }

  while (weekday() == 6) { //friday
    while ((hour() >= 0 and hour() < 7) or (hour() >= 8 and hour() < 15)
        or (hour() >= 22 and hour() <= 23)) {
      while (temperature < 17) {
        digitalWrite(2, HIGH); //simulates heating element
      }
    }
    while ((hour() >= 7 and hour() < 8) or (hour() >= 15 and hour() < 22)) {
      while (temperature < 21) {
        digitalWrite(2, HIGH); //simulates heating element
      }
    }
    digitalWrite(2, LOW); //simulates heating element
  }

  while (weekday() == 7) { //saturday
    while ((hour() >= 0 and hour() < 7) or (hour() >= 22 and hour() <= 23)) {
      while (temperature < 17) {
        digitalWrite(2, HIGH); //simulates heating element
      }
    }
    while ((hour() >= 7 and hour() < 22)) {
      while (temperature < 21) {
        digitalWrite(2, HIGH); //simulates heating element
      }
    }
    digitalWrite(2, LOW); //simulates heating element
  }

  while (weekday() == 1) { //sunday
    while ((hour() >= 0 and hour() < 7) or (hour() >= 22 and hour() <= 23)) {
      while (temperature < 17) {
        digitalWrite(2, HIGH); //simulates heating element
      }
    }
    while ((hour() >= 7 and hour() < 22)) {
      while (temperature < 21) {
        digitalWrite(2, HIGH); //simulates heating element
      }
    }
    digitalWrite(2, LOW); //simulates heating element
  }
}
int exception_time(int day, int hour_start, int hour_stop) {
  /*int exception_day = day;
   int exception_hours_on_start = hour_start;
   int exception_hours_on_stop = hour_stop;
   */
  while (day == weekday()) {
    while (hour() >= hour_start and hour() < hour_stop) {
      while (temperature < 17) {
        digitalWrite(2, HIGH); //simulates heating element
      }
    }
    digitalWrite(2, LOW); //simulates heating element
  }
  default_schedule();
}

What makes phone_signal true? If phone_signal never goes true does the loop ever exit?

How can this part of the code run if phone_signal is never changed to true?

Like I wrote, scope is a really important concept to grasp.

like what everyone is saying, scoping, Make shure that the variables you are using, are not declared inside a scope, that hides it from where you use it.
If you declare something inside the loop function, they are not available inside another method.
That beeing said, don't declare everything in the global scope if it is not necessary.

And a method calling itself is bad, escpecially when you dont controll it.
I wouldn't call weekday and hour methods everytime they are needed like what @Juraj did here, but instead declare variable for them at the top in default_schedule, and initialize them there, and use the variables further on.
You also repeat lot of code, mon-fri could be same code it seems, and sat-sun could also be the same code.
Makes it a bit better to read, and easier to maintain.
You probably could even use an structure to define schedule, instead of lot of code like this, but that is too advanced right now it seems :slight_smile:

If you begin with a meaningful, standard flow chart you have a chance. The one you have there would be no help at all in coding this problem. It doesn't follow the flowchart paradigm faithfully, so it's not much use.

A flow chart should show only decisions (branches, conditional program flow) and processes (the rectangle boxes). "While" definitely doesn't belong in there. Just removing it won't help either because the flow is poorly defined.

If you start the race with your shoes tied together, you haven't much chance at a finish.

'bad' is a little harsh, don't you think?

How about:

A method calling itself must control the depth of the recursion,
and keep an eye on the used stack space, or it will crash the system.

Rather than use procedural or functional programming, the OP's chart and code are exemplary aspirational programming.

a7

I meant bad practice, the last word fell out, but I guess that didn't help either.

You are right, I would have disagreed to 'bad practice' also.

Recursion is a nice tool, but like a scalpel, it needs some care in handling.

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