The purpose of this device is to trigger an alarm when someone opens a bottle of medicine. The device uses an arduino with potentiometer for analog input. Once a certain threshold value from the potentiometer is reached then the alarm is triggered and increases in pitch as you continue to unscrew off the cap. The photocell is a motion detector that continues to vary the pitch until the cap is screwed back on. The values of the two analog inputs are summed and input into a Piezzo speaker which converts this integer into a sound.
*Note the picture shows a bluetooth shield on top of my Arduino, it is currently not used for this project.
Code
The code is based off of the Thereamin example from lab. I first modified it to accept another photocell to make a double theramin. Once I had the idea for the medicine alarm, I changed it to a potentiometer and left the wires the same.
Another change was adding the threshold as a condition into the play cycle loop. The threshold stops any piezzo noise before a certain value from the potentiometer has been reached.
The cycle loop was also modified to play a continuously variable noise instead of a note of a specific duration.
/*
Medicine alarm arduino code - requires a potentiometer in a medicine container taped to the cap.
*/
int potPin = 0;// input pin for the photocell
int photoPin1 = 1;// select the input pin for the pot
int speakerPin = 7;
int val = 0;
int val1 = 0;
void setup() {
pinMode(speakerPin, OUTPUT);
Serial.begin(9600);
Serial.println("ready");
}
void loop() {
digitalWrite(speakerPin, LOW);
val = analogRead(potPin); // read value from the pot
val = val; // process the value a little
val1 = analogRead(photoPin1); // read value from the photosensor
val1 = val1; // process the value a little
//val = val/2; // process the value a little
if (val<250){ //this condition makes is so the alarm doesn't go off unless the cap is opened
for( int i=0; i<1; i++ ) { // play it for 50 cycles
Serial.println(val1);
digitalWrite(speakerPin, HIGH);
delayMicroseconds((val+val1));
digitalWrite(speakerPin, LOW);
delayMicroseconds((val+val1));
}
}
}
- Login to post comments