In this lesson, we are going to make a smart alarm clock. Leave it by your window, and when the first light of the morning comes in, it will ring to wake you up.
The photoresistor can change its resistance value with the intensity of the light. The stronger the light is, the smaller the resistance value will be. When the intensity of the light reaches a certain degree, the buzzer sounds.
After creating the project, click Create variable button to create 6 variables: btn, btnVal, buz, flag, pho, and phoVal respectively.
Assign the values of pins to each variable at the beginning. The button is connected to D0 and we set it into input pull down. And we initialize the value of the flag to 0.
The values of btn (button) and pho (photoresistor) are assigned to btnVal and the
phoVal variable respectively.
A print block is put below the set phoVal to block to help print the photoresistor’ s value.
Drag an if do block and an and block into the Forever block.
Create two judgement conditions. Drag 2 Comparison Operator ( = ) blocks from the Logic category to the and block. Furthermore, change values as follows. When the conditions on both sides of and are met, code in do is executed.
Put a set pwm P0 frequency to block and two set pwm P0 pulse width to 0% blocks into the do block, then place 3 buz blocks into the the slot of pins.
Drag the Low C block from the Music category to the set pwm P0 frequency toblock, then add 2 delay blocks to set the beats to 1/8.
Add another if do block below the if do block to create another judgment condition that if the button is pressed, set flag to 1, then the system will not enter the first if do block again, and the buzzer will not ring.
Save the project and flash it into the Raspberry Pi (refer to lesson 0 for details). We can simulate a night environment by blocking the light from the photoresistor by hand to test whether the circuit works well.
Take out the matching cardboard of lesson 5.
Fold a cube like this, according to the creases.
Build the circuit as follows. Please DO NOT change the position of components optionally when building circuits, in case the card cannot be installed.
Mount the cube of cardboard on the circuit as shown.