Yunho Lee Analog&Digital Switch – Christmas Tree Decoration

Concept and Inspiration

The inspiration for the project is Christmas tree decorations that have LED light bulbs blinking to make the tree look pretty.

There are one analog and one digital input that the circuit includes.

Digital – The red digital button switch chooses the blinking mode of the LED lights. (1 – off, 2 – on, 3 – blinks)

Analog – The potentiometer’s input decides the speed of the blinking of the LED lights and also the speed of the carol being played from the speaker.

How I implemented the Carol to be played in the speaker – First, I made an array of melodies where “0” indicates MUTE and from 1 to 11 each means a note. As time passes, the index will move on to the next element in the melodies array, playing the music as it is written in the melodies array.

The code I used is inserted below.

  • I have bad connections with the blue lightbulbs, so it sometimes turns off in the video.
  • I was going to make mode #4 that automatically turns the light bulb on and off depending on the light in the room, but I think I broke my light sensor while bending it, and was not able to make it happen.
#include "pitches.h"

int melody[] = {
  5,5,5,6,5,5,3,3,3,0,0,0,5,5,5,6,5,5,3,3,3,0,0,0,9,9,0,0,9,9,7,7,7,0,0,0,8,8,0,0,8,8,5,5,5,0,0,0,6,6,0,0,6,6,8,8,7,7,6,6,5,5,5,6,5,5,3,3,3,0,0,0,6,6,0,0,6,6,8,8,7,7,6,6,5,5,5,6,5,5,3,3,3,0,0,0,9,9,0,0,9,9,11,11,9,9,7,7,8,8,8,8,0,0,10,10,10,10,0,0,8,8,5,5,3,3,5,5,5,4,2,2,1,1,1,1,1,1,0,0,0,0,0,0
};

int notes[] = {
  NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5
};

byte prevButtonState = LOW;
byte patternNum = 0;
unsigned long timer = 0;
unsigned long timer2 = 0;
unsigned int index = 0;

bool redOn = LOW;
bool yellowOn = LOW;
bool greenOn = LOW;
bool blueOn = LOW;

void setup() {
  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(12, INPUT);
  Serial.begin(9600);
}

void loop() {
  byte buttonState  = digitalRead(12);

  int lightVal = analogRead(A0);

  int mappedPotValue = map(lightVal, 0, 1023, 150, 500);
  Serial.println(mappedPotValue);
  
  if (buttonState == HIGH && prevButtonState == LOW) {
    if (patternNum<2){
      patternNum++;
    }
    else patternNum = 0;
  }
  prevButtonState = buttonState;

// Music play code
  if (millis() > timer) {
    timer = millis() + mappedPotValue;
    if (melody[index] != 0){
      tone(5, notes[melody[index]-1], mappedPotValue+20);
    }
    else noTone(5);
    index++;
    if (index == 144) index = 0;
  }
// Light bulbs Control
  if (patternNum == 0){
    redOn = LOW;
    yellowOn = LOW;
    greenOn = LOW;
    blueOn = LOW;
  }
  else if (patternNum == 1){
    redOn = HIGH;
    yellowOn = HIGH;
    greenOn = HIGH;
    blueOn = HIGH;
  }
  else if (patternNum == 2){
    if (millis() > timer2) {
      timer2 = millis() + mappedPotValue;
      redOn = !redOn;
      yellowOn = !yellowOn;
      greenOn = !greenOn;
      blueOn = !blueOn;
    }
  }
  else {
    redOn = LOW;
    yellowOn = LOW;
    greenOn = LOW;
    blueOn = LOW;
  }

  digitalWrite(8, redOn);
  digitalWrite(7, yellowOn);
  digitalWrite(4, greenOn);
  digitalWrite(2, blueOn);
}

 

Leave a Reply