Proiectul reprezinta un sistem ce poate fi folosit in recoletele de rosii, castraveti, etc pentru fermele din Romania. Acesta poate aduce un plus de siguranta deoarece intercepteaza distantele pana la animalele / pasarile care dauneaza recolta.
Sistemul presupune un senzor ultrasonic de distanta prin care un obiect aflat in fata acestuia este interceptat, calculandu-se distanta pana la el. Distanta este in permanenta afisata pe un LCD, iar daca distanta scade sub o anumita valoare minima, un servomotor porneste miscarea unei elice care are rolul de a speria “animalul rapitor”.
#include<Servo.h> #include <LiquidCrystal_I2C.h> #define echo 4 #define trigger 5
LiquidCrystal_I2C lcd(0x27,20,4); const int inf = 2e9; int pos = 0; double duration, distance; double before_distance = inf; Servo servomotor;
void setup() {
servomotor.attach(7);
DDRD |= (1 << trigger); DDRD &= ~(1 << echo); Serial.begin(9600); lcd.init(); lcd.backlight();
}
void updateDistance() {
PORTD |= (1 << trigger); delayMicroseconds(5); PORTD &= ~(1 << trigger); duration = pulseIn(echo, HIGH); distance = (float)duration * (0.0345) / 2; if (fabs(distance - before_distance) < 3e-1) { distance = before_distance; } else { before_distance = distance; }
}
void loop() {
updateDistance(); lcd.setCursor(0,0); lcd.print("Distance is: "); lcd.setCursor(0, 1); lcd.print(distance); lcd.print(" cm"); if (distance <= 100) { for (pos = 0; pos <= 180; pos += 1) { // tell servo to go to position in variable 'pos' servomotor.write(pos); updateDistance(); if (distance > 100) { break; } delay(2); } for (pos = 180; pos >= 0; pos -= 1) { servomotor.write(pos); updateDistance(); if (distance > 100) { break; } delay(2); } lcd.clear(); }
}