Hello everyone, I'm new in this Arduino stuff, I downloaded a code for PLX DAQ, and It worked, the person in charge told me he wanted a delay of 5 minutes between each read (analogRead) which was easy, but then he also told me to add a digitalWrite If the analog sensor raises over 800.
As you can see It can't be done with a single delay because the digital write will take 5 minutes to perform It's action. I have been readeing about Millis but I don't know how to implment it on my code.
Here Is my code:
float pinoPotenciometro = 0;
int linea = 0;
int LABEL = 1;
int valor = 0;
void setup(){
Serial.begin(9600);
Serial.println("CLEARDATA");
Serial.println("LABEL,Hora,valor,linea");
}
void loop(){
valor = analogRead(pinoPotenciometro);
linea++;
Serial.print("DATA,TIME,");
Serial.print(valor);
Serial.print(",");
Serial.println(linea);
if (linea > 100)
{
linea = 0;
Serial.println("ROW,SET,2");
}
delay(300000);
if(analogRead(A0)>=800){
digitalWrite(7,HIGH);
delay(1000);}
if(analogRead(A0)<=800){
digitalWrite(7,LOW);
delay(1000);}
}
moderator added code tags
if you do CTRL-T in the IDE you get more readable code!
some refactoring to remove the delay calls and replace with millis()...
try this variation
const int pinoPotenciometro = A0;
const unsigned long interval = 5 * 60 * 1000UL; // UL stands for unsigned long
unsigned long lastWrite = 0;
unsigned long lastEvent = 0;
int linea = 0;
int LABEL = 1;
int valor = 0;
void setup()
{
Serial.begin(9600);
Serial.println("CLEARDATA");
Serial.println("LABEL,Hora,valor,linea");
}
void loop()
{
// MAKE MEASUREMENT
valor = analogRead(pinoPotenciometro);
// SEND TO PLXDAQ EVERY 5 MINUTES
if (millis() - lastWrite >= interval)
{
lastWrite += interval; // set truigger for next time
linea++;
Serial.print("DATA,TIME,");
Serial.print(valor);
Serial.print(",");
Serial.println(linea);
// UPDATE ADMISITRATION
if (linea > 100)
{
linea = 0;
Serial.println("ROW,SET,2");
}
}
// TAKE ACTION IF NEEDED
if (valor >= 800)
{
digitalWrite(7, HIGH);
lastEvent = millis();
}
if (millis() - lastEvent >= 1000)
{
digitalWrite(7, LOW);
}
}
robtillaart Thank you so much, It worked, since i'm a total noob I'm gonna ask this. I heard If you use millis() in about 50 days the code will stop working. Is It true?
kazplat:
robtillaart Thank you so much, It worked, since i'm a total noob I'm gonna ask this. I heard If you use millis() in about 50 days the code will stop working. Is It true?
Absolutely false. But it will likely happen if you didn't use unsigned long variables as time stamps.