Can you help me for my program and explain me

Hello excuse me I don't know arduino and I have a project to realize if you can help me and explain me.

In my program I have to make two motors turn as soon as the ultra sound sensor detects something in front of it.
Here is my program how I can make the assembly in real I do not know what is hardware used if you can help me?

// Constants
const int enableBridge1 = 2;
const int MotorGauche = 3;
const int MotorGauche2 = 5;
const int MotorDroite = 4;
const int MotorDroite2 = 6;
int trig = 8;
int echo =10;
long lecture_echo;
long cm;

// Variables
int Power = 10; //Vitesse du moteur entre 0 et 255
void setup(){
pinMode(MotorGauche,OUTPUT);
pinMode(MotorGauche2,OUTPUT);
pinMode(MotorDroite,OUTPUT);
pinMode(MotorDroite2,OUTPUT);
pinMode(enableBridge1,OUTPUT);
pinMode(trig, OUTPUT);
digitalWrite(trig, LOW);
pinMode(echo, INPUT);
Serial.begin(9600);

analogWrite(MotorGauche,0);
analogWrite(MotorGauche2,0);
analogWrite(MotorDroite,0);
analogWrite(MotorDroite2,0);
}
void loop(){

digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
lecture_echo = pulseIn(echo,HIGH);
cm = lecture_echo /58;
Serial.print("Distance en cm :");
Serial.println(cm);

if(cm < 60){ //Demarrer le moteur quand le capteur voit le panier
analogWrite(MotorGauche2,0);
analogWrite(MotorGauche,1);
}
else {
analogWrite(MotorGauche,0);
analogWrite(MotorGauche2,0);
}
if(cm < 60){
analogWrite(MotorDroite,1);
analogWrite(MotorDroite2,1);
}
else {
analogWrite(MotorDroite,0);
analogWrite(MotorDroite2,0);
}
}

Start with learning how to use code tags.

So you have written a program and now you want advice on which hardware to get, correct?

So you programmed for 4 motors.

But you only want to use 2? Why program for 4 motors when you only want to use 2. Oh well that's your thing.

Very strange way of doing things, coding first then buying the hardware to fit the code.

Your post was MOVED to its current location as it is more suitable.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

I think it's a direct bridge drive. So, one pin for each side of the driver bridge. I assume it's copied from some project site...

1 Like

Oi! Thanks for that.

It looks like you do not know how to use the analogWrite function. Writing a value of 1 will probably not make the motor run.

You have if(cm < 60){ twice but doing different things.

What Arduino are you using? An Uno or Nano does not have PWM on pin 4.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.