Can the (if statement) have A counter on it?
hi read this.
i know if (if statement) can have counter on it. count ten , stop hole program.
here little bit of code want run 10 times , go (state = 0) ,
turn off arduino or stop code going on.
all of program
i know if (if statement) can have counter on it. count ten , stop hole program.
here little bit of code want run 10 times , go (state = 0) ,
turn off arduino or stop code going on.
code: [select]
if (counter > maxnum) {
// turn led on:
state = 1;
}
else {
// turn led off:
state = 0;
delay(10);
}
all of program
code: [select]
const int ledpin = 9;
int inputpin = a5;
int sensorvalue;
int counter = 0;
int maxnum = 200;
byte state;
void setup() {
pinmode(ledpin, output);
serial.begin(9600);
}
void loop() {
// serial.print("input on pin 5: ");
// serial.println(sensorvalue);
serial.print("led state: ");
serial.println(state);
// serial.print("counter: ");
// serial.println(counter);
sensorvalue = analogread(inputpin);
if (state == 0)
{
counter++;
}
if (sensorvalue > 0) {
// turn led on:
digitalwrite(ledpin, high);
counter = 0;
}
else {
// turn led off:
digitalwrite(ledpin, low);
delay(10);
}
if (counter > maxnum) {
// turn led on:
state = 1;
}
else {
// turn led off:
state = 0;
delay(10);
}
}
here little bit of code want run 10 times , go (state = 0) ,
turn off arduino or stop code going on.
sounds want loop, code doesn't seem match requirements:
code: [select]
if (state == 0)
{
counter++;
}
so every pass through loop, want counter increment (as long sensor reading zero)? with delay, gives 2 seconds before maxnum hit.
Arduino Forum > Using Arduino > Programming Questions > Can the (if statement) have A counter on it?
arduino
Comments
Post a Comment