I have a question about a sketch I am running.
Got an Arduino to read and register gasmeter etc. Unfortunately our gasmeter (1987) doesn't have mirror on digit so I have put a CNY70 IR sensor on and after reading values I parse these to identify when the 2nd digit turns over.
When I test the code occasionally I get 2 additions, in 1 loop, and I don't understand. Could it be because I am using float as type??
Someone have any ideas?
Code:
int sensorPin = A2; // select the input pin for the potentiometer
int currentReading = 0; // variable to store the value coming from the sensor
int previousReading = 0;
int pHigh = 0;
int pLow = 0;
float totalgasteller=33303.16;
void setup() {
Serial.print ("start....");
Serial.begin (9600);
currentReading = analogRead(sensorPin);
previousReading=currentReading;
}
void loop() {
// read the value from the sensor:
currentReading = analogRead(sensorPin);
pHigh=previousReading + 5;
pLow=previousReading - 5;
if (currentReading >= pHigh || currentReading <= pLow) {
// it has turned
totalgasteller+=0.01;
Serial.print ("Teller: ");
Serial.print(totalgasteller);
Serial.print (" ");
Serial.print(currentReading);
Serial.print (" - ");
Serial.print(previousReading);
Serial.print (" ");
Serial.println();
delay(4000);
currentReading = analogRead(sensorPin);
}
previousReading=currentReading;
delay(500);
Serial.print ("reading: ");
Serial.print(currentReading);
Serial.println();
}
and this is 2 parts from my logging;
reading: 380
reading: 380
reading: 382
Teller: 33303.19 392 - 382
reading: 377
reading: 377
reading: 377
reading: 377
reading: 378
reading: 378
reading: 378
reading: 378
reading: 378
reading: 378
Teller: 33303.21 384 - 378
reading: 378
reading: 378
Teller: 33303.26 377 - 382
reading: 380
reading: 379
reading: 380
reading: 380
reading: 380
reading: 379
reading: 380
reading: 380
reading: 380
Teller: 33303.28 387 - 380
reading: 376
reading: 377
reading: 376