Go Down

Topic: Example advise not using the Millis function. (Read 589 times) previous topic - next topic

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?.

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);                   
}

 

pert

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]// your code is here[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.


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).

AWOL

You could use micros, and handle the rollover yourself
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

UKHeliBob

How about using a Real Time Clock (RTC) ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

dataoikogarden

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.

Coding Badly

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.)


pert

#6
Nov 03, 2018, 10:04 am Last Edit: Nov 03, 2018, 10:04 am by pert Reason: Improve Formatting
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:
https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

dataoikogarden


exacly XY PROBLEM !!

thanks for your help!

Soory because i havent usw <> for the code.

UKHeliBob

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

Have a look at Using millis() for timing.  A beginners guide
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

dataoikogarden

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

AWOL

well BlinkWithoutDelay it is not the answer for my problem. Any other advice ?
Read it again
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

sherzaad

#11
Nov 03, 2018, 02:03 pm Last Edit: Nov 03, 2018, 02:19 pm by sherzaad
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?.


 
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!  :)
Code: [Select]
#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();
  }
}

UKHeliBob

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
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

dataoikogarden

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

dataoikogarden

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 ?


Go Up