I wanna make some sketch to generate number to represent sensor value, with range from high (900) to low (670), that number decrease with range between 1-10 and with some delay of switching number with range from 5-15 seconds:
void setup() {
Serial.begin(9600);
}
void number(){
int sensor = 0; //sensor variable
int a = random(0, 10); //range subtraction sensor value
int delay = random(5000, 15000); //range delay switching value
for(sensor = 900; sensor >= 670; sensor -=a){ //iteration sensor value with range 670-900
Serial.println(sensor);
delay(delay);}
}
void loop() {
number();
}
with output:
- 900 (delay 7 sec)
- 894 (delay 3 sec)
- 988 (delay 5 sec)
- 979 (delay 9 sec)
The sketch above was work as well, but i still can't using variable "sensor" result after iteration "for" in void loop. I hope you can help me, thanks.
c++
tag is valid, but the other 4 tags are not valid ... it is not completely clear what you are asking, but that appears to be because of language differencefor()
loop iteration variable after the end of the loop, it has to be defined outside of the loop. Defining a variable in the loop is a convenience for when you only need it inside the loop.