0

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.

asked Jan 8, 2020 at 19:10
4
  • upvote for well presented question ... this is a programming question, so the 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 difference Commented Jan 8, 2020 at 19:25
  • i just wanna generate random number, with range, maybe had another alternative sketch? Commented Jan 8, 2020 at 19:34
  • To use the for() 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. Commented Jan 8, 2020 at 20:31
  • how to upvote? so i need to make variable utside the loop? Commented Jan 8, 2020 at 20:50

1 Answer 1

2
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);
}
answered Jan 8, 2020 at 20:18
12
  • @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. Commented Jan 8, 2020 at 20:39
  • 1
    @MichelKeijzers, your function returns only the last value. the function number() should simulate values returned from a sensor Commented Jan 8, 2020 at 20:44
  • @Juraj ... thanks ... I thought that was the intention, but I guess the question was not so clear. Upvoted! Commented Jan 8, 2020 at 20:46
  • @UlinnuhaLuthfi (I removed my answer, this answer should answer your question already. Commented Jan 8, 2020 at 20:47
  • 1
    sorry everyone, i just realize how to upvote Commented Jan 8, 2020 at 21:03

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.