Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Libraries*/
- #include <Servo.h>
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #include <SimpleDHT.h>
- /*Pins*/
- #define trig_Pin A3
- #define echo_Pin A2
- #define motorPin1 8
- #define motorPin2 9
- #define motor2Pin1 10
- #define motor2Pin2 11
- #define pinDHT11 5
- #define PIN_PIR 2
- /*Instances*/
- SimpleDHT11 dht11(pinDHT11);
- LiquidCrystal_I2C lcd(0x27, 20, 4);
- Servo myservo;
- /*Variables*/
- int distance = 100;
- int distanceR = 0;
- int distanceL = 0;
- int Time = 0;
- int Temp;
- int Humidity;
- int state = 0;
- unsigned long tempoMesure = 0;
- byte temperature = 0;
- byte humidity = 0;
- int stAlarm = 0;
- int stTemp = 0;
- float slAlarmTemp = 35;
- boolean blAlarm = 0;
- String received;
- void setup() {
- Serial.begin(9600);
- lcd.begin();
- myservo.attach(7);
- myservo.write(70);
- pinMode(motorPin1, OUTPUT);
- pinMode(motorPin2, OUTPUT);
- pinMode(motor2Pin1, OUTPUT);
- pinMode(motor2Pin2, OUTPUT);
- pinMode(trig_Pin, OUTPUT);
- pinMode(echo_Pin, INPUT);
- pinMode(PIN_PIR, INPUT);
- delay(1000);
- }
- void loop()
- {
- /*Chaque 5s mesurer la temperature et l'humidité
- et tester la proximité (PIR)*/
- tempoMesure = millis();
- if (millis() - tempoMesure > 5000)
- {
- mesureTemperature();
- proximite();
- }
- }
- /*
- Fonctions:
- |->int obstacle()
- |->int lookRight()
- |->int lookLeft()
- |->void avant()
- |->void arriere()
- |->void stop()
- |->void droite()
- |->void gauche()
- |
- |->void getTemperature()
- |->void proximite()
- |->void mesureTemperature()
- */
- int obstacle()
- {
- digitalWrite(trig_Pin, HIGH);
- delayMicroseconds(1000);
- digitalWrite(trig_Pin, LOW);
- Time = pulseIn(echo_Pin, HIGH);
- distance = (Time / 29.1) / 2;
- if (distance < 0)
- distance = 0;
- if (distance > 200)
- distance = 200;
- return distance;
- }
- int lookRight()
- {
- myservo.write(0);
- delay(500);
- distance = obstacle();
- delay(200);
- myservo.write(70);
- return distance;
- }
- int lookLeft()
- {
- myservo.write(170);
- delay(500);
- distance = obstacle();
- delay(200);
- myservo.write(70);
- return distance;
- }
- void avant() {
- if (distance <= 40)
- { digitalWrite(motorPin1, HIGH);
- digitalWrite(motorPin2, HIGH);
- digitalWrite(motor2Pin1, HIGH);
- digitalWrite(motor2Pin2, HIGH);
- delay(2000);
- distanceR = lookRight();
- delay(200);
- distanceL = lookLeft();
- delay(200);
- if (distanceR >= distanceL)
- {
- digitalWrite(motorPin1, HIGH);
- digitalWrite(motorPin2, LOW);
- digitalWrite(motor2Pin1, HIGH);
- digitalWrite(motor2Pin2, HIGH);
- } else
- {
- digitalWrite(motorPin1, HIGH);
- digitalWrite(motorPin2, HIGH);
- digitalWrite(motor2Pin1, HIGH);
- digitalWrite(motor2Pin2, LOW);
- }
- }
- }
- void arriere() {
- digitalWrite(motorPin1, LOW);
- digitalWrite(motorPin2, HIGH);
- digitalWrite(motor2Pin1, LOW);
- digitalWrite(motor2Pin2, HIGH);
- }
- void stop() {
- digitalWrite(motorPin1, HIGH);
- digitalWrite(motorPin2, HIGH);
- digitalWrite(motor2Pin1, HIGH);
- digitalWrite(motor2Pin2, HIGH);
- }
- void droite() {
- if (distance <= 40)
- {
- distanceR = lookRight();
- delay(200);
- distanceL = lookLeft();
- delay(200);
- if (distanceR >= distanceL)
- {
- digitalWrite(motorPin1, HIGH);
- digitalWrite(motorPin2, LOW);
- digitalWrite(motor2Pin1, HIGH);
- digitalWrite(motor2Pin2, HIGH);
- } else
- {
- digitalWrite(motorPin1, HIGH);
- digitalWrite(motorPin2, HIGH);
- digitalWrite(motor2Pin1, HIGH);
- digitalWrite(motor2Pin2, LOW);
- }
- }
- else
- {
- digitalWrite(motorPin1, HIGH);
- digitalWrite(motorPin2, HIGH);
- digitalWrite(motor2Pin1, HIGH);
- digitalWrite(motor2Pin2, LOW);
- }
- }
- void gauche() {
- if (distance <= 40)
- {
- distanceR = lookRight();
- delay(200);
- distanceL = lookLeft();
- delay(200);
- if (distanceR >= distanceL)
- {
- digitalWrite(motorPin1, HIGH);
- digitalWrite(motorPin2, LOW);
- digitalWrite(motor2Pin1, HIGH);
- digitalWrite(motor2Pin2, HIGH);
- } else
- {
- digitalWrite(motorPin1, HIGH);
- digitalWrite(motorPin2, HIGH);
- digitalWrite(motor2Pin1, HIGH);
- digitalWrite(motor2Pin2, LOW);
- }
- }
- else
- {
- digitalWrite(motorPin1, HIGH);
- digitalWrite(motorPin2, LOW);
- digitalWrite(motor2Pin1, HIGH);
- digitalWrite(motor2Pin2, HIGH);
- }
- }
- /*
- Fonction qui découpe une chaine de
- charactère en sous chaines de charactères
- pour separer les differentes commandes reçus
- */
- void decoupeur(String inputString) {
- char inputChar[inputString.length() + 1] ;
- inputString.toCharArray(inputChar, inputString.length() + 1);
- char* command = strtok(inputChar, "&");
- while (command != 0)
- {
- char* valueCommand = strchr(command, '=');
- if (valueCommand != 0)
- {
- *valueCommand = 0;
- ++valueCommand;
- if (String(command) == "slalarm") {
- slAlarmTemp = String(valueCommand).toFloat();
- } else if (String(command) == "color") {
- }
- }
- command = strtok(0, "&");
- }
- }
- void getTemperature() {
- temperature = 0;
- humidity = 0;
- int err = SimpleDHTErrSuccess;
- if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
- Serial.print("Read DHT11 failed, err="); Serial.println(err); delay(1000);
- return;
- }
- Serial.print("Sample OK: ");
- Serial.print((int)temperature); Serial.print(" *C, ");
- Serial.print((int)humidity); Serial.println(" H");
- }
- void proximite() {
- if (digitalRead(PIN_PIR) == HIGH && blAlarm == 0) {
- blAlarm = 1;
- if (stAlarm == 1) {
- Serial.println("alarm=1" );
- }
- }
- else if (digitalRead(PIN_PIR) == LOW) {
- blAlarm = 0;
- }
- }
- void mesureTemperature() {
- if (stTemp == 1) {
- getTemperature();
- Serial.println("temp=" + String(temperature) );
- Serial.println("hum=" + String(humidity) );
- }
- }
- /*
- Function d'interruption
- elle est executée dès qu'une
- trame arrive au port Serie
- */
- void serialEvent() {
- if (Serial.available() != 0) //si il y a des données sur le buffers Serie
- {
- received = Serial.readString(); //lire les données et les stocker
- if (received == "1")
- {
- avant();
- }
- else if (received == "2") {
- arriere();
- }
- else if (received == "3") {
- stop();
- }
- else if (received == "4") {
- droite();
- }
- else if (received == "5") {
- gauche();
- }
- else if (received == "etat") {
- Serial.println("stalarm=" + String(stAlarm));
- Serial.println("sttemp=" + String(stTemp));
- Serial.println("sltemp=" + String(slAlarmTemp));
- }
- else if (received == "sttemp=1") {
- stTemp = 1;
- }
- else if (received == "sttemp=0") {
- stTemp = 0;
- }
- else if (received == "stalarm=1") {
- stAlarm = 1;
- }
- else if (received == "stalarm=0") {
- stAlarm = 0;
- } else {
- decoupeur(received);
- }
- }
- Serial.println(received);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement