I have two analog sensors hooked up to my arduino. I would like to do a program, which counts how many times the other sensor (pressure sensor) is pressed and at a certain stage of the other sensor. So if the other sensor (a potentiometer) stays untouched the counter goes on forewer, but if the potentiometer is touched and it gives a different output the counter is reseted.
I have managed the counter-part, but I can not figure out, if is possible somehow to reset the counter..So help is really appreciated, I guess this is a simple question for someone, who has more experience..I'm still a novice in these things
Here is my code so far.. :
void setup()
{
Serial.begin(9600);
Serial.print("potentiometer state is : ");
int potentiometer=analogRead(A1);
Serial.println(potentiometer);
}
void loop()
{
int counter=0;
int pressuresensor=analogRead(A0);
int potentiometer=analogRead(A1);
int reading = map (potentiometer, 1023,0,0,100);
while(potentiometer reading stays the same); //I don't know how to do this !!!
{
if(pressuresensor>0)
{
counter=counter+1;
Serial.print(counter);
Serial.print(" at ");
Serial.print(reading);
delay(1000);
}
}
}
while(potentiometer reading stays the same); //I don't know how to do this !!!
Well, to me, "potentiometer reading stays the same" means that the current reading is the same as the previous reading. This implies that you need a global or static variable to save the current reading in, at the end of loop(), as the previous reading.
Then, the while statement would look something like:
while(currReading == prevReading)
{
// Do some stuff
potentiometer=analogRead(A1);
currReading = map (potentiometer, 1023,0,0,100);
}
prevReading = currReading;
unsigned int potentiometer; // value of potmeter reading
unsigned int pressuresensor; // value of pressure sensor reading
unsigned int reading; // mapped potmeter value to range 1-100
unsigned int old_reading; // old mapped value of potmeter
void setup()
{
//
// open serial
//
Serial.begin(9600);
Serial.print("potentiometer state is : ");
//
// do initial reading of potentionmeter
//
potentiometer=analogRead(A1);
old_reading = map (potentiometer, 1023,0,0,100);
//
// display value
//
Serial.println(potentiometer);
//
// initialize counter
//
int counter=0;
}
void loop()
{
//
// read both sensor and potentiometer
//
pressuresensor=analogRead(A0);
potentiometer=analogRead(A1);
reading = map (potentiometer, 1023,0,0,100);
//
// check if potmeter has changed
//
if (reading != old_reading) {
//
// potmeter setting has changed so reset counter and
// save reading
//
old_reading = reading;
counter = 0;
} else {
//
// potmeters has not changed so test if pressure sensor needs
// to be incremented
//
if(pressuresensor>0)
{
//
// pressure sensor shows value > 0
//
counter++;
Serial.print(counter);
Serial.print(" at ");
Serial.print(reading);
delay(1000);
}
}
}
If not it will turn into a negative number as the most significant bit is interpeted as a sign. Defining it as unsigned means it will always be positive. if numbers are less than around 31000 it is no problem. But it is a matter of habit to make a number unsigned if it should not be negative.