Part of the code I used
Count = 1;
CountUP()
{
Val = analogRead(X);
delay(200);
if (Val >= 200 && Val <=250 && Count <10)
{
Count = Count =+1;
}
}
I have two values 400-450 and 200-250
When the Value (Val) is between 200-250
I want only to count once in 200-250 untill i change to 400-450 value and again change to 200- 250
but this code counts continuously from 1 to 10 while in 200-250
Any idea how to stop this?
Try this:
Count = 1;
bool flag = false;
CountUP()
{
Val = analogRead(X);
delay(200);
if (Val >= 200 && Val <= 250 && Count < 10 && flag == false; )
{
flag = true;
Count = Count = +1;
}
if (Val < 200 !! Val > 250 ){
flag = false;
}
ops there a error, sorry
try this:
Count = 1;
bool flag = false;
CountUP()
{
Val = analogRead(X);
delay(200);
if (Val >= 200 && Val <= 250 && Count < 10 && flag == false )
{
flag = true;
Count = Count = +1;
}
if (Val < 200 || Val > 250 ){
flag = false;
}
There is another error, bus this is your error:
Count = Count = +1; error
Count = Count +1; Right
or
Count++;
Complete test:
float Val = 0;
int Count = 1;
void setup() {
Serial.begin(115200);
}
//---------------------------------------------------------------
void loop() {
CountUP();
delay(500);
}
//---------------------------------------------------------------
bool flag = false;
void CountUP()
{
Serial.println(Count);
Serial.println(Val);
Val = analogRead(0);
delay(200);
if (Val >= 200 && Val <= 250 && Count < 10 && flag == false )
{
flag = true;
Count++;
}
if (Val < 200 || Val > 250 ) {
flag = false;
}
}
But a problem, i used the same code for count down , when i use it, then it again repeat the counter ( for both up and down,
Count down value is 650 - 700
Normal position 400-450 nothing happens
Count up 200-250
Count down 650-700
Your code works for count up,
When use same code for count down,
Repeat starts again