Blog 10 – Energy Efficient Lighting

Integration

This blog entry contains the amalgamation of all of our code so far as well as some additional functions used to display data on the LCD.

Combining our codes for the different components proved to be challenging as our structure required reworking, as well as writing new code to be integrated.

Firstly,  we thought that our automation() function was required to pass the personCount variable to the new score() and screen() functions, in order for them to work out whether they need to be adding and printing scores to either the player or the cpu. This turned out not to be the case.

Secondly, this caused our score to increase continuously as personCount would remain zero after someone left the room. As personCount remained zero the code was stuck in a loop and unable to exit the loop to run the code that changes the value of personCount.

The code with these problems in below:

Code

https://create.arduino.cc/editor/JackGormley/44752454-f8b2-4d93-a27f-17fae9694a0b/preview?embed

 

Further Revelations

After some guidance as to the structure and placement of our boolean flags, the code finally functions as intended.

Our loop() function now runs a couple of if() statements and a switch case: statement to check on the input from the three way switch and run the correct set of functions accordingly.

In the “ON” position, the light is forced on and the lcd.off() function is run, which turns off both the lcd backlight and the lcd itself. The automation() code is still called as we want to always keep track of personCount.

In the “AUTO” position the lcd.on() – turns the lcd back on, automation(),  lights(personCount) – which allows the lights to be influenced by  number of people in the room, score()  and screen(pPercentage, cpuPercentage). The latter two functions deal with calculating the score percentages and with printing the information on the lcd.

In the “OFF” position, the lcd is turned off, automation(), score() and screen() are all run so that it can accurately increase cpuScore even when the screen is off.

score()

This function contains an if() statement that only runs when two conditions, isRoomEmpty = true and wasRoomEmpty = false have been met. These two conditions are set in the automation() function inside the if() statement that only runs when the sensors are triggered. This prevents the score() function from getting stuck in a loop once personCount hits zero. Based on the current position of the switch and the two booleans, it will either add a point to playerScore or to cpuScore.

Using these two variables, this function then uses a map() to work out what percentage of the total times the player and the cpu have won.

screen()

This function then takes the pPercentage and cpuPercentage from the score() function and prints them in opposite corners of the display followed by a ‘%’ symbol.

Tone

We had mentioned in an earlier blog that we were thinking of including a little piezo element to make a short audible cue when the cpu gains a point, in order to make the user aware when the cpu scored and encourage them to remember they are being scored against and thus turn off the light when they leave a room. This is small function playTune() at the end of the code is called in the score() function after the cpu scores a point.

playTune pic

Revised Code

https://create.arduino.cc/editor/JackGormley/f8bba017-3928-4212-a159-f3141c624d56/preview?embed

 

Circuit Diagram

Final-ish_bb

 

Video

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s