#define ENA_PIN 5 // ENA attach to pin5
#define ENB_PIN 6// ENB attach to pin6
#define MOTOR_L_1 8 // MOTOR_L_1 attach to pin8
#define MOTOR_L_2 9 // MOTOR_L_1 attach to pin9
#define MOTOR_R_1 10 // MOTOR_R_1 attach to pin10
#define MOTOR_R_2 11 // MOTOR_R_1 attach to pin11
#define FORWARD 0 //define forward=0,car move forward
#define BACK 1 //define back=1,car move back
#define LIGHT_LEFT_1_PIN A0 //attach the left first Tracking module pinA0 to A0
#define LIGHT_LEFT_2_PIN A1 //attach the left second Tracking module pinA0 to A1
#define LIGHT_MIDDLE_PIN A2 //attach the module Tracking module pinA0 to A2
#define LIGHT_RIGHT_1_PIN A3 //attach the right second Tracking module pinA0 to A3
#define LIGHT_RIGHT_2_PIN A4 //attach the right first Tracking module pinA0 to A4
#define SPEED 180 //define SPEED=180,it is the rotate speed of motor
unsigned char flag = 3;
unsigned char angle = 90;
void setup()
{
/* set below pins as OUTPUT */
pinMode(ENA_PIN, OUTPUT);
pinMode(ENB_PIN, OUTPUT);
pinMode(MOTOR_L_1, OUTPUT);
pinMode(MOTOR_L_2, OUTPUT);
pinMode(MOTOR_R_1, OUTPUT);
pinMode(MOTOR_R_2, OUTPUT);
}
void loop()
{
if (analogRead(LIGHT_LEFT_1_PIN) > 758) //if read the value of the left first Tracking module pinA0 is more than 758
flag = 1;//flag=1
else if (analogRead(LIGHT_LEFT_2_PIN) > 758) //if read the value of the left second Tracking module pinA0 is more than 758
flag = 2;
else if (analogRead(LIGHT_MIDDLE_PIN) > 758) //if read the value of the module Tracking module pinA0 is more than 758
flag = 3;
else if (analogRead(LIGHT_RIGHT_2_PIN) > 758) //if read the value of the right second Tracking module pinA0 is more than 758
flag = 4;
else if (analogRead(LIGHT_RIGHT_1_PIN) > 758) //if read the value of the right first Tracking module pinA0 is more than 758
flag = 5;
else
flag = 6;
CAR_move(FORWARD, SPEED, SPEED); //car move forward with speed 180
}
void CAR_move(unsigned char direction, unsigned char speed_left, unsigned char speed_right)
{
switch (direction)
{
//car move forward with speed 180
case 0:
digitalWrite(MOTOR_L_1, HIGH); digitalWrite(MOTOR_L_2, LOW); //left motor clockwise rotation
digitalWrite(MOTOR_R_1, HIGH); digitalWrite(MOTOR_R_2, LOW); break; //right motor clockwise rotation
//car move back with speed 180
case 1:
digitalWrite(MOTOR_L_1, LOW); digitalWrite(MOTOR_L_2, HIGH);
digitalWrite(MOTOR_R_1, LOW); digitalWrite(MOTOR_R_2, HIGH); break;
default: break;
}
analogWrite(ENA_PIN, speed_left); //write speed_left to ENA_PIN,if speed_left is high,allow left motor rotate
analogWrite(ENB_PIN, speed_right); //write speed_right to ENB_PIN,if speed_right is high,allow right motor rotate
}
you have given the wrong code. its not for 4 sensors and its not working at all. you have mentioned 5 sensors in code and its just frustrating and making every thing vague and unclear
code is doing nothing but just to run motors continuously. all sensors are working but motors are not interactive to sensors.
Код не полный! И как указали выше он на 5 датчиков.
Вам нужно объединить код, если вы используете IR, вы указываете правильную конфигурацию и чувствительность.