Random functions in Arduino

In this Project, we will see about how to use Random Functions in Arduino to control LEDs .

Circuit Diagram:

Arduino Code:
random_simple_code.ino
//This Code is Developed by Sdev
//Follow Us Here : https://youtube.com/sdevelectronics

void setup() {
  Serial.begin(9600);
}

void loop() {
  int rand_num = random(100);
  Serial.println(rand_num);
  delay(500);
}

random_led_code.ino
//This Code is Developed by Sdev
//Follow Us Here : https://youtube.com/sdevelectronics

#define led_r 4
#define led_g 5
#define led_b 6
#define led_y 7

void setup() {
  Serial.begin(9600);
  pinMode(led_r, OUTPUT);
  pinMode(led_g, OUTPUT);
  pinMode(led_b, OUTPUT);
  pinMode(led_y, OUTPUT);
}

void loop() {
  int rand_num = random(20, 24);
  Serial.println(rand_num);
  if (rand_num == 20) {           //RED
    digitalWrite(led_r, HIGH);
    digitalWrite(led_g, LOW);
    digitalWrite(led_b, LOW);
    digitalWrite(led_y, LOW);
  }
  if (rand_num == 21) {           //GREEN
    digitalWrite(led_r, LOW);
    digitalWrite(led_g, HIGH);
    digitalWrite(led_b, LOW);
    digitalWrite(led_y, LOW);
  }
  if (rand_num == 22) {           //BLUE
    digitalWrite(led_r, LOW);
    digitalWrite(led_g, LOW);
    digitalWrite(led_b, HIGH);
    digitalWrite(led_y, LOW);
  }
  if (rand_num == 23) {           //GREEN
    digitalWrite(led_r, LOW);
    digitalWrite(led_g, LOW);
    digitalWrite(led_b, LOW);
    digitalWrite(led_y, HIGH);
  }
  delay(2000);
}

Youtube Video Tutorial:




Download our official Android App in PlayStore. Click Here
You can get the all the required files (like Circuit Diagram, Arduino.ino file, Libraries Used, and others) for the project  in ZIP format and much more...

No comments:

Post a Comment