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

Blog 8 – Energy Efficient Lighting

Revelations

As we have been stuck on the sensor part of the code for a while, we decided to have a meeting with our lecturer, the result of which is the following.

Instead of trying to make the code read sequentially, we have used interrupts to trigger certain events in the code instead. When the interrupts are triggered, the Interrupt Service Routines (ISRs) that run are the functions sensor1Flag and sensor2Flag, which set the time1 and time2 variables and, through a boolean operation, allow the if() statement controlling the behaviour in the main loop to run.

We have also added a reset() function that runs after the lights(personCount); line has run, which resets all the time time difference variables to 0 to ensure we don’t see strange behaviour when the loop runs again. This function also ensures that personCount doesn’t go below 0 as that would give us incorrect behaviour.

Here is the code:

https://create.arduino.cc/editor/JackGormley/78cb0c1c-fb50-4992-bb67-ffad77e321b1/preview?embed

 

Integration with switch

Now that the sensors part of the code is working, we integrated it with our existing code for using a manual override switch. The following code runs much like the original toggle switch code, except when it triggers case 2 it calls the function automation()  which contains all the code we had in the loop for the previous code on this page. case 1 and case 3 now do a digitalWrite() to the ledPin for either HIGH or LOW respectively as well as output to the serial “ON” or “OFF”.

https://create.arduino.cc/editor/JackGormley/a705ebcd-61a2-4248-aea2-bc7411042bd1/preview?embed

 

Video

This is the code working on a setup that is a combination of the sensor setup using pins 2 and 3; and the switch using pins 12, 11 and 10.

 

Blog 7 – Energy Efficient Lighting

Three way toggle switch

We want to incorporate a manual override in our light switch so that a user can manually set the lights to be on or off. To do this we figured a three-way toggle switch would be suitable, as it provides us with a tactile feedback for the user as well as three states: on, automated and off.

Testing

Upon receiving our switch we initially had difficulties figuring out how to wire it up. we had assumed that the two separated terminals would take the input current and the switch would route this through one of the three remaining pairs of terminals. While partially correct, after testing for continuity with a multimeter, we discovered that in fact the first set of terminals in the cluster of three pairs was our input. This is illustrated in the diagram below.

New_20Doc_202018-04-07.jpg

Code

After some initial tests with routing current to three separate LEDs, we decided to try and see if we could get the software to recognise when we operated the switch and then assign that to a variable. The following code uses if statements to check which of the three input pins is being turned on by the switch, before assigning a value between 1-3 to the variable state, which is then passed to a switch: case: statement and prints out a corresponding bit of text:

https://create.arduino.cc/editor/JackGormley/28b64c44-bcf5-4d51-9fb7-29c3f85c7364/preview?embed

Here is a frizting diagram for the setup used with the above code:

Toggle_switch.jpg

The previous video shows the three switch options being read. The video below shows the development of using the 3 way toggle switch, to have an output. Three different coloured LEDs have been added to show different switch options. The next step will be incorporating the IR sensors.

 

Blog 5 – Energy Efficient Lighting

Code using millis() VER1

Since our last iteration of code was a bit unreliable in terms of increasing personCount the correct amount, we’ve decided to try and use the millis() function to eliminate our use of delays and make our code more robust.

The following code starts a currentTime variable and assigns it to millis(), giving us a clock of how long our code has been running. When a sensor is triggered, a time variable is assigned the currentTime value. The idea is that when both sensors have been triggered, the operation time1 – time2 (sensor1  and sensor2 readings) would result in either a positive or a negative number, which would correlate to either increasing personCount or decreasing it. Here is this code:

https://create.arduino.cc/editor/JackGormley/3346c681-5550-4b09-a376-6e4c2a4aa378/preview?embed

 

Issues

The problem with this Code_Using_millis_VER_1 code is that no matter what direction the motion is going, personCount is increased no matter what. Additionally, the code will increase personCount as quickly as the loop is run, for as long as the sensor is being triggered, resulting in many tens of increases in just fractions of a second.

 

Code using millis() VER2

To resolve this we attempted to run our code in a for() loop that would only run the code every second:

code with millis snippet

 

Issues

While this provided a slower increment of personCount which was more accurate in testing, it still could only increase personCount and not decrease it.

We do not noe de wey.

 

 

Blog 3 – Energy Efficient Lighting

Given that we have done some research into our concept and found that it is worth pursuing, this blog entry details our first attempt at creating our first prototype.

The automatic light concept that we have in mind will need to control the number of people within a room. Without using a motion sensor, Infrared Sensor Module are an effective method of doing this. The sketch below is an expected layout of the products environment, a door with a light switch on the right, against a perpendicular wall.

 

Sketch

Sensors:

media-20180319.jpg

The above is an Infrared Sensor Module which consists of an IR emitter (dark diode) and an IR reciever (clear diode). It has three pins out, which are VCC, hooked up to a 5V rail; GND, going to ground; and OUT, which gives out the reading of the sensor.

Two of these sensors will be used next to each other to determine direction of the movement.

161-00.jpg
https://www.adafruit.com/product/161

The above is the photo-resistor (LDR) mentioned at the end of the previous blog.

 

The Setup:

2Sensors_test.jpg

The above image shows the setup with two infrared barriers connected to digital pins 2 and 8. An LED is connected to pin 13 as it has a built in resistor.

 

The Code:

https://create.arduino.cc/editor/JackGormley/09da655a-1fbb-48af-9977-6547150b32d8/preview?embed

 

The code is supposed to work by using if statements to increment the variable personCount based on which sensor is activated first. personCount is then given to the lightCount() function which either turns the LED on or off depending on the value.

In practice we found that the code would increment and decrement the personCount variable and this would either turn on or off the LED, however there were some issues.

First off the personCount variable had the ability to go into the negatives, which could potentially lead to inaccurate counting of people in a room. This was fixed by adding && personCount > 0 in the following line of code that handles decreasing the value of personCount:

Capture

The second issue we were having was that if only one of the sensors was triggered it would increase the personCount variable very quickly, often ending up many tens higher or lower than intended. We are still working on this issue, but have had some success with incrementing the personCount variable. By creating the increment command inside of a nested if() statement, we were able to make sure that the variable is only incremented when both sensors are triggered, first sensor 1 and then sensor 2:

nested if

While this made the increment scenario work acceptably, it completely broke the decrement scenario, where the variable would no longer decrement. We tried reverting this if() statement to its original structure of not being nested, which allowed the personCount variable to temporarily be decreased, however it would then return to its previous un-decremented value.

decrement not work.PNG

We think this has to do with it then running the increment scenario if() statement right after the delay, but are not sure.

 

Video:

 

 

RGB & Flame Combo – Jack Gormley & Emily Mortimore

Continuing on from last week.

After the mild success of last week where we managed to get the RGB light to change colour based on the readings from the flame module, we were prepared to add to this concept with other outputs.

We decided for our final demonstration to combine our components with a DC motor and a stepper motor. Our idea was to use the stepper motor to bring a lit candle closer to the sensor, while displaying a visual alert through the use of the RGB LED, which once it gets to a suitable range is blown out by a DC motor – driven fan.

 

Conceptualisation

After a few rough sketches and measurements of components, we had a concept that would achieve our desired result.thumbnail

CAD

From this, we went on to CAD up parts we needed to 3D print in Solidworks.

tea-light-cad.pngenclosure-cad.png

 

Code

Taking inspiration from our previous code, we attempted to use switch case statements to replicate our results, however we discovered that we were indeed trying to use old code with new hardware, so we rewrote the code using while and if statements as in the example below:

https://create.arduino.cc/editor/JackGormley/a705ebcd-61a2-4248-aea2-bc7411042bd1/preview?embed

 

Which we quickly learned didn’t work. Rewriting the code (with the exemption of nested if statements) a couple iterations later yielded the following code using if and else if statements with greater success:

https://create.arduino.cc/editor/JackGormley/a705ebcd-61a2-4248-aea2-bc7411042bd1/preview?embed

This code allowed the step motor to turn once the sensor detected a flame and turned the LED red when fire was detected, defaulting to green when there was no flame present.

Week 1 – RGB Module

KY-009_RGB_full_color_LED_SMD_arduino_module-240x240

 

An RBG Light Emitting Diode (LED) with four pins, Red, Green, Blue and ground. The three colour pins correspond to three different coloured LEDs, ground goes to ground.

As the diodes are of the three primary colours, any colours on the RGB spectrum can be achieved by altering the values sent to the diodes.

 

RGB MOd_2.jpg

The Pins

The pins take values ranging from 0-255, which controls how much of the emitted light is a particular colour, 0 being no light, 255 being full brightness. Pins need to be connected to PWM ports on a board in order for a range of values to be sent to them.

The LED is called a 5050, as those are the dimensions of the LED itself.

 

The Setup

The three input pins require resistors much the same as any LED would, some RGB Modules have these built in, but this one does not, so I’ve added my own. You will need:

  • 1X Arduino Uno
  • 1X Breadboard
  • 1X RGB Module
  • 3X 560 ohm resistors
  • 4X wires

Fritzing.jpg

The Code

Embedded here is the code i used to display the three primary colours, as well as a step in colour between them.

 

https://create.arduino.cc/editor/JackGormley/a705ebcd-61a2-4248-aea2-bc7411042bd1/preview?embed

 

I created a function called “setColour()” which takes three values you give it and analogWrite s them to the red, green and blue pins.

This function is called in the loop() function and is written with delays of one second between colour changes.

 

Video