Time to Buy Food!!
I can remind you to buy food
Why I Want to Do This
My mother always forgets to buy orange so I make this to remind her.
What We Need
Arduino uno R3 1個
電路試驗版 1個
超音波距離感應器。1個
電阻 1個
燈泡 1個
線 (因人而異)
電路試驗版 1個
超音波距離感應器。1個
電阻 1個
燈泡 1個
線 (因人而異)
Bread Boards
Run a Program
https://create.arduino.cc/editor/JennyLin717/5ef4ca22-ae25-4c82-adf0-d0706d6711e3/preview
int UltrasonicSensorCM(int trigPin, int echoPin) //Ultrasonic Sensor Code Auto Generated Return CM max distance 200
{
long duration;
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(20);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
duration = duration / 59;
if ((duration < 2) || (duration > 200)) return false;
return duration;
}
void setup(){ // put your setup code here, to run once:
digitalWrite( 11 , LOW ); //set ultrasonic sensor trigPin
pinMode( 10 , OUTPUT); // sets the digital pin as output
}
void loop(){ // put your main code here, to run repeatedly:
if ( UltrasonicSensorCM( 11 , 12 ) <10) {
digitalWrite( 10 , LOW ); // sets the digital pin on/off
noTone(8); //stop generating the tone
}
if ( UltrasonicSensorCM( 11 , 12 ) >10) {
digitalWrite( 10 , HIGH ); // sets the digital pin on/off
tone(8, 440, 1000); //buzzer or speaker frequency
}
}
int UltrasonicSensorCM(int trigPin, int echoPin) //Ultrasonic Sensor Code Auto Generated Return CM max distance 200
{
long duration;
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(20);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
duration = duration / 59;
if ((duration < 2) || (duration > 200)) return false;
return duration;
}
void setup(){ // put your setup code here, to run once:
digitalWrite( 11 , LOW ); //set ultrasonic sensor trigPin
pinMode( 10 , OUTPUT); // sets the digital pin as output
}
void loop(){ // put your main code here, to run repeatedly:
if ( UltrasonicSensorCM( 11 , 12 ) <10) {
digitalWrite( 10 , LOW ); // sets the digital pin on/off
noTone(8); //stop generating the tone
}
if ( UltrasonicSensorCM( 11 , 12 ) >10) {
digitalWrite( 10 , HIGH ); // sets the digital pin on/off
tone(8, 440, 1000); //buzzer or speaker frequency
}
}
How to Work
https://youtu.be/pRDcsejsyQ8