delay function question

Hello everyone,

I'm using the delay function to create a timer, for 12 hours. I'm new to programming and new to C++, but I would like to display this delay but also display the count down of the delay. so If I have 12 hours of delay I would like my delay function to count every second down to 0. After 0 it would move on.

I'm not sure how to go about doing this or if this can even be done in c++.

Let me know what you are thinking, I'm open to changing my code to use a library if you feel that would be a better way to go about it.

Thank you for your time,
Joe

What is this project for?

Use CTRL T to format your code.
Attach your complete sketch between code tags
[code]Paste your sketch here[/code]

Don’t use delay() as it freezes normal code from executing.

Study this, Several things at the same time.

larryd:
What is this project for?

Good Afternoon Larryd, thank you for getting back to me I was away from my computer all week, but the project will be used for a plant timer, that will have a relay run a light for 12 hours so my plants can get good the right sunlight.

I looked at the blink without delay. I feel I understand the function but it isn’t working for me I don’t understand why

#include <Arduino.h>
#include <potentiometer.h>
#include <OLED.h>
#include <rtc.h>
//#include <LED.h>
#include <wire.h>
//*****************************************Declare*****************************//
// Function prototyping
const int timer12 = 13; // setting timer to ditigal pin 13 for LED example
void setup();
int replaystate = LOW;            // a state for the relay
unsigned long previousMillis = 0; // stores last updated vaule of relay
const long interval12 = 2000;

void timer();
//*****************************************Declare*****************************//
//******************************************Main******************************//

int main()
{

 Potentiometer PotentiometerO;
 OLED OLEDO;
 OLEDO.intdisplay();
 displaytime();
 //ledblink();
 timer(); // Will this allow while loop to run or will it not before the void loop will not end?

 while (true)
 {

   PotentiometerO.getpot();
   OLEDO.OLEDdraw(PotentiometerO.getpot());
 }
 return (0);
} // end int main
//******************************************Main******************************//
//******************************************Setup*****************************//
void setup()
{
 // set the digital relay pin an output
 pinMode(timer12, OUTPUT); // setting pin 3 to an output pin
} //end setup
//******************************************Setup*****************************//
//*****************************************Functions**************************//
void timer()
{
 //check to see if it is time to turn on the relay
 unsigned long currentMillis = millis();
 if (currentMillis - previousMillis >= interval12)
 {
   // save the last time you turned on the relay
   previousMillis = currentMillis;

   // if the relay is off turn it on and vice-versa:
   if (replaystate == LOW)
   {

     replaystate = HIGH;
   }
   else
   {

     replaystate = LOW;
   }
   digitalWrite(timer12, replaystate); // setting the relay state to the realy pinout
 }
} // end timer
 //*****************************************Functions**************************//

Did you decide to write your own main() function for a particular reason rather than using the normal Arduino setup() and loop() function code ?

 timer(); // Will this allow while loop to run or will it not before the void loop

What "void loop" would that be ?
You don't have a loop() function in your program.

Don’t see where setup() is called in main()

Why are you using main() ?

Suggest you use setup() and loop() and let the compiler handle the integration of main() automatically.

i.e. just have
void setup()
{
. . .
}

void loop()
{
. . .
}

other user functions

I don't see where init() is being called...

This is the THIRD thread I've seen just today with people putting main() in their code! Are these people all taking the same class or something??? Clearly not one of them has read a single Arduino tutorial...

And if you ARE going to put a main() in your code, then use Arduino functions, there are thing you MUST do, like call init()!

Regards,
Ray L.

Good afternoon everyone,

I'm really new at programming, I have been learning how to code with a book I got from my c++ class but that is for computer c++ not Arduino.

Sorry to make everyone mad. I had a void loop before but my pot function didn't work with the void loop, I had to change it to int main and return (0) and it started working. it said something like expected int main.

I will go back and change the code up, I will then just get the blink to work and I will slowly move to getting my other code back up and running.

Thanks for the help

Joe

Ok So I got the blink code working when using void loop

I will slowly move all my other code to the void loop.

Thank you for the help everyone !

Joe

No one is mad, confused maybe :slight_smile:

It would be wise to go through the different examples in the IDE to gain some experience and confidence.

larryd:
No one is mad, confused maybe :slight_smile:

It would be wise to go through the different examples in the IDE to gain some experience and confidence.

Hahah your not the first person to tell me that! I guess I should

larryd:
No one is mad, confused maybe :slight_smile:

It would be wise to go through the different examples in the IDE to gain some experience and confidence.

Larryd I got everything working, and also my OLED doesn’t blink anymore after updating a value.

Thanks for the Help everyone!

Congratulations!