Advertisement
HDElectronics

Lahcen_PFE

Jul 5th, 2021 (edited)
1,247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*Libraries*/
  2. #include <Servo.h>
  3. #include <Wire.h>
  4. #include <LiquidCrystal_I2C.h>
  5. #include <SimpleDHT.h>
  6.  
  7. /*Pins*/
  8. #define trig_Pin A3
  9. #define echo_Pin A2
  10. #define motorPin1 8
  11. #define motorPin2 9
  12. #define motor2Pin1 10
  13. #define motor2Pin2 11
  14. #define pinDHT11 5
  15. #define PIN_PIR 2
  16.  
  17. /*Instances*/
  18. SimpleDHT11 dht11(pinDHT11);
  19. LiquidCrystal_I2C lcd(0x27, 20, 4);
  20. Servo myservo;
  21.  
  22. /*Variables*/
  23. int distance = 100;
  24. int distanceR = 0;
  25. int distanceL =  0;
  26. int Time = 0;
  27. int Temp;
  28. int Humidity;
  29. int state = 0;
  30. unsigned long tempoMesure = 0;
  31. byte temperature = 0;
  32. byte humidity = 0;
  33. int stAlarm = 0;
  34. int stTemp = 0;
  35. float slAlarmTemp = 35;
  36. boolean blAlarm = 0;
  37. String received;
  38.  
  39.  
  40. void setup() {
  41.   Serial.begin(9600);
  42.   lcd.begin();
  43.   myservo.attach(7);
  44.   myservo.write(70);
  45.   pinMode(motorPin1, OUTPUT);
  46.   pinMode(motorPin2, OUTPUT);
  47.   pinMode(motor2Pin1, OUTPUT);
  48.   pinMode(motor2Pin2, OUTPUT);
  49.   pinMode(trig_Pin, OUTPUT);
  50.   pinMode(echo_Pin, INPUT);
  51.   pinMode(PIN_PIR, INPUT);
  52.   delay(1000);
  53. }
  54.  
  55. void loop()
  56. {
  57.   /*Chaque 5s mesurer la temperature et l'humidité
  58.   et tester la proximité (PIR)*/
  59.   tempoMesure = millis();
  60.   if (millis() - tempoMesure > 5000)
  61.   {
  62.     mesureTemperature();
  63.     proximite();
  64.   }
  65. }
  66.  
  67. /*
  68.   Fonctions:
  69.   |->int obstacle()
  70.   |->int lookRight()
  71.   |->int lookLeft()
  72.   |->void avant()
  73.   |->void arriere()
  74.   |->void stop()
  75.   |->void droite()
  76.   |->void gauche()
  77.   |
  78.   |->void getTemperature()
  79.   |->void proximite()
  80.   |->void mesureTemperature()
  81. */
  82. int obstacle()
  83. {
  84.   digitalWrite(trig_Pin, HIGH);
  85.   delayMicroseconds(1000);
  86.   digitalWrite(trig_Pin, LOW);
  87.   Time = pulseIn(echo_Pin, HIGH);
  88.   distance = (Time / 29.1) / 2;
  89.   if (distance < 0)
  90.     distance = 0;
  91.   if (distance > 200)
  92.     distance = 200;
  93.   return distance;
  94. }
  95.  
  96. int lookRight()
  97. {
  98.   myservo.write(0);
  99.   delay(500);
  100.   distance = obstacle();
  101.   delay(200);
  102.   myservo.write(70);
  103.   return distance;
  104. }
  105.  
  106. int lookLeft()
  107. {
  108.   myservo.write(170);
  109.   delay(500);
  110.   distance = obstacle();
  111.   delay(200);
  112.   myservo.write(70);
  113.   return distance;
  114. }
  115.  
  116. void avant() {
  117.   if (distance <= 40)
  118.   { digitalWrite(motorPin1, HIGH);
  119.     digitalWrite(motorPin2, HIGH);
  120.     digitalWrite(motor2Pin1, HIGH);
  121.     digitalWrite(motor2Pin2, HIGH);
  122.     delay(2000);
  123.     distanceR = lookRight();
  124.     delay(200);
  125.     distanceL = lookLeft();
  126.     delay(200);
  127.     if (distanceR >= distanceL)
  128.     {
  129.       digitalWrite(motorPin1, HIGH);
  130.       digitalWrite(motorPin2, LOW);
  131.       digitalWrite(motor2Pin1, HIGH);
  132.       digitalWrite(motor2Pin2, HIGH);
  133.     } else
  134.     {
  135.       digitalWrite(motorPin1, HIGH);
  136.       digitalWrite(motorPin2, HIGH);
  137.       digitalWrite(motor2Pin1, HIGH);
  138.       digitalWrite(motor2Pin2, LOW);
  139.     }
  140.   }
  141. }
  142.  
  143. void arriere() {
  144.   digitalWrite(motorPin1, LOW);
  145.   digitalWrite(motorPin2, HIGH);
  146.   digitalWrite(motor2Pin1, LOW);
  147.   digitalWrite(motor2Pin2, HIGH);
  148. }
  149.  
  150. void stop() {
  151.   digitalWrite(motorPin1, HIGH);
  152.   digitalWrite(motorPin2, HIGH);
  153.   digitalWrite(motor2Pin1, HIGH);
  154.   digitalWrite(motor2Pin2, HIGH);
  155. }
  156.  
  157. void droite() {
  158.   if (distance <= 40)
  159.   {
  160.     distanceR = lookRight();
  161.     delay(200);
  162.     distanceL = lookLeft();
  163.     delay(200);
  164.     if (distanceR >= distanceL)
  165.     {
  166.       digitalWrite(motorPin1, HIGH);
  167.       digitalWrite(motorPin2, LOW);
  168.       digitalWrite(motor2Pin1, HIGH);
  169.       digitalWrite(motor2Pin2, HIGH);
  170.     } else
  171.     {
  172.       digitalWrite(motorPin1, HIGH);
  173.       digitalWrite(motorPin2, HIGH);
  174.       digitalWrite(motor2Pin1, HIGH);
  175.       digitalWrite(motor2Pin2, LOW);
  176.     }
  177.   }
  178.   else
  179.   {
  180.     digitalWrite(motorPin1, HIGH);
  181.     digitalWrite(motorPin2, HIGH);
  182.     digitalWrite(motor2Pin1, HIGH);
  183.     digitalWrite(motor2Pin2, LOW);
  184.   }
  185. }
  186.  
  187. void gauche() {
  188.   if (distance <= 40)
  189.   {
  190.     distanceR = lookRight();
  191.     delay(200);
  192.     distanceL = lookLeft();
  193.     delay(200);
  194.     if (distanceR >= distanceL)
  195.     {
  196.       digitalWrite(motorPin1, HIGH);
  197.       digitalWrite(motorPin2, LOW);
  198.       digitalWrite(motor2Pin1, HIGH);
  199.       digitalWrite(motor2Pin2, HIGH);
  200.  
  201.     } else
  202.     {
  203.       digitalWrite(motorPin1, HIGH);
  204.       digitalWrite(motorPin2, HIGH);
  205.       digitalWrite(motor2Pin1, HIGH);
  206.       digitalWrite(motor2Pin2, LOW);
  207.     }
  208.   }
  209.   else
  210.   {
  211.     digitalWrite(motorPin1, HIGH);
  212.     digitalWrite(motorPin2, LOW);
  213.     digitalWrite(motor2Pin1, HIGH);
  214.     digitalWrite(motor2Pin2, HIGH);
  215.   }
  216. }
  217.  
  218. /*
  219.   Fonction qui découpe une chaine de
  220.   charactère en sous chaines de charactères
  221.   pour separer les differentes commandes reçus
  222. */
  223. void decoupeur(String inputString) {
  224.   char inputChar[inputString.length() + 1] ;
  225.   inputString.toCharArray(inputChar, inputString.length() + 1);
  226.   char* command = strtok(inputChar, "&");
  227.   while (command != 0)
  228.   {
  229.     char* valueCommand = strchr(command, '=');
  230.     if (valueCommand != 0)
  231.     {
  232.       *valueCommand = 0;
  233.       ++valueCommand;
  234.       if (String(command) == "slalarm") {
  235.         slAlarmTemp = String(valueCommand).toFloat();
  236.       } else if (String(command) == "color") {
  237.       }
  238.     }
  239.     command = strtok(0, "&");
  240.   }
  241. }
  242.  
  243. void getTemperature() {
  244.  
  245.   temperature = 0;
  246.   humidity = 0;
  247.   int err = SimpleDHTErrSuccess;
  248.   if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
  249.     Serial.print("Read DHT11 failed, err="); Serial.println(err); delay(1000);
  250.     return;
  251.   }
  252.   Serial.print("Sample OK: ");
  253.   Serial.print((int)temperature); Serial.print(" *C, ");
  254.   Serial.print((int)humidity); Serial.println(" H");
  255. }
  256.  
  257. void proximite() {
  258.   if (digitalRead(PIN_PIR) == HIGH && blAlarm == 0) {
  259.  
  260.     blAlarm = 1;
  261.     if (stAlarm == 1) {
  262.       Serial.println("alarm=1" );
  263.     }
  264.   }
  265.   else if (digitalRead(PIN_PIR) == LOW) {
  266.     blAlarm = 0;
  267.   }
  268. }
  269.  
  270. void mesureTemperature() {
  271.   if (stTemp == 1) {
  272.     getTemperature();
  273.     Serial.println("temp=" + String(temperature) );
  274.     Serial.println("hum=" + String(humidity) );
  275.   }
  276. }
  277.  
  278. /*
  279.   Function d'interruption
  280.   elle est executée dès qu'une
  281.   trame arrive au port Serie
  282. */
  283. void serialEvent() {
  284.   if (Serial.available() != 0) //si il y a des données sur le buffers Serie
  285.   {
  286.     received = Serial.readString(); //lire les données et les stocker
  287.  
  288.     if (received == "1")
  289.     {
  290.       avant();
  291.     }
  292.     else if (received == "2") {
  293.       arriere();
  294.     }
  295.     else if (received == "3") {
  296.       stop();
  297.     }
  298.     else if (received == "4") {
  299.       droite();
  300.     }
  301.     else if (received == "5") {
  302.       gauche();
  303.     }
  304.     else if (received == "etat") {
  305.       Serial.println("stalarm=" + String(stAlarm));
  306.       Serial.println("sttemp=" + String(stTemp));
  307.       Serial.println("sltemp=" + String(slAlarmTemp));
  308.     }
  309.     else if (received == "sttemp=1") {
  310.       stTemp = 1;
  311.     }
  312.     else if (received == "sttemp=0") {
  313.       stTemp = 0;
  314.     }
  315.     else if (received == "stalarm=1") {
  316.       stAlarm = 1;
  317.     }
  318.     else if (received == "stalarm=0") {
  319.       stAlarm = 0;
  320.     } else {
  321.       decoupeur(received);
  322.     }
  323.   }
  324.   Serial.println(received);
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement