Medicine Door Trigger¶
Accelerometer¶
To identify when the medicine door opens to play the motivational voice recording, we tried using an accelerometer. This device is useful because it can accurately detect its orientation in 3 axes. It also uses I2C communication which can help mitigate the number of pins needed.
We used proprietary wires that came with the device that represent the SCL with yellow and SDA with blue. Additionally, we desoldered the pins that were attached on the device in order to fit it inside the medicine door.
The device worked perfectly at first. We used example code to print out the accelerometer readings and observed which readings were changing and to what value when we opened and closed the door.
We found that the acceleration in the y axis was the most reliable method of determining when the door opens, specifically when a.acceleration.y < -5
.
However, when assembling the final device for the presentation, we found that the accelerometer malfunctions when operated simultaneously with a lot of other components, so we had to rely on something else.
Tilt Switch¶
Tilt ball switches are switches that activate when the ball makes contact with the two ends, this only happens when the switch is upright or angled at small angles.
For the first iterations of the design we tested using this switch, however, the medicine door did not open enough for the ball to reliably come of the contact points and break the electrical circuit. This is the reason we elected to use an accelorometer, since we could control the actuation point accurately. However, for our final design, the door opens approximately 90°, this makes using a tilt ball switch more reliable.
Tilt ball switches are simple digital inputs, however, you must be careful when implementing them in your projects because of the change in angle they need to reliably activate and because of the starting position (high or low).
Buzzer¶
To indicate when the time for medicine has come, we elected to use a simple tune:
This tune was generated using Copilot AI. The code can be seen below:
#include "pitches.h"
#define BUZZER_PIN 9
int melody[] = {
NOTE_C4, NOTE_E4, NOTE_G4, NOTE_C5, NOTE_G4, NOTE_E4, NOTE_C4,
NOTE_E4, NOTE_G4, NOTE_B4, NOTE_D5, NOTE_B4, NOTE_G4, NOTE_E4
};
int noteDurations[] = {
8, 8, 8, 4, 8, 8, 8,
8, 8, 8, 4, 8, 8, 8
};
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
for (int thisNote = 0; thisNote < 8; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(BUZZER_PIN, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(8);
}
}
void loop() {
//no need for looping as we only want it to play once
}