Make LED turn on if random value is true, off if value is false

Hello.

I am new to the Arduino system. I have done electronics for about two years now but I am just getting in to the Arduino programming language. By looking at a couple of the programs that come when you download the Arduino software, I was able to make out some of the code. This project that I'm trying to do makes the on board LED randomly light up or stay dark depending if the random number is 1 or 2. When I tried my code, the on board LED would always light up. I want to know if anything is wrong with my code.

void setup() {
  // put your setup code here, to run once:
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
int ran = random(1,3);
if(ran = 1) {
  digitalWrite(13, HIGH);
  Serial.begin(9600);
  Serial.println("1");
}

}

void loop() {
  // put your main code here, to run repeatedly:

}

if (ran == 1)

Yeah, as above.

"==" means "is equal to".

This is the one you want.

THe next point is your code only runs once...why not place it in the loop() and have a delay(2000); in there to see the effect?

void setup() {
  // put your setup code here, to run once:
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
Serial.begin(9600);
}

void loop() {
  byte ran = random(1,3);
if(ran == 1) {
  digitalWrite(13, HIGH);
  Serial.println(ran);
  delay(2000);
  digitalWrite(13,LOW);
    }
}

PS. One problem is the LED 13 is also the LED that lights up when Serial is being used...

Have you got another LED and a 200-400ohm resistor?

Johnny010: PS. One problem is the LED 13 is also the LED that lights up when Serial is being used...

On which boards? Not on an UNO for sure. It has a Tx light and an Rx light that flicker when sending or receiving Serial. But they have nothing to do with pin 13. The only thing that affects 13 is if you are using SPI where 13 is the clock signal.

Delta_G: On which boards? Not on an UNO for sure. It has a Tx light and an Rx light that flicker when sending or receiving Serial. But they have nothing to do with pin 13. The only thing that affects 13 is if you are using SPI where 13 is the clock signal.

Ooops yeah sorry! Ignore what I said.