1x Arduion Nano.
2x Ultrasonic sensors.
1x Servo Motor.
1x RGB led.
1x breadboard
1x Capacitor. (To control the servo smoothly)
and quite a few jumper wires.
I won't explain more in this because the reason has been already explained in the all Arduino posts page. If you have come from the YouTube then you have already seen how cool the smart bin is.
The sketch
#include <Servo.h> Servo myservo; int trigPin2 = 3; int echoPin2 = 4; int trigPin1 = 5; int echoPin1 = 6; int redPin = 10; int greenPin = 11; int bluePin = 12; int pos = 170; // variable to store the servo position #define COMMON_ANODE void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object pinMode(trigPin1, OUTPUT); pinMode(echoPin1, INPUT); pinMode(trigPin2, OUTPUT); pinMode(echoPin2, INPUT); pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); Serial.begin(9600); } void firstsensor(){ // This function is for first sensor. setColor(0, 255, 0); // green delay(15); setColor(0, 0, 0); // green int duration1, distance1; digitalWrite (trigPin1, HIGH); delayMicroseconds (10); digitalWrite (trigPin1, LOW); duration1 = pulseIn (echoPin1, HIGH); distance1 = (duration1/2) / 29.1; if (distance1 >=5 && distance1 <=30) { // Change the number for long or short distances. for (pos = 170; pos >= 90; pos -= 1) { // goes from 180 degrees to 0 degrees myservo.write(pos); delay(20); } delay(2000); for (pos = 90; pos <= 170; pos += 1) { // in steps of 1 degree myservo.write(pos); delay(20); } } else { secondsensor(); } } void secondsensor(){ // This function is for second sensor. int duration2, distance2; digitalWrite (trigPin2, HIGH); delayMicroseconds (10); digitalWrite (trigPin2, LOW); duration2 = pulseIn (echoPin2, HIGH); distance2 = (duration2/2) / 29.1; Serial.print("2nd Sensor: "); Serial.print(distance2); Serial.print("cm "); if (distance2 >=2 && distance2 <=6) { // Change the number for long or short distances. full(); } else { delay(1000); firstsensor(); } } void full(){ // The bin is full. delay(1000); setColor(255, 0, 0); // red; delay(15); setColor(0, 0, 0); // red full(); } void setColor(int red, int green, int blue) { #ifdef COMMON_ANODE red = 255 - red; green = 255 - green; blue = 255 - blue; #endif analogWrite(redPin, red); analogWrite(greenPin, green); analogWrite(bluePin, blue); } void loop() { secondsensor(); } |
No comments:
Post a Comment