unsigned long currentTime;
unsigned long startTime;
const int motor1SpeedPin = 5;
const int motor1DirectionPin1 = 3;
const int motor1DirectionPin2 = 4;
const int motor2SpeedPin = 9;
const int motor2DirectionPin1 = 2;
const int motor2DirectionPin2 = 7;
#define middleIR A3
#define rightIR A4
#define ext_rightIR A5
#define leftIR A2
#define ext_leftIR A1
//int akssil;
int speedright = 100; // is two
int speedleft =110; // is one
int speedrighturn_1 = 150;
int speedlefturn_1 =180;
int speedrighturn_2 = 150;
int speedlefturn_2 =180;
int speedextrighturn = 150;
int speedextlefturn =180;
int last_value;
int t;
const int blue_pin = A0; //17 blue
const int car_pin = 12; //27 voiture
const int green_pin = 13; // 6 green
const int red_pin = 8; //13 red
int wekt =100 ;
int bdl = 20;
int check_green=0;
int check_red=0;
int check_car=0;
void setup() {
t=10;
last_value=1;
Serial.begin(9600);
pinMode(motor1SpeedPin, OUTPUT);
pinMode(motor1DirectionPin1, OUTPUT);
pinMode(motor1DirectionPin2, OUTPUT);
pinMode(motor2SpeedPin, OUTPUT);
pinMode(motor2DirectionPin1, OUTPUT);
pinMode(motor2DirectionPin2, OUTPUT);
pinMode(middleIR, INPUT);
pinMode(rightIR, INPUT);
pinMode(leftIR, INPUT);
pinMode(ext_rightIR, INPUT);
pinMode(ext_leftIR, INPUT);
pinMode(car_pin, INPUT);
pinMode(green_pin, INPUT);
pinMode(blue_pin, INPUT);
pinMode(red_pin, INPUT);
demarage();
startTime = millis();
}
void loop() {
linefollower();
make_decisions();
}
void linefollower() {
bool middle = digitalRead(middleIR);
bool left = digitalRead(leftIR);
bool right = digitalRead(rightIR);
bool ext_left = digitalRead(ext_leftIR);
bool ext_right = digitalRead(ext_rightIR);
if (!ext_right && !right && middle && !left && !ext_left) {
last_value = 1;
forwardD();
} else if (right && middle && left ) {
last_value = -1;
stopDef();
}
else if (ext_right && right && !left && !ext_left) {
//turnleft
last_value= 30;
sharpturnL();
} else if (!ext_right && !right && left && ext_left) {
//turnright
last_value= -30;
sharpturnR();
}
else if (ext_right && !right && !middle && !left && !ext_left) {
last_value=20;
left_turn_2();
} else if (!ext_right && !right && !middle && !left && ext_left) {
last_value=-20;
right_turn_2();
}
else if (!ext_right && right && !middle && !left && !ext_left) {
last_value=10;
left_turn_1();
} else if (!ext_right && !right && !middle && left && !ext_left) {
last_value=-10;
right_turn_1();
}
else if (ext_right && right && middle && left && ext_left) {
if (last_value == 1){
forwardD();
delay(t);
last_value=1;
}else if(last_value == -1){
stopDef();
delay(t);
last_value=1;
}else if(last_value == 10){
left_turn_1();
delay(t);
last_value=1;
}else if (last_value == -10){
right_turn_1();
delay(t);
last_value=1;
}else if(last_value == 20){
left_turn_2();
delay(t);
last_value=1;
}else if(last_value == -20){
right_turn_2();
delay(t);
last_value=1;
}else if(last_value == 30){
sharpturnL();
delay(t);
last_value=1;
}else if(last_value == -30){
sharpturnL();
delay(t);
last_value=1;
}
}
}
void make_decisions(){
bool car = digitalRead(car_pin);
bool red = digitalRead(red_pin);
bool green = digitalRead(green_pin);
bool blue = digitalRead(blue_pin);
if(car){
if(check_car==0){
asber();
delay(2000);
demarage();
last_value=1;
check_car = 1;
//déchargé
//we9t li lzm
}else {
check_car = 1;
//madir wlo
last_value=1;
}
}
if(red){
if(check_red==0){
asber();
delay(5000);
//demarage();
//last_value=1;
check_red = 1;
//déchargé
//we9t li lzm
}
else {
check_red = 1;
}
}
if(blue){
currentTime = millis();
unsigned long elapsedTime = currentTime - startTime;
if (elapsedTime>=4000){
asber();
delay(2000);
startTime = currentTime;
//last_value=1;
blue=0;
}
}
if(green){
if (check_green == 0) {
asber();
delay(5000);
demarage();
//last_value=1;
delay(200);
asber();
delay(3000);
check_green = 1;
//chargé
}
else{
check_green = 1;
}
}
}
void forwardD() {
analogWrite(motor1SpeedPin, speedleft);
digitalWrite(motor1DirectionPin1, HIGH);
digitalWrite(motor1DirectionPin2, LOW);
analogWrite(motor2SpeedPin, speedright);
digitalWrite(motor2DirectionPin1, HIGH);
digitalWrite(motor2DirectionPin2, LOW);
}
void sharpturnR() {
analogWrite(motor1SpeedPin, speedextlefturn);
digitalWrite(motor1DirectionPin1, LOW);
digitalWrite(motor1DirectionPin2, HIGH);
analogWrite(motor2SpeedPin, speedextrighturn);
digitalWrite(motor2DirectionPin1, HIGH);
digitalWrite(motor2DirectionPin2, LOW);
}
void sharpturnL() {
analogWrite(motor1SpeedPin, speedextlefturn);
digitalWrite(motor1DirectionPin1, HIGH);
digitalWrite(motor1DirectionPin2, LOW);
analogWrite(motor2SpeedPin, speedextrighturn);
digitalWrite(motor2DirectionPin1, LOW);
digitalWrite(motor2DirectionPin2, HIGH);
}
void left_turn_1() {
analogWrite(motor1SpeedPin, speedlefturn_1);
digitalWrite(motor1DirectionPin1, HIGH);
digitalWrite(motor1DirectionPin2, LOW);
analogWrite(motor2SpeedPin, speedrighturn_2);
digitalWrite(motor2DirectionPin1, LOW);
digitalWrite(motor2DirectionPin2, HIGH);
}
void right_turn_1() {
analogWrite(motor1SpeedPin, speedlefturn_2);
digitalWrite(motor1DirectionPin1, LOW);
digitalWrite(motor1DirectionPin2, HIGH);
analogWrite(motor2SpeedPin, speedrighturn_1);
digitalWrite(motor2DirectionPin1, HIGH);
digitalWrite(motor2DirectionPin2, LOW);
}
void left_turn_2() {
analogWrite(motor1SpeedPin, speedlefturn_2);
digitalWrite(motor1DirectionPin1, HIGH);
digitalWrite(motor1DirectionPin2, LOW);
analogWrite(motor2SpeedPin, speedrighturn_1);
digitalWrite(motor2DirectionPin1, LOW);
digitalWrite(motor2DirectionPin2, HIGH);
}
void right_turn_2() {
analogWrite(motor1SpeedPin, speedlefturn_1);
digitalWrite(motor1DirectionPin1, LOW);
digitalWrite(motor1DirectionPin2, HIGH);
analogWrite(motor2SpeedPin, speedrighturn_2);
digitalWrite(motor2DirectionPin1, HIGH);
digitalWrite(motor2DirectionPin2, LOW);
}
void stopDef() {
analogWrite(motor1SpeedPin, 0);
digitalWrite(motor1DirectionPin1, LOW);
digitalWrite(motor1DirectionPin2, LOW);
analogWrite(motor2SpeedPin, 0);
digitalWrite(motor2DirectionPin1, LOW);
digitalWrite(motor2DirectionPin2, LOW);
delay(50);
}
void asber(){
/*analogWrite(motor1SpeedPin, 0);
digitalWrite(motor1DirectionPin1, LOW);
digitalWrite(motor1DirectionPin2, LOW);
analogWrite(motor2SpeedPin, 0);
digitalWrite(motor2DirectionPin1, LOW);
digitalWrite(motor2DirectionPin2, LOW);*/
analogWrite(motor1SpeedPin, 0);
digitalWrite(motor1DirectionPin1, LOW);
digitalWrite(motor1DirectionPin2, LOW);
analogWrite(motor2SpeedPin, 0);
digitalWrite(motor2DirectionPin1, LOW);
digitalWrite(motor2DirectionPin2, LOW);
delay(50);
}
void demarage(){
analogWrite(motor1SpeedPin, 180);
digitalWrite(motor1DirectionPin1, HIGH);
digitalWrite(motor1DirectionPin2, LOW);
analogWrite(motor2SpeedPin, 180);
digitalWrite(motor2DirectionPin1, HIGH);
digitalWrite(motor2DirectionPin2, LOW);
delay(400);
}
Comments