Room Mood Display

Step 1 - Build the Project

Use temperature, light, and sound sensors to measure the environment around you. Then display the values using the brightness of LED lights. What color is your environment?

Step 2 - Upload the Code

/* * Display the mood of your room by taking light, sound, and * temperature readings. */ float averageNoise;//hold the average of all the noise readings //Using a timer variable and millis() gets rid of delay long timer = millis(); void setup() { pinMode(A4,INPUT); //Center pin of sound trigger pinMode(A2,INPUT_PULLUP); //Light sensor //Check the temp sensor pinout, getting it backwards ruins it pinMode(A3,INPUT); //Center pin of temp sensor //One of the outside pins is in 5V, the other is in GND row //Get initial sound measurement to 'seed' the average averageNoise = analogRead(A4); //LED pins or a single multicolor LED pinMode(9,OUTPUT); pinMode(10,OUTPUT); pinMode(11,OUTPUT); } void loop() { //Compute the moving average of the noise level //Each new reading contributes 1% to the average averageNoise = 0.3*((float)(analogRead(A4))) + 0.7*(averageNoise); //soundValue is cast to a float so it can be multiplied by decimals /* Because the microphone needs to be read very often, * use millis to print only once every 250 ms*/ if (millis()-timer > 250){ //this loop runs every 250 milliseconds //converting reading to tempF with conversion equation const int tempF = int(0.88*float(analogRead(A3)) - 58.0); //Read the light sensor const int lightValue = analogRead(A2); //Map temp of 60-80 F to LED brightness of 0-255 analogWrite(10, map(tempF,60,80,0,255)); //Map light reading of 50-1000 to LED brightness of 0-255 analogWrite(9,map(lightValue,50,1000,0,255)); //Map sound reading of 200-800 to LED brightness of 0-255 analogWrite(11,map((int)(averageNoise),200,800,0,255)); timer = millis(); //Reset the timer to millis so difference is 0 } } //Change the map range for the light sensor (100-900) to make the // LED get brighter as the light level decreases // (c) 2017 Let's Start Coding. License:

Step 3 - Read the Walkthrough

At the top of your code, create a 'float' type variable called averageNoise. 

Next, create a 'long' type variable and name it timer. This timer allows you to take measurements at certain intervals without using the delay() command. Since delay() actually pauses the program, you can't do anything in that time period. The timer variable eliminates that.

In the setup() section of the code, set the pinMode of each of your sensors and LEDs. Ensure that you have the temperature sensor plugged in the correct orientation to + and - or the sensor will get very hot.

Here you'll set the value of averageNoise equal to the analogRead of A4. You will add each reading of the sound to this average in the loop, so you want to start with a typical sound reading, not 0. 

You're going to update the value of the averageNoise variable. Look at the first half of the equation. When you see float(analogRead(A4)), that's called a cast. You're telling the code "for this calculation only, treat the analog reading as a float variable". Doing that allows you to multiply analogRead(A4) by a decimal. Otherwise, an integer variable type will just cut decimals off the end of the value. Once you have cast the reading as a float, you multiply it by 0.3. 

In the second half of the equation, you multiply averageNoise (already a float) by 0.7. In effect, you are saying "The new value of averageNoise should be made up of 30% of the latest reading and 70% averageNoise.". You can change the impact of any new reading by changing the decimals attached to soundValue and averageNoise.

averageNoise is updated very quickly, once per loop. The rest of the loop sits inside an 'if' statement that runs only once per 100 milliseconds. How does this work? The 'if' statement compares the value of millis() and the value of the variable timer. If the difference between them is more than 100 milliseconds, the 'if' statement is true. Otherwise, the 'if' statement is false. Remember that millis() is a built-in timer in the Arduino software and it is always counting up, which is why you don't need to create it as a variable. 

Inside the 'if' statement, you'll create an integer variable called tempReading and set it equal to the value of analogRead(A3);. Next, you will create a variable tempF that uses the tempReading variable. The numbers in this equation are specific to this sensor, don't change those. However, you can see another variable cast in this line. You temporarily make tempReading a float variable so that you can multiply it by the decimals. On the very outside of the parentheses, though, you can see 'int'. That means that after your calculations are complete, you actually re-cast the value to an integer. tempF ends up as an integer.

Taking the light reading is much simpler. You simply create an integer variable called lightValue and set its value to analogRead(A2). 

Once all of the readings are taken, you can write them to the LEDs. analogWrite needs two arguments: the pin number and the brightness value. Brightness values can range from 0-255, but something like temperature won't range that much. In that case, you use the map function to map 60 degrees to 0 brightness and 80 degrees to 255 brightness. The same goes for the lightValue and averageNoise. You can change the first two numbers within map to see the effect of changing the mapped range.

Outside of the 'if' statement, you reset the timer variable to millis(). That means that the next time through the loop, the difference between millis() and timer won't be greater than 100 and the light and temperature readings will wait until another pass to be updated.