For this week myself and Richard Channing created a smiling customer tips box. It’s composed of the heartbeat sensor from my week 2 post, the relay from Richards week 2 post, the led array from week 3, and a motor, as well a resistor and 2 LEDs.
The code for this project got quite complicated quite quickly. We used 1 library (the ledcontrol library from github previously mentioned). as well as 7 global variables, 2 arrays of
byte variables, 1 of our own functions (
else statements, and 2
for loops. For a total of 68 lines of code.
The printFace(bool) function:
Rather that re-typing the
for loops every time we want to change the LED array’s display, I’ve given it’s own function with 1 parameter; doesSmile. If I were to type:
Then the led array would be set to display the smile. (the for loops are explained in greater depth in week 3’s post).
The main loop():
The main loop is where all the action happens:
Initially we we’re going to keep the LED array and motor on by using the
millis() function. However instead we decided to use
delay(), to avoid complication if a second coin were to be inserted before the timer had reached 2000 milliseconds.
Next we read the pin for the IR emitter/receiver (heartbeat) sensor. and stored this in the int sensorValue which was globally defined. If the value read by the sensor is significantly higher than the background infrared noise, then a series of events happens:
- The time of the event triggering is stored to lastRun.
- The LED verification pin is triggered (so I know if the array isn’t working).
- The LED array is changed to a smile.
- The relay is switched on.
- A 2 second delay is triggered.
else statement the LED verification pin, LED array, and relay are all switched off.
In our circuit we used the relay to close our second circuit with the motor and 9V battery. The LED array took 5V from the Uno, the heartbeat sensor also. Occasionally the array glitches out because it doesn’t get a high enough current from the board.
Initially we had a big problem with the relay switch. After one or two clicks while attached to the motor it would get stuck in the closed position. Reasons to this could have been the back e.m.f. (electro magnetic force) generated by the motor, so we stuck in a diode on the output of the switch, however this didn’t seem to change anything apart from making the behaviour of the switch more predictable. Every fourth click of the relay it was still getting stuck.
We concluded that the back e.m.f. must be greater than the peak inverse voltage (PVI). We added in a second diode (this time an led) and the circuit now works perfectly.
The rest of the components were fairly easy to use. The heartbeat sensor had 3 pins, +, -, S (signal). The led array was a little more complicated with 5 pins, VCC, GND, DIN, CS, and CLK. These 3 pins create a Serial Peripheral Interface Bus (SPI), this is a serial connection to the MAX7219CNG chip on the array board which controls the array.