LED Constellation

Since I learned about digital input and analog input last week, this week I decided to build upon on my star constellation project and add functionality to it using an Arduino. For this project, I built my circuit using an Arduino Uno, one button switch, and 9 LEDS. The schematic for the circuit is found below.

My first version of this project included a push switch that allowed all the LEDs to light up only while the button is pressed. For my second version, I wanted my button to act similar to an on and off switch. The first time it is pressed the lights will turn on and the second time the lights will turn off. You no longer have to press down continuously to have the LEDs light up. The second feature I wanted to add was to make the LED’s randomly light up. I also wanted to fix the 9th LED light so that it shined as less than before since it is wired in parallel with 5 volts.

As you can see from the gif above, I was able to achieve my goals. One thing to note is that I had to press the button twice to turn it completely off. This is do the fact that I included a delay() in my Arduino code to slow down the rate of flickering of the lights.

Here is the code:


const int button= 6;
int myPins[] = {11, 10, 9, 8, 7};
int brightness = HIGH;
int decide= 0;
int reading;
bool on=false; 
bool first= true;

void setup() {
   // initialize serial communications at 9600 bps:
    Serial.begin(9600);
    // declare the led pin as an output:
    pinMode(button, INPUT);
   for (byte i = 0; i < 5; i = i + 1) 
   {
    pinMode(myPins[i], OUTPUT);
   }

}

void loop() {
  //Serial.println(decide);

 //is button pressed
 reading = digitalRead(button);

 //Serial.println(pressed);
 //Serial.println("button");
 Serial.println(reading);
 //Serial.println("on");
 Serial.print(on);
 if (reading == 1 && first == true)
  {
   first=false; 
   on = true;
  }
  else if (reading ==1 && first == false)
  { 
   first=true;
   on = false;
  }
  else
  {
    
    on = false;
  }

  if (on==true){
    // turn on LEDS
   for (byte i = 0; i <5; i = i + 1) 
    {
      int decide = random(0,2);
      if (decide==1)
      {
        digitalWrite(myPins[i], HIGH); // on
      }
      else
      {
        digitalWrite(myPins[i], LOW);   // of
      }
      delay(100);
    }
  }

}

Posted in: Physical Computing

Tagged as: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s