Auto-zero

Hello,

I am working whit pressure sensor. Some how i nead a first analog value, every time i turn on my analog sensor, so, i can substract it to get zero. And later, that line should be ingnored.

I dont know how to do it, hellp :slight_smile:

Put it in "setup()"

I am tryng this code

void setup() {
  Serial.begin(9600);
 float sen=analogRead(A0);
 Serial.println("sen" );
  Serial.println( sen);

}

void loop() {
  int sensorValue = analogRead(A0);
  float voltage = sensorValue * (5.0 / 1023.0);
  Serial.println(voltage);


  delay (1000);
}

When i try this code, i want to see "sen" printed out whit current value. Its in serial (), but i cant see the value printed, it only prints the voltage.

The problem with what's declared in "setup()" (like "sen") stays in "setup()"

setup() only runs when you first reset the Arduino.
loop() will run again and again....

If my analogvalue ("sen") stays in setup, then how is it posible to use it later in my loop? Is there a way? maybe i can put it in global as iteger? it doesint work.

float adcResult = 0;
float greitis = 0;
int nul=analogRead(0)*(5.0/1023);

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  adcResult = adcResult * 0.05 + analogRead(0) * 0.95;
  greitis = (sqrt(2000*((adcResult * (5.0 / 1023.0))-nul)/1.204))*3.6;
  if (greitis >=0)
{Serial.print(" greitis ");
Serial.println( greitis );
Serial.println( nul );
}
else
{
  Serial.print(" greitis ");
  Serial.println(" 0 ");
}
  
  delay(50);
}

Maybe integer variables don't store floating point values.
Maybe you could declare it as a global but initialise it in "setup()"

make the var global do the work in setup()

float sen;  // make it global

void setup() 
{
  Serial.begin(115200);
  sen = analogRead(A0);
  Serial.print("sen: " );
  Serial.println( sen);
  delay (1000);
}

void loop() 
{
  int sensorValue = sen - analogRead(A0);
  float voltage = sensorValue * (5.0 / 1023.0);
  Serial.println(voltage);
  delay (1000);
}

WATCH OUT!!

greitis = (sqrt(2000*((adcResult * (5.0 / 1023.0))-nul)/1.204))*3.6;

if nul larger than (adcResult * (5.0 / 1023.0)) you get the sqrt of a negative number

Lithuania?

PS, please use CTRL-T before posting code

Thanks! Solved it! :slight_smile: Yes, Lithuania :slight_smile: greitis - speed, the code is for mesuring airspeed. I know that i can get a negative numper, thats why i put it in if statment. But i will change the code later, to awoid it. Its only simple skech, for testing. Im new to programing, so its a bit dificult :slight_smile:

greitis - speed

I looked up that one :slight_smile: