Smart Medicine Cabinet

Ask questions about projects relating to: computer science or pure mathematics (such as probability, statistics, geometry, etc...).

Moderators: AmyCowen, kgudger, bfinio, MadelineB, Moderators

Locked
zaranye6345
Posts: 1
Joined: Thu Jan 19, 2023 8:33 pm
Occupation: Student

Smart Medicine Cabinet

Post by zaranye6345 »

Hi, my name is Zara and i'm in grade 7. My science fair is very close and ive been trying to create a smart medicine alerting system. For my example for the judges I sent a timer for 10 seconds and made a serial monitor where if the medicine bottle is within 4 or less inches and the timer for 10 seconds reaches zero the buzzer i coded sounds and the LED lights up. I coded an if statement where if the medicine bottle is more than 4 inches away the LED will stop lighting up and the buzzer will have noTone however only the LED turns off while the buzzer just changes its sound. I'm also having trouble having the whole process restart again. Below is my code

int trigPin=12;
int echoPin=11;
int pingTravelTime;
float pingTravelDistance;
float distanceToTarget;
int ledPin=13;
int i=10;
int buzzer=9;
void setup() {
// put your setup code here, to run once:
pinMode(ledPin,OUTPUT);
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
Serial.begin(9600);
}

void loop() {
// put your main code here, to run once
Serial.print(i);
digitalWrite(trigPin,LOW);
delayMicroseconds(10);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
pingTravelTime=pulseIn(echoPin,HIGH);
delay(25);
pingTravelDistance=(pingTravelTime*765.*5280.*12)/(3600.*1000000)
;distanceToTarget=pingTravelDistance/2;
Serial.print("Distance To Target is: ");
Serial.print(distanceToTarget);
Serial.println(" inches.");
i=i-1;
delay(1000); {
if (i<0 && distanceToTarget < 4.0) {
digitalWrite(13,HIGH);
tone(9,500);
delay(1000);
}
if (i<0 && distanceToTarget > 4.0) {
digitalWrite(13,LOW);
tone(9,noTone);}
}
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
pingTravelTime=pulseIn(echoPin,HIGH);
delay(25);
pingTravelDistance=(pingTravelTime*765.*5280.*12)/(3600.*1000000)
;distanceToTarget=pingTravelDistance/2;
Serial.print("Distance To Target is: ");
Serial.print(distanceToTarget);
Serial.println(" inches.");

Please let me know how to fix this problem quickly.
Thank you for your time,
Zara
bfinio
Expert
Posts: 748
Joined: Mon Aug 12, 2013 2:41 pm
Occupation: Science Buddies Staff
Project Question: Expert
Project Due Date: n/a
Project Status: Not applicable

Re: Smart Medicine Cabinet

Post by bfinio »

Hi Zara,

I looked at your code and have a few suggestions that might help you debug.

First, you should always comment your code. Ideally you should add comments to each line explaining exactly what it's supposed to do. This will help you remember what the code does if you come back to it later, and it will also help other people read your code. This might also be something the judges look for at the fair, although I'm not sure.

Regarding your 10-second timer: it looks like "i" starts out at 10, you subtract 1 from it in each loop, and then your if statements only activate if i<10. It would probably be easier to just add a delay(10000) in your setup function. That would simplify your IF statements and you could get rid of the delay in your loop function. Currently that delay(1000) means you are only taking a reading once every second, so the circuit will react very slowly. Then there's another delay inside one of the IF statements, meaning it could be up to two seconds before the sensor takes another reading.

This can also cause a problem because the Arduino "int" variable type has a limited range - it can only store numbers between -32,768 to 32,767. Eventually, if you keep subtracting 1 from i, it will reach -32,768, but then it can't get any more negative - it will "wrap around" to positive 32,767. The way your code is written now, that would take over 8 hours, but if you get rid of or shorten the delays in the loop function, that would happen much faster.

You are also taking a reading from the ultrasonic sensor and printing it twice within the loop function. You shouldn't need to do that. Just take the reading once at the beginning of the loop, print the values, and then use your IF statements to control the LED and buzzer.

Finally, I am not that familiar with the Arduino tone function, but note that there are two different types of buzzers - "active" and "passive." The passive buzzers work with the tone function and can vary their pitch. Active buzzers just require a constant 5V and can only produce a single tone. You can tell which type you have just be connecting its positive pin to the Arduino's 5V and its negative pin to Arduino GND. If it makes noise, it's an active buzzer, and you would control it using the digitalWrite command (just set the pin high or low, like you would with an LED), not the tone command.

Hope all of that helps, please write back and let me know if it works.
Locked

Return to “Grades 6-8: Math and Computer Science”