This shows you the differences between two versions of the page.
|
pm:prj2025:vradulescu:vlad_andrei.voicu [2025/05/25 19:32] vlad_andrei.voicu [Software Design] |
pm:prj2025:vradulescu:vlad_andrei.voicu [2025/05/25 19:34] (current) vlad_andrei.voicu [Software Design] |
||
|---|---|---|---|
| Line 27: | Line 27: | ||
| * (etapa 3) surse şi funcţii implementate | * (etapa 3) surse şi funcţii implementate | ||
| * #include <SoftwareSerial.h> | * #include <SoftwareSerial.h> | ||
| + | |||
| + | <code cpp> | ||
| + | #include <SoftwareSerial.h> | ||
| + | #include <Servo.h> | ||
| + | #include <LiquidCrystal.h> | ||
| + | |||
| + | // Bluetooth | ||
| + | SoftwareSerial mySerial(7, 6); | ||
| + | |||
| + | // LCD | ||
| + | LiquidCrystal lcd(12, 11, 5, 4, 3, 2); | ||
| + | |||
| + | // Servo | ||
| + | Servo servoMotor; | ||
| + | const int servoPin = 10; | ||
| + | |||
| + | // Senzor ultrasonic | ||
| + | const int trigPin = 9; | ||
| + | const int echoPin = 8; | ||
| + | |||
| + | // Configurări | ||
| + | const int posInitial = 0; | ||
| + | const int posApasare = 150; | ||
| + | const int pragNivelApa = 10; // cm | ||
| + | const int delayApasare = 500; | ||
| + | |||
| + | void setup() { | ||
| + | mySerial.begin(9600); | ||
| + | Serial.begin(9600); | ||
| + | |||
| + | lcd.begin(16, 2); | ||
| + | lcd.print("Sistem pornit"); | ||
| + | |||
| + | servoMotor.attach(servoPin); | ||
| + | servoMotor.write(posInitial); | ||
| + | |||
| + | pinMode(trigPin, OUTPUT); | ||
| + | pinMode(echoPin, INPUT); | ||
| + | } | ||
| + | |||
| + | long masoaraNivelApa() { | ||
| + | digitalWrite(trigPin, LOW); | ||
| + | delayMicroseconds(2); | ||
| + | digitalWrite(trigPin, HIGH); | ||
| + | delayMicroseconds(10); | ||
| + | digitalWrite(trigPin, LOW); | ||
| + | |||
| + | long durata = pulseIn(echoPin, HIGH); | ||
| + | long distanta = durata * 0.034 / 2; | ||
| + | return distanta; | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | if (mySerial.available()) { | ||
| + | char comanda = mySerial.read(); | ||
| + | Serial.print("Comanda primita: "); | ||
| + | Serial.println(comanda); | ||
| + | |||
| + | lcd.clear(); | ||
| + | lcd.setCursor(0, 0); | ||
| + | lcd.print("Comanda: "); | ||
| + | lcd.print(comanda); | ||
| + | |||
| + | if (comanda == '1') { | ||
| + | long nivelApa = masoaraNivelApa(); | ||
| + | |||
| + | lcd.setCursor(0, 1); | ||
| + | lcd.print("Apa: "); | ||
| + | lcd.print(nivelApa); | ||
| + | lcd.print("cm"); | ||
| + | |||
| + | if (nivelApa > pragNivelApa) { | ||
| + | Serial.println("Apasare..."); | ||
| + | lcd.setCursor(0, 1); | ||
| + | lcd.print("Cafea pornita "); | ||
| + | |||
| + | servoMotor.write(posApasare); | ||
| + | delay(delayApasare); | ||
| + | servoMotor.write(posInitial); | ||
| + | } else { | ||
| + | Serial.println("Apa insuficienta"); | ||
| + | lcd.setCursor(0, 1); | ||
| + | lcd.print("Apa insuficienta"); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | |||
| + | </code> | ||