Using sound to control light
Last time we learned how to set up light control using illumination sensor. This is a nice beginner program to get your basics down, but I bet you want to learn more? This lesson will cover Boolean logic as well as if statement covered last time. We will build a system that will turn the lights on and off when you clap your hands. This has more uses than the one we looked last time since now you have a full control over the lights.
To get things up and running, connect Robbo Lab to your laptop/PC, run IDE and select COM/board as usual
We covered this on the last lesson so let’s not waste too much time here.
To get this project done we will need a LAD and a sound sensor. They are located on ports numbered 5 and A3 correspondingly on Lab. You are partially correct if you think that “if” statement will be used here. Yes, it makes sense to write “if sounds level more than a given value, then turn the lights on”; however we will not be able to switch the lights off afterwards by clapping. To do so, Boolean algebra comes into play.
Let’s look closer. Boolean statuslamp can have only 2 values: “true” or “false” for lamp being on and off respectively. Since our lamp (LED) is turned off when we write the program, line 12 states “statuslamp=false” .
if(analogRead(A3)>60)
{
statuslamp=!statuslamp;
digitalWrite(5,statuslamp);
delay(20);
}
means that after we clap this value rewrites itself by the opposite value, so if the lamp was off and we clapped, lights go on, but if LED was already on, after a clap they will be turned down. NOTE that you will need to adjust mic’s sensors’ readings (most likely you will get different value than 60 in your room) “Delay” is only to set up the delay between claps so that there will be no sudden noise that is happening to be taken as a factor by the board. This is the beauty of Boolean statements, you do not need to write long lines of code to program something to be done, you simply rewrite the value in the memory of the board and proceed further.
You can play around with numbers (but not the ports’) so that you will find the one suitable for you. After all of this, you are set and can do personal sound light control!
This is the fourth blog post brought to you by ROBBO to help you to become a pro robot hacker. Learning has never been so interactive!