Pages: [1]   Go Down
Author Topic: double addition in sketch  (Read 512 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13478
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Rob Tillaart

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

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13478
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void setup()
{
  Serial.println("start....");
  Serial.begin(9600);
  currentReading = analogRead(sensorPin);
  previousReading = currentReading;
}
you should also switch the first two lines of setup() - see?
Logged

Rob Tillaart

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13478
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  pHigh = previousReading + 5;
  pLow = previousReading - 5;

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

how about

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

and

Code:
    Serial.print(previousReading);
    Serial.print ("   ");
    Serial.println(); 

into

Code:
    Serial.println(previousReading);
Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: