Skip to content

Embedded programming

here is the code

#include "BluetoothSerial.h"
#include <Ultrasonic.h>
// #include <WiFi.h>
// #include <ArduinoOTA.h>

BluetoothSerial serialBT;


// const char* ssid = "YELLOLAB_WIFI_ADMIN";
// const char* password = "Y'ELLOLAB@AD20";
// Définition des broches des moteurs
int in1 = 23;
int in2 = 18;
int in3 = 14;
int in4 = 27;
int ENA = 19; // Broche PWM pour le moteur A
int ENB = 12;

// Capteur à ultrasons
Ultrasonic ultrasonic(26, 25);

// Définition des broches pour les DEL et le buzzer
const int DelPin1 = 32;
const int DelPin2 = 33;
const int BuzzerPin = 21;
const int laser = 5;

int buzCount = 0;
int startBuz = 0;
int distance;
int speedV = 255;
char dataIn;
int m = 0;
int period = 30000;

void setup() {
  Serial.begin(115200);
  serialBT.begin("Robot");

  // Initialisation des broches des moteurs
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);

  // Initialisation des broches des DELs et du buzzer
  pinMode(DelPin1, OUTPUT);
  pinMode(DelPin2, OUTPUT);
  pinMode(BuzzerPin, OUTPUT);
  pinMode(laser, OUTPUT);

  Serial.println("Setup completed");
  startBeep();
}

void loop() {
  // ArduinoOTA.handle();
  // Lecture des données Bluetooth
  if (serialBT.available()) {
    dataIn = serialBT.read();
    Serial.print("Received data: ");
    Serial.println(dataIn);

    switch (dataIn) {
      case '0':
        m = 0;
        break;
      case '1':
        m = 1;
        break;
      case '2':
        m = 2;
        break;
      case '3':
        m = 3;
        break;
      case '4':
        m = 4;
        break;
      case '5':
        m = 5;
        break;
      default:
        if (dataIn >= '7') {
          speedV = (dataIn - '0') * 25; // Adjusting the conversion for a wider range of speeds
          Serial.print("Set speedV to: ");
          Serial.println(speedV);
        }
        break;
    }
  }

  switch (m) {
    case 0:
      Initposition();
      break;
    case 1:
      forward();
      break;
    case 2:
      backward();
      break;
    case 3:
      right();
      break;
    case 4:
      left();
      break;
    case 5:
      blink();
      Detection();
      alertMovement(); // Ajouter une alerte en cas de détection
      m = 0; // Reset m after detection to prevent continuous alert
      break;
  }
}

void forward() {
  Serial.println("Moving forward");
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  analogWrite(ENA, speedV);

  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  analogWrite(ENB, speedV);
  delay(1000);
}

void backward() {
  Serial.println("Moving backward");
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(ENA, speedV);

  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  analogWrite(ENB, speedV);
  delay(1000);
}

void Initposition() {
  Serial.println("Stopping");
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4,  LOW);
  analogWrite(ENA, 0);
  analogWrite(ENB, 0);
  delay(1000);
}

void left() {
  Serial.println("Rotating left");
  digitalWrite(in1, HIGH );
  digitalWrite(in2, LOW);
  analogWrite(ENA, speedV);

  digitalWrite(in3, LOW);
  digitalWrite(in4,  LOW);
  analogWrite(ENB, 0);
  delay(1000);
}

void right() {
  Serial.println("Rotating right");
  digitalWrite(in1, LOW );
  digitalWrite(in2, LOW);
  analogWrite(ENA, 0);

  digitalWrite(in3, HIGH);
  digitalWrite(in4,  LOW);
  analogWrite(ENB, speedV);
  delay(1000);
}

void blink() {
  Serial.println("Blinking LEDs");
  digitalWrite(DelPin2, HIGH);
  digitalWrite(DelPin1, HIGH);
  delay(1000);
  digitalWrite(DelPin2, LOW);
  digitalWrite(DelPin1, LOW);
  delay(1000);
}

void Launch_Detection() {

   while(buzCount<2)
  {
  Serial.println("Launching detection with buzzer");
  digitalWrite(BuzzerPin,LOW);
  delay(50);
  digitalWrite(BuzzerPin,HIGH);
  delay(50);
  digitalWrite(BuzzerPin,LOW);
  buzCount++;
  }
}


void Detection() {
  distance = ultrasonic.read();
  Serial.print("Distance in CM: ");
  Serial.println(distance);
  delay(100);

  if(distance < 70){
    Initposition();
    blink();
    Launch_Detection();     
    Initposition();
    delay(period);
    backward();
  }
  if(distance > 70){
    forward();
  }
}

void laser_diode() {
  Serial.println("Laser diode on");
  digitalWrite(laser, HIGH);
  delay(100);
  Serial.println("Laser diode off");
  digitalWrite(laser, LOW);
  delay(100);
}

void alertMovement() {
  for(int i = 0; i < 3; i++) { // Répéter le mouvement de va-et-vient 3 fois
    forward();
    blink();
    Launch_Detection();
    laser_diode();
    backward();
    blink();
    Launch_Detection();
    laser_diode();
  }
  Initposition(); // Arrêter les moteurs après l'alerte
}
void startBeep()
{
  while(startBuz<2)
  {
  digitalWrite(BuzzerPin,LOW);
  delay(50);
  digitalWrite(BuzzerPin,HIGH);
  delay(50);
  digitalWrite(BuzzerPin,LOW);
  delay(50);
  digitalWrite(BuzzerPin,HIGH);
  delay(75);
  digitalWrite(BuzzerPin,LOW);
  startBuz++;
  }
 } 

for the camera

code camera

Code

code


Last update: June 24, 2024