Go Down

Topic: double addition in sketch (Read 685 times) previous topic - next topic

ewatje

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:
Code: [Select]

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

PaulS

Code: [Select]
  currentReading = analogRead(sensorPin);   
  pHigh=previousReading + 5;
  pLow=previousReading - 5;

  if (currentReading >= pHigh || currentReading <= pLow) {
    <snip>
    delay(4000);
    currentReading = analogRead(sensorPin);
  }
  previousReading=currentReading;

I don't understand this. If the current reading differed from the previous reading by a significant amount, do some stuff, wait 4 seconds, take another reading and store that as the previous reading.

0.01 is not a value that can easily be represented in binary. I think that if you print the value to more than 2 decimal places, you'll see the problem.

ewatje

yes, i use the pHigh and pLow as the boundries for reading the sensor.

But I will try and change the float to in and see what happens. The totalgasteller that is defined as float is the actual meter reading.

I will double check using an int.

thx, and hope solved :)


robtillaart

better use an unsigned long as totaalteller.

At the end you can do something like

Serial.println(totaalteller*0.01, 2);

to print it with two digits
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

ewatje

@ Rob & Paul,

thx, have had a quick play and addition with long is not going that well.

Have indeed updated to unsigned long to keep it consistant.

thx for the replly.



robtillaart

Code: [Select]
void setup()
{
  Serial.println("start....");
  Serial.begin(9600);
  currentReading = analogRead(sensorPin);
  previousReading = currentReading;
}

you should also switch the first two lines of setup() - see?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart

Code: [Select]
  pHigh = previousReading + 5;
  pLow = previousReading - 5;

  if (currentReading >= pHigh || currentReading <= pLow) {


how about

Code: [Select]
if (abs(currentReading - previousReading) > 5) {

and

Code: [Select]
    Serial.print(previousReading);
    Serial.print ("   ");
    Serial.println(); 


into

Code: [Select]
    Serial.println(previousReading);
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up