Sunteți pe pagina 1din 3

#define lMotorAnode 8

#define lMotorCathode 7
#define rMotorAnode 13
#define rMotorCathode 12
#define vhighSpeed 255
#define highSpeed 255
#define lowSpeed 50
#define vlowSpeed 25

const int rSpeed = 11;


const int lSpeed = 6;

int irRight = 0;
int irRiiight = 0;
int irLeft = 0;
int irLeeeft = 0;

const int irRightPin = 5;


const int irRiiightPin = 3;
const int irLeftPin = 4;
const int irLeeeftPin = 2;

void setup()
{
//Serial.begin(9600);
pinMode(lMotorCathode, OUTPUT);
pinMode(lMotorAnode, OUTPUT);
pinMode(lMotorCathode, OUTPUT);
pinMode(lMotorAnode, OUTPUT);

pinMode(irRightPin, INPUT);
pinMode(irRiiightPin, INPUT);
pinMode(irLeftPin, INPUT);
pinMode(irLeeeftPin, INPUT);

pinMode(rSpeed,OUTPUT);
pinMode(lSpeed,OUTPUT);

digitalWrite(rMotorCathode, LOW);
digitalWrite(rMotorAnode, LOW);
digitalWrite(lMotorCathode, LOW);
digitalWrite(lMotorAnode, LOW);
}

void loop()
{
readSensors();

//Serial.println(irRight);
if (irLeeeft == 0 && irLeft == 1 && irRight == 1 && irRiiight == 0)
{
motorOn();
straight();
}
else if (irLeeeft == 1 && irLeft == 1 && irRight == 1 && irRiiight == 0)
{
motorOn();
leftTurn();
}
else if (irLeeeft == 1 && /*irLeft == 1 &&*/ irRight == 0 && irRiiight == 0)
{
motorOn();
hardLeftTurn();
}
else if (irLeeeft == 0 && irLeft == 1 && irRight == 1 && irRiiight == 1)
{
motorOn();
rightTurn();
}
else if (irLeeeft == 0 && irLeft == 0 /*&& irRight == 1*/ && irRiiight == 1)
{
motorOn();
hardRightTurn();
}
else if (irLeeeft == 1 && irLeft == 1 && irRight == 1 && irRiiight == 1)
{
motorOn();
off();
}
else if (irLeeeft == 0 && irLeft == 0 && irRight == 0 && irRiiight == 0)
{
straight();
delay(100);
off();
}
}
void motorOn()
{
digitalWrite(rMotorCathode, HIGH);
digitalWrite(rMotorAnode, LOW);
digitalWrite(lMotorCathode, HIGH);
digitalWrite(lMotorAnode, LOW);
}

void straight()
{
analogWrite(rSpeed, highSpeed);
analogWrite(lSpeed, highSpeed);
}

void leftTurn()
{
analogWrite(rSpeed, highSpeed);
analogWrite(lSpeed, lowSpeed);

void hardLeftTurn()
{
analogWrite(rSpeed, vhighSpeed);
analogWrite(lSpeed, vlowSpeed);

void rightTurn()
{
analogWrite(rSpeed, lowSpeed);
analogWrite(lSpeed, highSpeed);
}

void hardRightTurn()
{
analogWrite(rSpeed, vlowSpeed);
analogWrite(lSpeed, vhighSpeed);

void off()
{
digitalWrite(rMotorCathode, LOW);
digitalWrite(rMotorAnode, LOW);
digitalWrite(lMotorCathode, LOW);
digitalWrite(lMotorAnode, LOW);
}

void readSensors()
{
irLeft = digitalRead(irLeftPin);
irLeeeft = digitalRead(irLeeeftPin);
irRight = digitalRead(irRightPin);
irRiiight = digitalRead(irRiiightPin);
}

S-ar putea să vă placă și