Music Box Light Control
Language: | English • 中文 |
---|
ContentsObjectiveWe build a music box here. You can turn to different songs by opening the door of the box and shut the door to stop the music. PrincipleWe can detect the door's open or close via light-sensitive sensor. When opening the door, light is intense and it will trigger the change of songs or by closing the door, the music will be stopped. Equipment
Preparation
Download program examples: music_box_light
Software DebuggingCode description: One part for detecting light intensity and the other part for sending serial instructions to control Audio playing, pause or cut according to brightness.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
#define AUDIO_PORT mySerial
//#define AUDIO_PORT mySerial
Define pins of light-sensitive detection. #define light_cm 300
#define light_pin A0
if ( light_val > light_cm && !change)
{
if (millis() - time1 > 100)
{
light_val = analogRead(light_pin);
if (light_val > light_cm)
{
change = true;//True means the "if" condition will execute once.
off = false;
audio_down();
//audio_choose(1);
//audio_play();
}
time1 = millis();
}
}
if ( light_val > light_cm && !change)
{
if (millis() - time1 > 100)
{
light_val = analogRead(light_pin);
if (light_val > light_cm)
{
change = true;//True means the "if" condition above can execute one time.
off = false;
audio_down();
//audio_choose(1);
//audio_play();
}
time1 = millis();
}
} Hardware Debugging
ResultOpen the music door and you can cut songs or close the door and stop the music. You can also build a beautiful LEGO shell since mCookie can be stacked with LEGO easily. Video |