Example advise not using the Millis function.

Hi to eyerybody.

Can someone give me an EXAMPLE CODE how to read data from a soil moisture sensor every 12 hours without using the MILLIS and DELAY function?.

I am using :

int sensorPin = 0; // select the input pin for the Soil moisture sensor
int sensorValue = 0; // variable to store the value coming from the sensor

void setup() {
// declare the ledPin as an OUTPUT:
Serial.begin(9600);
}

void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
delay(1000);
Serial.print("sensor = " );
Serial.println(sensorValue);
}

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

dataoikogarden:
without using the MILLIS

Why don't you want to use millis()?

If you need better timing accuracy then I recommend using an RTC (real time clock).

You could use micros, and handle the rollover yourself

How about using a Real Time Clock (RTC) ?

Thanks for the respont.
I am not so much interstead for a accurasy. I do not want to use miilis because it hastime limit .
If i am using RTC whai for example i need to change inside the code for similliar to delayed without the the time limitation after 49 days or so? That why i am trying to figure days now how to do it.

dataoikogarden:
I do not want to use miilis because it hastime limit .

A problem easily overcome by correctly handling the rollover. The trick is in the unsigned subtraction. (And lack of range checking by the compiler.)

Ah, an XY problem! There is absolutely no time limit problem for you using millis() as long as you write your code correctly, and that's quite easy to do. You can find an example of how to do this at File > Examples > 02.Digital > BlinkWithoutDelay and the associated tutorial:

exacly XY PROBLEM !!

thanks for your help!

Soory because i havent usw <> for the code.

I do not want to use miilis because it hastime limit .

Wrong !

Have a look at Using millis() for timing. A beginners guide

well BlinkWithoutDelay it is not the answer for my problem. Any other advice ?

dataoikogarden:
well BlinkWithoutDelay it is not the answer for my problem. Any other advice ?

Read it again

dataoikogarden:
Hi to eyerybody.

Can someone give me an EXAMPLE CODE how to read data from a soil moisture sensor every 12 hours without using the MILLIS and DELAY function?.

dataoikogarden:
well BlinkWithoutDelay it is not the answer for my problem. Any other advice ?

if you dont want to use RTC then millis() is the way to go my friend. something like this will most likely work for you! :slight_smile:

#define HOURS 12
#define MINUTES 60
#define SECONDS 60
#define SECOND 1000 //1s =1000ms

int sensorPin = 0;    // select the input pin for the Soil moisture sensor
int sensorValue = 0;  // variable to store the value coming from the sensor
uint8_t seconds_counter = 0;
uint8_t minute_counter = 0;
uint8_t hour_counter = 0;
unsigned long oldtime;

void setup() {
  // declare the ledPin as an OUTPUT:
  Serial.begin(9600);

  oldtime = millis();
}

void loop() {

  if (millis() - oldtime > SECOND) {
    //update second counter approx ever second
    seconds_counter = (seconds_counter + 1) % SECONDS;

    if (seconds_counter == 0) {
      //update minute counter every 60s
      minute_counter = (minute_counter + 1) % MINUTES;

      if (minute_counter == 0) {
        //update hour counter every 60 minutes
        hour_counter = (hour_counter + 1) % HOURS;

        if (hour_counter == 0) {
          // read the value from the sensor every 12 hours
          analogRead(sensorPin);
          sensorValue = analogRead(sensorPin);


          Serial.print("sensor = " );
          Serial.println(sensorValue);
        }
      }
    }
    oldtime = millis();
  }
}

dataoikogarden:
well BlinkWithoutDelay it is not the answer for my problem. Any other advice ?

Are you saying that BlinkWithoutDelay will fail because of some time limit ?

Take BWOD, change the period to 12 hours then, instead of changing the state of an LED read the sensor.

Job done

YEP ! SOUNDS GOOD . MANY Thanks, I appreciate the help you give me !!!! :slight_smile:

PLEASE HELP I have another question. I interface finally RTC DS 1307 with arduino uno and my soil sensors. Everything is working fine .

NO THAT I AM USING THE PREVIOUS CODE WITH RTC , DO I HAVE TO CHANGE SOMETHING INSIDE THE CODE OR I CAN KEEPIT AS IT IS ?

case closed

const byte SensorPin = A0;    // select the input pin for the Soil moisture sensor


const unsigned long TwelveHourInterval = 12UL * 60UL * 60UL * 1000UL;


unsigned long StartTime;


void setup()
{
  Serial.begin(9600);
  StartTime = 0;
}


void loop()
{
  if (millis() - StartTime >= TwelveHourInterval)
  {
    StartTime += TwelveHourInterval;
    int sensorValue = analogRead(SensorPin);
    Serial.print("sensor = " );
    Serial.println(sensorValue);
  }
}

i have to thank everyone for the help that give to me .
God bless you all.

There is absolutely no time limit problem for you using millis() as long as you write your code correctly, and that's quite easy to do.

Suppose that you want to do something every 60 days...

Can you show me the easy code to do that, using millis()?

Suppose that you want to do something every 60 days...

pert will no doubt be along soon to reply, but timing 2 sequential 30 day periods is hardly difficult as you well know