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
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.