we made a obstacle avoidance robot, and all the hardware setup is correct and we tested it out with a code online and it works perfectly, but we're supposed to write the code with port manipulation(DDRD,PORTD, ...) so we switched the code to it, and now only 1 wheel is working and i can't seem to find the bug.
The Left Forward motor is pin 7
Left backward motor is pin 6
right forward motor is pin 4
right backward motor is 5
This is our code(we wrote it ourselves, we just used the working one as reference)
#include <Servo.h>
#include <NewPing.h>
boolean goesForward = false;
int distance = 150;
#define trig_pin A1
#define echo_pin A2
#define maximum_distance 250
NewPing sonar(trig_pin, echo_pin, maximum_distance);
Servo servo_motor;
void setup(){
DDRD = B11110000;
servo_motor.attach(10);
servo_motor.write(115);
delay(500);
distance = readPing();
delay(200);
}
void loop(){
int distanceRight = 0;
int distanceLeft = 0;
delay(50);
if (distance <= 30){
moveStop();
delay(100);
moveBackward();
delay(900);
moveStop();
delay(300);
distanceRight = lookRight();
delay(200);
distanceLeft = lookLeft();
delay(200);
if (distanceRight >= distanceLeft){
turnRight();
delay(400);
moveStop();
}
else{
turnLeft();
delay(400);
moveStop();
}
}
else{
moveForward();
}
distance = readPing();
}
int lookRight(){
servo_motor.write(145);
delay(500);
int distance = readPing();
delay(500);
servo_motor.write(90);
return distance;
}
int lookLeft(){
servo_motor.write(35);
delay(500);
int distance = readPing();
delay(500);
servo_motor.write(90);
return distance;
}
int readPing(){
delay(75);
int cm = sonar.ping_cm();
if (cm==0){
cm=250;
}
return cm;
}
void moveStop(){
PORTD = B00000000;
}
void moveForward(){
PORTD = B10010000;
}
void moveBackward(){
PORTD = B01100000;
}
void turnRight(){
n
PORTD = B10100000;
}
void turnLeft(){
PORTD = B01010000;
}
And this is the code that worked
#include <Servo.h> //Servo motor library. This is standard library
#include <NewPing.h> //Ultrasonic sensor function library. You must install this library
//our L298N control pins
const int LeftMotorForward = 7;
const int LeftMotorBackward = 6;
const int RightMotorForward = 4;
const int RightMotorBackward = 5;
//sensor pins
#define trig_pin A1 //analog input 1
#define echo_pin A2 //analog input 2
#define maximum_distance 250
boolean goesForward = false;
int distance = 150;
NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor; //our servo name
void setup(){
pinMode(RightMotorBackward, OUTPUT);
pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
servo_motor.attach(10); //our servo pin
servo_motor.write(115);
delay(500);
distance = readPing();
delay(200);
}
void loop(){
int distanceRight = 0;
int distanceLeft = 0;
delay(50);
if (distance <= 30){
moveStop();
delay(100);
moveBackward();
delay(900);
moveStop();
delay(300);
distanceRight = lookRight();
delay(200);
distanceLeft = lookLeft();
delay(200);
if (distanceRight >= distanceLeft){
turnRight();
delay(400)
moveStop();
}
else{
turnLeft();
delay(400);
moveStop();
}
}
else{
moveForward();
}
distance = readPing();
}
int lookRight(){
servo_motor.write(145);
delay(500);
int distance = readPing();
delay(500);
servo_motor.write(90);
return distance;
}
int lookLeft(){
servo_motor.write(35);
delay(500);
int distance = readPing();
delay(500);
servo_motor.write(90);
return distance;
}
int readPing(){
delay(75);
int cm = sonar.ping_cm();
if (cm==0){
cm=250;
}
return cm;
}
void moveStop(){
digitalWrite(RightMotorForward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorBackward, LOW);
}
void moveForward(){
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
void moveBackward(){
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorForward, LOW);
}
void turnRight(){
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, LOW);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, HIGH);
}
void turnLeft(){
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorBackward, LOW);
}