lukabafoil
Published © GPL3+

Random Light Display

A simple Arduino circuit which picks random lights, and turns them on and then off.

BeginnerProtip7,722
Random Light Display

Things used in this project

Hardware components

LED (generic)
LED (generic)
As many or as few Leds can be used, but the more there are the better the end result looks
×5
Arduino UNO Wifi Rev.2
Arduino UNO Wifi Rev.2
×1
Switch Sealing Boot, Button Operators
Switch Sealing Boot, Button Operators
any other button works, though the wiring may vary based on the button
×1
9V battery (generic)
9V battery (generic)
×1
9V Battery Clip
9V Battery Clip
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Circuit layout and connection

This is a screenshot of the basic circuit design, though it can be edited and changed to accommodate for different buttons or LED's

Code

Random Light Display

Python
The final code for the project, with annotations attempting to explain the process of the program
void setup () { 
  for (int i = 0; i > 13; i++) {
    pinMode(i, OUTPUT) ; // setup all of the neccesary LED pins
  }
  pinMode(2, INPUT) ; // setup the button pin 
  Serial.begin(9600) ;  // setup the serial for debugging 
}
void LightSequence() {
  int randomNum = random(10, 14) ; // pick random light to turn on. 
  // the lowest variable is the lowest light that turns on, and the highest is
  // the one above the higheest light, so that can be adjusted for more or 
  // less lights 
  Serial.println(randomNum); // print the chosen light in the serial 
  digitalWrite(randomNum, HIGH) ; // turn on the randomly chosen light 
  int randomTime = random(500, 3000) ; // pick a random amount of time to wait, the lowest being the minimum and the highest being the maxium
  Serial.println(randomTime); // print the chosen time in the serial 
  delay(randomTime) ;  //wait the random time 
  digitalWrite(randomNum, LOW) ; // turn off the light 
}
void LightDisplay(){
  LightSequence() ; // trigger a random light, and wait for it to turn off
  LightSequence() ; // repeat ten times, meaning ten lights will turn off and on 
  LightSequence() ; 
  LightSequence() ; 
  LightSequence() ; 
  LightSequence() ; 
  LightSequence() ; 
  LightSequence() ; 
  LightSequence() ; 
  LightSequence() ; 
  Serial.println("End"); //print end in the serial to show when the sequence has ended
}
void loop () {
  int button = digitalRead(2) ; //setup the button's value to be used
  if (button == 1) { //when the button is pressed
    LightDispay() ; //trigger the light sequence of ten lights
  }
}

Credits

lukabafoil
0 projects • 0 followers

Comments