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.
1 Answer 1
int sensor = 900; //variabel sensor
void setup() {
Serial.begin(9600);
while (!Serial);
randomSeed(analogRead(A0));
}
int number() {
int a = random(0, 10); //range pengurang nilai sensor
int d = random(500, 1500); //range delay perubahan nilai
if (sensor < 670) {
sensor = 900;
}
sensor -= a;
delay(d);
return sensor;
}
void loop() {
int val = number();
Serial.println(val);
}
I modified the delay for test.
randomSeed ensures different random values at every run
EDIT: a not blocking version which returns the same values for random interval:
int sensor = 900; //variabel sensor
unsigned long nextChangeMillis = 0;
void setup() {
Serial.begin(9600);
while (!Serial);
randomSeed(analogRead(A0));
}
int number() {
if (millis() < nextChangeMillis)
return sensor;
int a = random(0, 10); //range pengurang nilai sensor
int d = random(500, 1500); //range delay perubahan nilai
if (sensor < 670) {
sensor = 900;
}
sensor -= a;
nextChangeMillis += d;
return sensor;
}
void loop() {
int val = number();
Serial.println(val);
}
-
@Juraj Why did you kind of copied my answer? Btw, you don't need to return sensor in number(), since it's a global variable anyway.Michel Keijzers– Michel Keijzers01/08/2020 20:39:03Commented Jan 8, 2020 at 20:39
-
1@MichelKeijzers, your function returns only the last value. the function number() should simulate values returned from a sensor01/08/2020 20:44:06Commented Jan 8, 2020 at 20:44
-
@Juraj ... thanks ... I thought that was the intention, but I guess the question was not so clear. Upvoted!Michel Keijzers– Michel Keijzers01/08/2020 20:46:10Commented Jan 8, 2020 at 20:46
-
@UlinnuhaLuthfi (I removed my answer, this answer should answer your question already.Michel Keijzers– Michel Keijzers01/08/2020 20:47:37Commented Jan 8, 2020 at 20:47
-
1sorry everyone, i just realize how to upvoteUlinnuha Luthfi– Ulinnuha Luthfi01/08/2020 21:03:57Commented Jan 8, 2020 at 21:03
Explore related questions
See similar questions with these tags.
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.