Thanks! I changed my code completely following your other post and Planning and Implementing a Programm. Which I found extremely helpful.
This was the result;
int state=0;
const int trigPin1 = 10;
const int echoPin1 = 7;
// defines variables
long duration;
int distance;
int angle = 0;
int Fobstacle;
const int LeftMotor= 12;
const int RightMotor= 13;
const int LeftBrake= 9;
const int RightBrake= 8;
const int LeftGo= 3;
const int RightGo= 11;
void setup() {
//Setup Channels
pinMode(LeftMotor, OUTPUT); //Motor Channel A
pinMode(LeftBrake, OUTPUT); //Brake Channel A
pinMode(RightMotor, OUTPUT); //Motor Channel B
pinMode(RightBrake, OUTPUT); //Brake Channel B
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
digitalWrite(LeftMotor, HIGH);
digitalWrite(RightMotor, LOW); // Connection is inverted.
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
void loop() {
checkFrontObstacle();
checkDirection();
moveForward();
moveBackwards();
moveRight();
moveLeft();
nowStop();
turnRight();
turnLeft();
}
void moveForward() {
if (state == 'F' && Fobstacle == 'N') {
delayMicroseconds(100);
digitalWrite(LeftMotor, HIGH);
digitalWrite(LeftBrake, LOW); //Brake is off
digitalWrite(RightBrake, LOW); // Brake is off
digitalWrite(RightMotor, LOW);
analogWrite(LeftGo, 255); //Spins the motor on Channel A at full speed
analogWrite(RightGo, 255); //Spins the motor on Channel B at full speed
}
}
void moveBackwards() {
if (state == 'B' && Fobstacle == 'N' ){
delayMicroseconds(100);
digitalWrite(LeftMotor, LOW);
digitalWrite(LeftBrake, LOW);
digitalWrite(RightBrake, LOW);
digitalWrite(RightMotor, HIGH);
analogWrite(LeftGo, 255); //Spins the motor on Channel A at full speed
analogWrite(RightGo, 255); //Spins the motor on Channel B at full speed
}
if (state == 'B' && Fobstacle == 'C' ){
delayMicroseconds(100);
digitalWrite(LeftMotor, LOW);
digitalWrite(LeftBrake, LOW);
digitalWrite(RightBrake, LOW);
digitalWrite(RightMotor, HIGH);
analogWrite(LeftGo, 255); //Spins the motor on Channel A at full speed
analogWrite(RightGo, 255); //Spins the motor on Channel B at full speed
}
else if (Fobstacle == 'A'){
delayMicroseconds(100);
digitalWrite(LeftMotor, LOW);
digitalWrite(LeftBrake, LOW);
digitalWrite(RightBrake, LOW);
digitalWrite(RightMotor, HIGH);
analogWrite(LeftGo, 255); //Spins the motor on Channel A at full speed
analogWrite(RightGo, 255); //Spins the motor on Channel B at full speed
}
}
void moveRight() {
if (state == 'R'&& Fobstacle == 'N'){
delayMicroseconds(100);
digitalWrite(LeftMotor, HIGH);
digitalWrite(LeftBrake, LOW); //A
digitalWrite(RightBrake, LOW);//B
digitalWrite(RightMotor, LOW);//B
analogWrite(RightGo, 90); //Spins the motor on Channel B at full speed
analogWrite(LeftGo, 255); //Spins the motor on Channel A at low speed
}
}
void moveLeft() {
if (state == 'L' && Fobstacle == 'N'){
delayMicroseconds(100);
digitalWrite(LeftMotor, HIGH); //A
digitalWrite(LeftBrake, LOW); //A
digitalWrite(RightBrake, LOW);//B
digitalWrite(RightMotor, LOW);//B
analogWrite(LeftGo, 90); //Spins the motor on Channel A at low speed
analogWrite(RightGo, 255); //Spins the motor on Channel B at full speed
}
}
void turnRight() {
if(state == '3' && Fobstacle == 'N'){
delayMicroseconds(100);
digitalWrite(LeftMotor, HIGH); //A
digitalWrite(LeftBrake, LOW); //A
digitalWrite(RightBrake, LOW);//B
digitalWrite(RightMotor, HIGH);//B
analogWrite(LeftGo, 255); //Spins the motor on Channel A at full speed
analogWrite(RightGo, 255); //Spins the motor on Channel B at full speed
}
}
void turnLeft() {
if(state == '2' && Fobstacle == 'N'){
delayMicroseconds(100);
digitalWrite(LeftMotor, LOW); //A
digitalWrite(LeftBrake, LOW); //A
digitalWrite(RightBrake, LOW);//B
digitalWrite(RightMotor, LOW);//B
analogWrite(LeftGo, 255); //Spins the motor on Channel A at full speed
analogWrite(RightGo, 255); //Spins the motor on Channel B at full speed
}
}
void nowStop() {
if(state == '0'){
delayMicroseconds(100);
digitalWrite(LeftBrake, HIGH);
digitalWrite(RightBrake, HIGH);
}
}
void checkFrontObstacle() {
// Clears the trigPin1
digitalWrite(trigPin1, LOW);
delayMicroseconds(200);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin1, HIGH);
delayMicroseconds(100);
digitalWrite(trigPin1, LOW);
// Reads the echoPin1, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin1, HIGH);
// Calculating the distance
distance= duration*0.034/2;
//if some data is sent, reads it and saves in distance
if (distance > 40) {
Fobstacle = 'N'; // N for none
}
if (distance < 40 && distance > 20 ) {
Fobstacle = 'C'; // C for close
}
if (distance < 20 ) {
Fobstacle = 'A'; // A for approaching
}
}
void checkDirection(){
if(Serial.available() >= 0)
{
state = Serial.read();
}
}
Now I am able to make the car function properly, even when it comes to "If the Robot felt an obstacle from 40 to 20 centimeters away it just would be able to back up through the controls, that way it wouldn't be able to crash into something. "
However, when it comes to something "Advancing" towards the robot it goes two ways:
-
It goes completely haywire, to the point that doesn't respond to the phone commands.
-
Starts backing up and doesn't stop. Only if I tell it to with the phone. At that point when it does stop and I tell it to go forward, even if it had worked before, it starts going backward again, almost if this part of the code had been overwritten
void moveForward() {
if (state == 'F' && Fobstacle == 'N') {
delayMicroseconds(100);
digitalWrite(LeftMotor, HIGH);
digitalWrite(LeftBrake, LOW); //Brake is off
digitalWrite(RightBrake, LOW); // Brake is off
digitalWrite(RightMotor, LOW);
analogWrite(LeftGo, 255); //Spins the motor on Channel A at full speed
analogWrite(RightGo, 255); //Spins the motor on Channel B at full speed
}
}
by this other part
void moveBackwards() {
if (state == 'B' && Fobstacle == 'N' ){
delayMicroseconds(100);
digitalWrite(LeftMotor, LOW);
digitalWrite(LeftBrake, LOW);
digitalWrite(RightBrake, LOW);
digitalWrite(RightMotor, HIGH);
analogWrite(LeftGo, 255); //Spins the motor on Channel A at full speed
analogWrite(RightGo, 255); //Spins the motor on Channel B at full speed
}
if (state == 'B' && Fobstacle == 'C' ){
delayMicroseconds(100);
digitalWrite(LeftMotor, LOW);
digitalWrite(LeftBrake, LOW);
digitalWrite(RightBrake, LOW);
digitalWrite(RightMotor, HIGH);
analogWrite(LeftGo, 255); //Spins the motor on Channel A at full speed
analogWrite(RightGo, 255); //Spins the motor on Channel B at full speed
}
else if (Fobstacle == 'A'){
delayMicroseconds(100);
digitalWrite(LeftMotor, LOW);
digitalWrite(LeftBrake, LOW);
digitalWrite(RightBrake, LOW);
digitalWrite(RightMotor, HIGH);
analogWrite(LeftGo, 255); //Spins the motor on Channel A at full speed
analogWrite(RightGo, 255); //Spins the motor on Channel B at full speed
}
}
My guess is that I'm having problems with this part of the code
else if (Fobstacle == 'A'){
delayMicroseconds(100);
digitalWrite(LeftMotor, LOW);
digitalWrite(LeftBrake, LOW);
digitalWrite(RightBrake, LOW);
digitalWrite(RightMotor, HIGH);
analogWrite(LeftGo, 255); //Spins the motor on Channel A at full speed
analogWrite(RightGo, 255); //Spins the motor on Channel B at full speed
}
And so I have even tried to change it with using
else if (Fobstacle =='A' && state > 0){}
But that didn't work because I am trying to make it back up automatically until the distance is wide enough and this only makes it manual. However, it does make the robot work at all times.
I would appreciate any other kind of idea to make it work.
Thanks