Oh I get it! I'll remove them.
So right know, this is my code. Since my motor was already on a PWM pin, I only changed every digitalWrite by a analogWrite
const int pingPin = 7;
int moteur= 3;
#include <Servo.h>
int rouge = 6;
int vert = 5;
int bleu= 4;
Servo myservo;
int pos = 0;
const int maxsonar = 1;
long anVolt, inches, cm2;
int sum=0;
int avgrange=60;
long duration,cm;
void setup() {
pinMode(moteur, OUTPUT);
pinMode(rouge, OUTPUT);
pinMode(vert,OUTPUT);
pinMode(bleu,OUTPUT);
myservo.attach(9);
pinMode(maxsonar,INPUT);
}
void loop()
{
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
cm = microsecondsToCentimeters(duration);
delay(100);
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
for(int i = 0; i < avgrange ; i++)
{
anVolt = analogRead(maxsonar)/2;
sum += anVolt;
delay(10);
}
inches = sum/avgrange;
cm2 = inches * 2.54;
sum = 0;
delay(500);
if (cm2>30 and cm>30 )
{
digitalWrite(rouge,HIGH);
delay(500);
digitalWrite(rouge,LOW);
delay(500);
digitalWrite(vert,HIGH);
delay(500);
digitalWrite(vert,LOW);
delay(500);
digitalWrite(bleu,HIGH);
delay(500);
digitalWrite(bleu,LOW);
delay(500);
analogWrite(moteur,170);
delay(500);
}
if (cm < 30 and cm2 <30 )
{
analogWrite(moteur,0);
delay(500);
digitalWrite(rouge,HIGH);
delay(500);
}
if(cm2>30 and cm<30)
{
analogWrite(moteur, 90);
delay(500);
for(pos = 0; pos < 180; pos += 1)
{
myservo.write(pos);
}
digitalWrite(vert,HIGH);
delay(500);
digitalWrite(vert,LOW);
delay(500); }
if(cm2<30 and cm>30)
{
analogWrite(moteur, 90);
delay(500);
for(pos = 180; pos>=1; pos-=1)
{
myservo.write(pos);
}
digitalWrite(bleu,HIGH);
delay(500);
digitalWrite(bleu,LOW);
delay(500);
}
}