I am trying to use this code to create a random number generator that will flash an led if it gets a certain number but it isnt working. It is only flashing the red led really fast constantly.
int randomnum;
#define RED 3
#define BLUE 4
void setup() {
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop() {
digitalWrite(BLUE,HIGH);
randomnum = random(50);
{
if (randomnum == 25);
{
Serial.println(randomnum);
digitalWrite(BLUE,LOW);
delay(20);
digitalWrite(RED, HIGH);
delay(10);
digitalWrite(RED,LOW);
}
}
}
When posting code, please use code tags.
Type
** **[code]** **
before your code
Type
** **[/code]** **
after your code
Also before posting code, use tools -> auto-format in the IDE to format your code. I've done it for you and the result is the below code window
int randomnum;
#define RED 3
#define BLUE 4
void setup()
{
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop()
{
digitalWrite(BLUE, HIGH);
randomnum = random(50);
{
if (randomnum == 25);
{
Serial.println(randomnum);
digitalWrite(BLUE, LOW);
delay(20);
digitalWrite(RED, HIGH);
delay(10);
digitalWrite(RED, LOW);
}
}
}
if (randomnum == 25);
The semicolon at the end ends the if block. So the below is always executed
{
Serial.println(randomnum);
digitalWrite(BLUE, LOW);
delay(20);
digitalWrite(RED, HIGH);
delay(10);
digitalWrite(RED, LOW);
}
Get rid of that semicolon and take it from there.
If you want so,etching to flash you have to turn it on and then off. A delay of 10mS is too small to see effectively at this space rate.