-1

Total Arduino noob, I come from a Python "background", though I barely know that, either.

I'm writing a super simple script that will turn on a bulb when the random number generator generates a number greater than 100. Obviously, I have a syntax problem or something: What am I doing wrong? Secondly, How quickly does random query numbers? Is it possible to slow it down? I don't need my bulb turning on 50 times a second.

Thanks! And please be kind; I know this is Googleable, I just lack the prerequisite knowledge to Google this efficiently.

/*

*/
int relayPin = 7;
int randomNumber = random(1001);


void setup() {
  pinMode(relayPin, OUTPUT);

   
}

void loop() {


if(randomNumber > 100)
  digitalWrite(relayPin, LOW);

else{
   digitalWrite(relayPin, HIGH);}
   
   
}``` 
7
  • 1
    what is the problem?
    – jsotola
    Feb 1, 2022 at 1:05
  • btw, put the ``` on a separate line so that it is invisible
    – jsotola
    Feb 1, 2022 at 1:06
  • The problem is that the relay module does not switch. I assume that there is an error in my code.
    – Tooluser
    Feb 1, 2022 at 2:35
  • 3
    yes, logic error ... ask yourself when is the value of randomNumber updated?
    – jsotola
    Feb 1, 2022 at 3:27
  • The current code (if you move the random number generation into loop()) will switch not just 50 times, but thousands of times per second. Look at some example code to get an idea about how to solve that.
    – StarCat
    Feb 1, 2022 at 6:55

1 Answer 1

1

There are a few things to be discussed in here.

First, the random number generation: in fact, the arduino does not generate random numbers. It has a list of numbers on its memory that just "seems" to be random. That's why it's called a pseudo-random number. So, you need to declare a starting point on this sequence, from which you will get the "random" numbers. This is made with "randomSeed()" in void setup. For example:

void setup() {
  // put your setup code here, to run once:
  randomSeed(analogRead(0)); //starts the pseudo-random sequence in a random point,
                             //in this case, the analogRead value of an unconnected pin
}

Next, let's generate our number. The variable can be global (as you declared it outside any function), but you must declare it with "random()" only inside "void loop". Otherwise, its value will be assigned just one time (that's what your code is telling it, to do it once).

Here is what I would do:

int relayPin = 7;
long randomNumber; //just declares the variable; it must be type "long"

void setup() {
  // put your setup code here, to run once:
  randomSeed(analogRead(0)); //starts the pseudo-random sequence in a random point
  pinMode(relayPin, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  randomNumber = random(0, 301);
  //each time the code loops, it assings another value to randomNumber
  //in this case, the number will be between 0 and 300 (301 is the excluded limit)
    
  if(randomNumber > 100){
    digitalWrite(relayPin, HIGH);
  }
  else{
    digitalWrite(relayPin, LOW);
  }
  delay(500); //half a second delay
     
}

If you want to have more details about how random numbers are generated in the Arduino, visit the Arduino language Reference: https://www.arduino.cc/reference/en/

1
  • 1
    Thank you for this! This explanation was very kind and helpful of you! I will implement this after work.
    – Tooluser
    Feb 1, 2022 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.