Introduction: Intel® Edison Hands-on Day 7: Luminous Treasure Box

Luminous treasure box, the name sounds fun. And that would be real fun! We will make this box, which will be closed during the day, once in the night, its light will get brighter with the box opened slowly. Ha-ha...the inner workings of the box is that analog ambient light sensor can detect light strong or weak. With the brightness changing, the output values will be different. At night, to reach the set value, the servo will rotate, and LED turn on at the same time.

What you needed:

Step 1: Connection Diagram

TowerPro SG50 Servo → Digital 9

Analog Ambient Light Sensor → Analog 0

Digital Piranha LED Light Module - Red → Digital 3


Fixed the servo at the connection of the box, the LED in the box. Of course, the sensor is exposed to detect ambient light. After the installation is completed, place the box in the dark. And you can check if the box should open automatically.

Step 2: Example Code

#include

Servo myservo;

int LED = 3; //the number of the LED pin

int val = 0; //variable to store the sensor value

int pos = 0;

int light =0;

void setup(){

// initialize the digital pin as an output.

pinMode(LED,OUTPUT);

// initialize serial communication at 9600 bits per second:

Serial.begin(9600);

// attaches the servo on pin 9 to the servo object

myservo.attach(9);

// tell servo to go to position in 0 degree

myservo.write(0);

}

void loop(){

val = analogRead(0); // read the input on analog pin 0

Serial.println(val); // print out the value you read

//Once smaller than the set value,increasing angles

if(val<40){

pos = pos +2;

if(pos >= 90){ //After moverd to 90 degree, keep in this degree

pos = 90;

}

// tell servo to go to position in variable 'pos'

myservo.write(pos);

delay(100);

// As the angle increases, increased brightness LED

light = map(pos,0,90,0,255);

analogWrite(LED,light); //set the brightness

}else{

pos = pos -2; //minus 2 degree

if(pos <= 0){

pos = 0; //reduced up to 0 degrees

}

myservo.write(pos);

delay(100);

light = map(pos,0,90,0,255);

analogWrite(LED,light);

}

}