I have a project that have to make a Maze Solver using 5 sensor and 2 motor . I have code it but when I upload the right motor keep spinning but the left motor is not moveing , when any of the sensor is on the white line it just stop.
#include<util/delay.h>
int LeftOutSensor; // val for Sensor 1 P2
int LeftCentreSensor; // val for Sensor 2 P3
int val3; // val for Sensor 3 P4
int RightCentreSensor; // val for Sensor 4 P5
int RightOutSensor; // val for Sensor 5 P6
int leftNudge;
int replaystage;
int rightNudge;
char path[30] = {};
int pathLength;
int readLength;
void setup()
{
DDRE &= ~_BV(DDE4); // Make Pin 2 PE4 as an input Sensor 1
DDRE &= ~_BV(DDE5); // Make Pin 3 PE5 as an input Sensor 2
DDRG &= ~_BV(DDG5); // Make Pin 4 PG5 as an input Sensor 3
DDRE &= ~_BV(DDE3); // Make Pin 5 PE3 as an input Sensor 4
DDRH &= ~_BV(DDH3); // Make Pin 6 PH3 as an input Sensor 5
DDRB = 0x30; //Make PB4 PB5 as an OUTPUT Pin 10 Pin 11 for LeftMotor
DDRH = 0x60; //Make PH5 PH6 as an OUTPUT Pin 8 Pin 9 For RightMotor
DDRB & 0b10000000; //Make PB7 an output pin
_delay_ms(1000);
}
void loop(){
readSensors();
if(LeftOutSensor>4 && RightOutSensor>4 && (LeftCentreSensor >4 || RightCentreSensor>4)) //If we do not see a left or right turn and at least 1 of our mid sensor see
{ // the black line , keep going straight
straight();
}
else{
leftHandWall();
}
}
void readSensors(){
LeftOutSensor = PINE & _BV(PE4);
LeftCentreSensor = PINE & _BV(PE5);
val3 = PING & _BV(PG5);
RightCentreSensor = PINE & _BV(PE3);
RightOutSensor = PINH & _BV(PH3);
}
void leftHandWall(){
if(LeftOutSensor<4 && RightOutSensor<4){ // If S1 and S5 are on the black line
HLHL();
_delay_ms(250);
readSensors(); //recheck the sensor
if(LeftOutSensor<4 || RightOutSensor<4){ // if S1 and S5 are on the white
done();
}
if(LeftOutSensor>4 && RightOutSensor>4){ // If both S1 and S5 are on the white which mean need to turn left
turnLeft();
}
}
if (LeftOutSensor < 4 )
{
HLHL();
_delay_ms(250);
readSensors();
if(LeftOutSensor>4 && RightOutSensor>4) // both S1 S5 on white turn left
{
turnLeft();
}
else
{
done();
}
}
if(RightOutSensor<4)
{
_delay_ms(10);
readSensors;
if(LeftOutSensor<4)
{
_delay_ms(240);
readSensors;
if(RightOutSensor<4 && LeftOutSensor<4) // if all are Black
{
done();
}
else
{
turnLeft();
return;
}
}
_delay_ms(240);
readSensors();
if(LeftOutSensor>4 && LeftCentreSensor<4 && RightCentreSensor<4 && RightOutSensor>4) //all are white right turn
{
turnRight();
return;
}
path[pathLength]='S';
pathLength++;
if(path[pathLength-2]=='B')
{
shortPath();
}
straight();
}
readSensors();
if(LeftOutSensor>4 && LeftCentreSensor<4 && val3 <4 && RightCentreSensor<4 && RightOutSensor>4)
{
turnAround();
}
}
void done()
{
LLLL();
replaystage=1;
path[pathLength]='D';
pathLength++;
while(LeftOutSensor<4){
PORTB = PORTB | 0b10000000; //Set PB7 to turn on LED
_delay_ms(150);
PORTB = PORTB & 0b01111111; //Clear PB7 to turn off LED
_delay_ms(150);
}
_delay_ms(1000);
replay();
}
void turnLeft()
{
while(RightCentreSensor>4 || LeftCentreSensor>4){
LHHL();
_delay_ms(500);
LLLL();
delay(250);
}
while(RightCentreSensor<4){
LHHL();
_delay_ms(500);
LLLL();
_delay_ms(250);
}
if(replaystage==0){
path[pathLength]='L';
pathLength++;
if(path[pathLength-2]=='B'){
shortPath();
}
}
}
void turnRight()
{
while(RightOutSensor>4)
{
HLLH();
_delay_ms(500);
LLLL();
_delay_ms(250);
}
while(RightCentreSensor<4)
{
HLLH();
_delay_ms(500);
LLLL();
_delay_ms(250);
}
if(replaystage==0){
path[pathLength]='R';
pathLength++;
if(path[pathLength-2]=='B'){
shortPath();
}
}
straight();
}
void straight(){
if(LeftCentreSensor<4)
{
HLHL();
_delay_ms(200);
HLLL();
_delay_ms(250);
}
if(RightCentreSensor<4)
{
HLHL();
_delay_ms(200);
LLHL();
_delay_ms(250);
}
else
{
HLHL();
}
}
void turnAround()
{
HLHL();
_delay_ms(250);
while(LeftOutSensor>4)
{
LHHL();
_delay_ms(250);
LLLL();
_delay_ms(250);
}
while(LeftCentreSensor<4){
LHHL();
}
path[pathLength]='B';
pathLength++;
straight();
}
void shortPath()
{
int shortDone=0;
if(path[pathLength-3]=='L' && path[pathLength-1]=='R')
{
pathLength-=3;
path[pathLength]='B';
shortDone=1;
}
if(path[pathLength-3]=='L' && path[pathLength-1]=='S' && shortDone==0)
{
pathLength-=3;
path[pathLength]='R';
shortDone=1;
}
if(path[pathLength-3]=='R' && path[pathLength-1]=='L' && shortDone==0)
{
pathLength-=3;
path[pathLength]='B';
shortDone=1;
}
if(path[pathLength-3]=='S' && path[pathLength-1]=='L' && shortDone==0)
{
pathLength-=3;
path[pathLength]='R';
shortDone=1;
}
if(path[pathLength-3]=='S' && path[pathLength-1]=='S' && shortDone==0)
{
pathLength-=3;
path[pathLength]='B';
shortDone=1;
}
if(path[pathLength-3]=='L' && path[pathLength-1]=='L' && shortDone==0)
{
pathLength-=3;
path[pathLength]='S';
shortDone=1;
}
path[pathLength+1]='D';
path[pathLength+2]='D';
pathLength++;
}
void replay()
{
readSensors();
if(LeftOutSensor>4 && RightOutSensor>4)
{
straight();
}
else
{
if(path[readLength]=='D')
{
HLHL();
_delay_ms(250);
LLLL();
endMotion();
}
if(path[readLength]=='L')
{
HLHL();
_delay_ms(250);
turnLeft();
}
if(path[readLength]=='R')
{
HLHL();
_delay_ms(250);
turnRight();
}
if(path[readLength]=='S')
{
HLHL();
_delay_ms(250);
straight();
}
readLength++;
}
replay();
}
void endMotion()
{
PORTB = PORTB | 0b10000000; //Set PB7 to turn on LED
_delay_ms(500);
PORTB = PORTB & 0b01111111; //Clear PB7 to turn off LED
_delay_ms(200);
PORTB = PORTB | 0b10000000; //Set PB7 to turn on LED
_delay_ms(200);
PORTB = PORTB & 0b01111111; //Clear PB7 to turn off LED
_delay_ms(500);
}
void HLHL()
{
PORTB &= 0xCF; // Make PB4=0 and PB5=0 FF
PORTB |= 0x20; // on the line, roatate motor clockwise PB5=1, PB4=0 OF
PORTH &= 0x9F; //Make PH6=0 and PH5=0 FF
PORTH |= 0x40; //Not on the line, rotate motor clockwise PH6=1 and PH5=0 OF
}
void LLLL()
{
PORTB &= 0xCF; // Make PB4=0 and PB5=0 FF
PORTB |= 0xCF; // stop motor
PORTH &= 0x9F; //Make PH6=0 and PH5=0 FF
PORTH |= 0x9F; // stop motor
}
void HLLH()
{
PORTB &= 0xCF; // Make PB4=0 and PB5=0 FF
PORTB |= 0x20; // on the line, roatate motor clockwise PB5=1, PB4=0 OF
PORTH &= 0x9F; //Make PH6=0 and PH5=0 FF
PORTH |= 0x20; //Not on the line, rotate motor anticlockwise PH6=0 and PH5=1 OF
}
void LHHL()
{
PORTB &= 0xCF; // Make PB4=0 and PB5=0 FF
PORTB |= 0x10; // on the line, roatate motor anticlockwise PB5=0, PB4=1 OF
PORTH &= 0x9F; //Make PH6=0 and PH5=0 FF
PORTH |= 0x40; //Not on the line, rotate motor clockwise PH6=1 and PH5=0 OF
}
void LLHL()
{
PORTB &= 0xCF; // Make PB4=0 and PB5=0 FF
PORTB |= 0xCF; // stop motor
PORTH &= 0x9F; //Make PH6=0 and PH5=0 FF
PORTH |= 0x40; //Not on the line, rotate motor anticlockwise PH6=1 and PH5=0 OF
}
void HLLL()
{
PORTB &= 0xCF; // Make PB4=0 and PB5=0 FF
PORTB |= 0x20; // on the line, roatate motor clockwise PB5=1, PB4=0 OF
PORTH &= 0x9F; //Make PH6=0 and PH5=0 FF
PORTH |= 0x9F; // stop motor
}
Please help me , im a noob at this stuff . Thanks for the help
I have test single code in it like void turnLeft , void turnLeft, void turnRight, void Straight...etc
But when put them up together like that it doesnt work .
I think is a logical problem