Go Down

Topic: Problem with counting up (Read 623 times) previous topic - next topic

lax123

Hi,

i have 2 pressure sensors, whenever the pressure rises like above the value of 2 i want to count that.

my sketch is working perfectly fine, but that counting part is not working at all.

i thought it would be as simple as this:

int Senscount0 = 0;
if (Druck0 >0.01){

Senscount0=Senscount0+1;   //or Senscount0++;

}
else{}

but it seems not to be.
When pressure is like below my threshold (here 0.01) it shows me 1 in the display, and if i go to a negative value with the pressure it shows 0 on my display. Going above 0.01 doesnt change the 1.

Could you help me please?

This is my code:


Code: [Select]
#define Drucksensor0PIN 0                 // Analog Pin 0
#define Drucksensor1PIN 1                 // Analog Pin 1

#include <math.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>  // F Malpartida's NewLiquidCrystal library
#define I2C_ADDR    0x27  // Define PCF8574A's I2C Address
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7
#define  LED_OFF  0
#define  LED_ON  1
LiquidCrystal_I2C  lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);


float vcc = 5;                    
float SpannungSens0;  
float Drucksensor0(int ADC0) {

SpannungSens0 = ((ADC0*vcc)/1024.0);
Serial.print("   ");
Serial.print(ADC0);
Serial.print("   ");
return SpannungSens0;


}

float SpannungSens1;
float Drucksensor1(int ADC1) {

SpannungSens1 = ((ADC1*vcc)/1024.0);                    
Serial.print("   ");
Serial.print(ADC1);
Serial.print("   ");
return SpannungSens1;                                    
}

void setup() {
 Serial.begin(9600);
 lcd.begin (20,4);
 lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
 lcd.setBacklight(LED_ON);
 }

void loop() {
float SpannungSens0;
float Druck0;
float SpannungSens1;
float Druck1;
int Senscount0 = 0;

  SpannungSens0=Drucksensor0(analogRead(Drucksensor0PIN));      
  SpannungSens1=Drucksensor1(analogRead(Drucksensor1PIN));

 lcd.setCursor(5, 1);
 Druck0=((((SpannungSens0/5.0)-0.04)/0.09));
 Druck1=((((SpannungSens1/5.0)-0.5)/0.2));
 lcd.print("p0: ");
 lcd.print(10 * Druck0,2);

 lcd.print("mbar  ");
 lcd.setCursor(5, 2);
 lcd.print("p1: ");
  lcd.print(10 * Druck1,2);
  lcd.print("mbar  ");
lcd.print (Senscount0);
 Serial.print(Druck0 * 10,1);
 Serial.print("mbar");
 
 
 if (Druck0 >0.01){

Senscount0=Senscount0+1;

}
else{}

lcd.setCursor(0, 0);
lcd.print (Senscount0);
Serial.print(Senscount0);
 Serial.println("");                                  
 delay(2000);                                      
}



pylon

You set Senscount0 to be a local variable which is reset to 0 every time loop() is called:

Code: [Select]
void loop() {
float SpannungSens0;
float Druck0;
float SpannungSens1;
float Druck1;
int Senscount0 = 0;
...
}


You must make Senscount0 a global variable to have the effect you probably wanna have.

lax123

omg so stupid that i want to punch myself in the face.

thx :-)

GoForSmoke

You could make the local variable static and then it will keep its value.

Other things:

Code: [Select]
float Drucksensor0(int ADC0) {

SpannungSens0 = ((ADC0*vcc)/1024.0);
Serial.print("   ");
Serial.print(ADC0);
Serial.print("   ");
return SpannungSens0;
}


Is essentially the same as float Drucksensor1(int ADC1) {

There is no need for two of the same. The passed arg (int ADC0) is just an int, it could be either. The name inside the function is whatever you called it in the function definition.

Also where you divide by 1024 should be divide by 1023. Yes, really. Do the math.

So one function:
Code: [Select]
float Drucksensor(int ADC) {

SpannungSens0 = ((ADC*vcc)/1023.0);
Serial.print("   ");
Serial.print(ADC);
Serial.print("   ");
return SpannungSens0;
}


and you call the function:
Code: [Select]

   SpannungSens0=Drucksensor(analogRead(Drucksensor0PIN));       
   SpannungSens1=Drucksensor(analogRead(Drucksensor1PIN));


See the SpannungSens0 & SpannungSens1 get the result of whatever is fed to Drucksensor();

At some point you should consider grouping SpannungSens0 & SpannungSens1 into a two-element array and Drucksensor0PIN & Drucksensor1PIN into another. Then you can access them from a common name and choose which by index.
Code: [Select]

#define Drucksensor0PIN 0                 // Analog Pin 0
#define Drucksensor1PIN 1                 // Analog Pin 1

byte DrucksensorPIN[2] = { Drucksensor0PIN, Drucksensor1PIN };
float  SpannungSens[2];

...

for ( byte i = 0; i < 2; i++ ) {
   SpannungSens[ i ] = Drucksensor(analogRead(DrucksensorPIN[ i ]));       
}


Yeah for two elements it seems a waste until you go changing your code. Then spaghetti begins.
I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

lax123

#4
Jun 18, 2012, 08:07 pm Last Edit: Jun 18, 2012, 08:09 pm by lax123 Reason: 1
thx so much GoForSmoke, i love that stuff -i started around a week ago so i better learn from my mistakes rather early before they manifest.

Hehe actually i had 1023 at start, because i thought 0-1023=1024 but then like most examples here use 1024...

GoForSmoke

It had to be pointed out to me to use 1023 but I did the math and had to agree. Intuition not followed up is like spelling a word wrong over and over.
I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Go Up