Manual and automatic mode

I found a code on the internet and it works perfect except that it doesn’t go forward and look for an other obstacle (automatic mode).

#include <AFMotor.h>
#include <NewPing.h>
#include <Servo.h>
#define TRIG_PIN A14 
#define ECHO_PIN A15
#define MAX_DISTANCE 200 
#define MAX_SPEED 235 // sets speed of DC  motors
#define MAX_SPEED_OFFSET 20  
AF_DCMotor motor1(1); 
AF_DCMotor motor2(4); 
Servo myservo;
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); 
boolean goesForward=false;
int distance = 100;
int speedSet = 0;
char command;
const int onfar = 13;
int dugme=1;

void setup() 
{   
  pinMode(onfar, OUTPUT);
  myservo.attach(9);  
  myservo.write(115); 
  int readPing();
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  Serial.begin(9600);  //Set the baud rate to your Bluetooth module.
}




int readPing() { 
  delay(70);
  int cm = sonar.ping_cm();
  if(cm==0)
  {
    cm = 250;
  }
  return cm;
}
 
void forward()
{
  motor1.setSpeed(255); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor clockwise
  motor2.setSpeed(255); //Define maximum velocity
  motor2.run(FORWARD); //rotate the motor clockwise
}

void back()
{
  motor1.setSpeed(255); 
  motor1.run(BACKWARD); //rotate the motor counterclockwise
  motor2.setSpeed(255); 
  motor2.run(BACKWARD); //rotate the motor counterclockwise
}

void left()
{
  motor1.setSpeed(255); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor clockwise
  motor2.setSpeed(0);
  motor2.run(RELEASE); //turn motor2 off
}

void right()
{
  motor1.setSpeed(0);
  motor1.run(RELEASE); //turn motor1 off
  motor2.setSpeed(255); //Define maximum velocity
  motor2.run(FORWARD); //rotate the motor clockwise
}
void onsol()
{
  motor1.setSpeed(255); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor clockwise
  motor2.setSpeed(125); //Define maximum velocity
  motor2.run(FORWARD); //rotate the motor clockwise
}

void onsag()
{
  motor1.setSpeed(125); 
  motor1.run(FORWARD); //rotate the motor counterclockwise
  motor2.setSpeed(255); 
  motor2.run(FORWARD); //rotate the motor counterclockwise
}

void arkasol()
{
  motor1.setSpeed(125); //Define maximum velocity
  motor1.run(BACKWARD); //rotate the motor clockwise
  motor2.setSpeed(255);
  motor2.run(BACKWARD); //turn motor2 off
}

void arkasag()
{
  motor1.setSpeed(255);
  motor1.run(BACKWARD); //turn motor1 off
  motor2.setSpeed(125); //Define maximum velocity
  motor2.run(BACKWARD); //rotate the motor clockwise
}

void Stop()
{
  motor1.setSpeed(0);
  motor2.run(RELEASE); //turn motor1 off
  motor2.setSpeed(0);
  motor2.run(RELEASE); //turn motor2 off
}

void otokapa()
{
  dugme=0;
  motor1.setSpeed(0);
  motor2.run(RELEASE); //turn motor1 off
  motor2.setSpeed(0);
  motor2.run(RELEASE); //turn motor2 off
}


 
void onac()
{
    myservo.write(90); 
}
void onkapa()
{
    myservo.write(0); 
}

int lookRight()
{
    myservo.write(50); 
    delay(500);
    int distance = readPing();
    delay(100);
    myservo.write(115); 
    return distance;
}

int lookLeft()
{
    myservo.write(170); 
    delay(500);
    int distance = readPing();
    delay(100);
    myservo.write(115); 
    return distance;
    delay(100);
}



void moveStop() {
  motor1.run(RELEASE); 
  motor2.run(RELEASE);
  } 
  
void moveForward() {

 if(!goesForward)
  {
    goesForward=true;
    motor1.run(FORWARD);      
    motor2.run(FORWARD);   
   for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
   {
    motor1.setSpeed(speedSet);
    motor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
    delay(5);
   }
  }
}

void moveBackward() {
    goesForward=false;
    motor1.run(BACKWARD);      
    motor2.run(BACKWARD);  
  for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
  {
    motor1.setSpeed(speedSet);
    motor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
    delay(5);
  }
}  

void turnRight() {
  motor2.run(FORWARD);
  motor1.run(BACKWARD);     
  delay(300);
  motor2.run(FORWARD);      
  motor1.run(FORWARD);      
} 
 
void turnLeft() {
  motor2.run(BACKWARD);     
  motor1.run(FORWARD);     
  delay(300);
  motor2.run(FORWARD);     
  motor1.run(FORWARD);
}    

void otoac()
{
 long duration, distance; 
 int distanceR = 0;
 int distanceL =  0;
 delay(40);
 
 if(distance<=24)
 {
  moveStop();
  delay(100);
  moveBackward();
  delay(300);
  moveStop();
  delay(200);
  distanceR = lookRight();
  delay(200);
  distanceL = lookLeft();
  delay(200);
  if(distanceR>=distanceL)
  {
    turnRight();
    moveStop();
  }else
  {
    turnLeft();
    moveStop();
  }
 }else
 {
  moveForward();
 }
 distance = readPing();
 }

void loop(){
   if(Serial.available() > 0){ 
    command = Serial.read(); 
    Stop(); 
    switch(command){
    case 'F':  
      forward();
      break;
    case 'B':  
       back();
      break;
    case 'L':  
      left();
      break;
    case 'R':
      right();
      break;
    case 'G':  
      onsol();
      break;
    case 'I':  
      onsag();
      break;
    case 'H':  
      arkasag();
      break;
    case 'J':
      arkasol();
      break;
    case 'W':  
      onac();
      break;
    case 'w':  
      onkapa();
      break;
     case 'X':  
      otoac();
      break;
    case 'x':  
      otokapa();
      break;
    }
  } 
}

Can someone help me with this? I read something about someone that he used dugme and it worked for him but i didn’t understand where to put it. Hope that someone will have an answer. Thanks for making time to reply on my post. I changed some little things in the code to adapt to my robot(uploaded as attachment because of the maximum allowed length)

Greetings from Holland

Bluetooth_controoled___obstacle_avoiding_arduino_robot___park_s.ino (5.23 KB)

Someone told me on youtube to make a while loop but how do i make that? I dont even know that… I am trying to learn it now as i am a student. I want to show it to my teacher if it’s perfect so could someone explain?

How can i make this code

void otoac()
{
 long duration, distance; 
 int distanceR = 0;
 int distanceL =  0;
 delay(40);
 
 if(distance<=20)
 {
  moveStop();
  delay(100);
  moveBackward();
  delay(300);
  moveStop();
  delay(200);
  distanceR = lookRight();
  delay(200);
  distanceL = lookLeft();
  delay(200);
  if(distanceR>=distanceL)
  {
    turnRight();
    moveStop();
  }else
  {
    turnLeft();
    moveStop();
  }
 }else
 {
  moveForward();
 }
 distance = readPing();
}

Into an while loop?

First, read about functions(), that will shrink your code by about 80%
Which in turn will allow you to see how the various calls interact with each other.

A couple of well thought-out functions with one or two parameters will make your code much more readable. e.g.
byte runMotor(leftSpeed, rightSpeed, duration);
You may need to think about it.

Adding a for() or while() will become trivial once you understand your own code,

Looking at the code I see so many problems and enough errors that I wouldn’t work on it if it was for my project.

void otoac()
{
 long duration, distance;
 int distanceR = 0;
 int distanceL =  0;
 delay(40);
 
 if(distance<=20)
 {

Right there, distance is created as a long variable. The value when created will be whatever was left on the stack before, and then the code compares that to 20… you didn’t see that?

Every line, every command, every last number, letter or symbol in code COUNTS.
That is why to start with the simple, there is less to figure out every bit of!

Putting a loop in the otoac() function will only break the code more. The problem is in the serial input control part. Whatever key you press only gets acted upon one time per press.

Key commands only get done with a new char available(). What you want is that when no new key is pressed, the last command runs.

This might work but really that code is horrendous!

void loop(){
   if(Serial.available() > 0){
    command = Serial.read();  // does not check to make sure only a correct key was entered
   }
    Stop();   // why is this here? to "fix" a bug or just make the robot jerk about more?
    switch(command){
    case 'F': 
      forward();
      break;
    case 'B': 
       back();
      break;
    case 'L': 
      left();
      break;
    case 'R':
      right();
      break;
    case 'G': 
      onsol();
      break;
    case 'I': 
      onsag();
      break;
    case 'H': 
      arkasag();
      break;
    case 'J':
      arkasol();
      break;
    case 'W': 
      onac();
      break;
    case 'w': 
      onkapa();
      break;
     case 'X': 
      otoac();
      break;
    case 'x': 
      otokapa();
      break;
    }
}

I found a code on the internet

So many beginners think that finding a code is a good starting point for a project. But as you have found out it is simply a waste of time if you have not yet acquired the skill to read and understand code. You are trying to run before you can crawl. It is like taking a five year old's reading book off them and giving them a copy of "A short history of time" to read instead.

So everyone is saying that i need to learn more about the code. And that i didn’t research. Do you realise how many hours i am behind this code searching for an solution. That’s just not possible for a beginner like me. The only thing that i wanted is help before i have that class on monday, i can just ask it my teacher and he will explain. But i wanted the solution for this weekend with help from you guy’s. As if it is so difficult for you guy’s to code something that will make the code loop or something. Everyone is doing secret-society about that knowledge even a guy on youtube didn’t react anymore about making a while loop and making a contridiction in the command while loop. Is that code so hard to make for me? I wake up with hope that someone really helped me instead of bashing my post. So thankyou everyone, i will never post something after this on this crappy forum. I will ask it on stackoverflow or something where they react in a normal way!

The only thing that i wanted is help before i have that class on monday

No you did not want help, you wanted us to do it for you. That is not help.

from the forum rules at How to use this forum - please read. - #2 by nickgammon - Installation & Troubleshooting - Arduino Forum

we also don't like doing other people's homework for them, and we see those kinds of questions often as well at certain periodic moments (on this and other electronics forums we are on - we all have seen it, we know when the waves are about to start with people asking for homework help - we don't mind honest questions toward understanding, but we don't like being conned into doing someone's homework).

It was this simple btw. while(dugme==1)

void otoac()
{
long duration, distance;
while(dugme==1)
{ 
int distanceR = 0;
int distanceL =  0;
delay(40);
if(distance<=24)
{
 moveStop();
 delay(100);
 moveBackward();
 delay(300);
 moveStop();
 delay(200);
 distanceR = lookRight();
 delay(200);
 distanceL = lookLeft();
 delay(200);
 if(distanceR>=distanceL)
 {
   turnRight();
   moveStop();
 }else
 {
   turnLeft();
   moveStop();
 }
}else
{
 moveForward();
}
distance = readPing();
}
}

But how will i get out of the loop when i touch ‘x’:

void otokapa()
{
  dugme=0;
  motor1.setSpeed(0);
  motor2.run(RELEASE); //turn motor1 off
  motor2.setSpeed(0);
  motor2.run(RELEASE); //turn motor2 off
}

If it really is that simple, read up on -
while, for, do, loop, break and continue.
The answer should be hiding in there somewhere.
And you’ll learn something new as well.

Seriously though, programming is not the ability to see how many bandages will stick on top of each other, it requires planning, understanding and review - on top of coding, testing and debugging.

If “planning, understanding and review” are a secret society,the world is doomed ! (I’m not doubting it)

Gester1989:
Do you realise how many hours i am behind this code searching for an solution.

But you've obviously been doing it wrong. That's what we're trying to help you with. Instead of looking for a piece of code that does what you want, you should be looking at the reference of the while statement and try to understand that. If you would put your project aside for a moment and learn even the basics of the language first then you would be able to understand the code you have. If you don't understand it, then no amount of searching is going to help.

Gester1989:
But how will i get out of the loop when i touch 'x':

You would find the answer to that a lot quicker than posting on here, with a simple Google search;

'Arduino how do I exit a While loop'

You think i didn't try that? I tried google but i still dont understand the code so thats why i came here

What did you google? Did you try "Arduino reference" or "c++ tutorial"

From reply #8

void otoac()
{
long duration, distance;
while(dugme==1)
{ 
int distanceR = 0;
int distanceL =  0;
delay(40);
if(distance<=24)

What is the value of “distance” in that “if”?

It was this simple btw.

It (whatever " it" is) was never that simple.

Gester1989:
So everyone is saying that i need to learn more about the code. And that i didn’t research. Do you realise how many hours i am behind this code searching for an solution. That’s just not possible for a beginner like me. The only thing that i wanted is help before i have that class on monday, i can just ask it my teacher and he will explain. But i wanted the solution for this weekend with help from you guy’s. As if it is so difficult for you guy’s to code something that will make the code loop or something. Everyone is doing secret-society about that knowledge even a guy on youtube didn’t react anymore about making a while loop and making a contridiction in the command while loop. Is that code so hard to make for me? I wake up with hope that someone really helped me instead of bashing my post. So thankyou everyone, i will never post something after this on this crappy forum. I will ask it on stackoverflow or something where they react in a normal way!

I posted clearly about the code and not just why the auto does not continue but where and how to fix it.

I did post that you need to learn more about code but I GAVE YOU ANSWERS, NOT SECRET SOCIETY. Answers put before you, you don't get because you are not literate enough in C to understand.

You spent all those hours and still didn't learn to not waste time playing with code you don't understand? That should be the main lesson, that you need to do this differently as what you did do does not work so time to try a different approach.

Either you picked the project or it was assigned to you. Don't blame us for the mouthful you can't chew, you bit it off!

Cry to teacher. Cry to Stack Exchange. Maybe a Mommy will come wipe your tears but I can tell you one thing for sure: it won't make you any better at code, only practice and study will do which is the same as for all of us. No one loaded me with what I know, I had to DO in order to learn. Why are you so special that you don't?

Ditto. Reply #9 has the keywords you need.

Gester1989:
It was this simple btw. while(dugme==1)

void otoac()

{
long duration, distance;
while(dugme==1)
{
int distanceR = 0;
int distanceL =  0;
delay(40);
if(distance<=24)
{
moveStop();
delay(100);
moveBackward();
delay(300);
moveStop();
delay(200);
distanceR = lookRight();
delay(200);
distanceL = lookLeft();
delay(200);
if(distanceR>=distanceL)
{
  turnRight();
  moveStop();
}else
{
  turnLeft();
  moveStop();
}
}else
{
moveForward();
}
distance = readPing();
}
}




But how will i get out of the loop when i touch 'x':


void otokapa()
{
  dugme=0;
  motor1.setSpeed(0);
  motor2.run(RELEASE); //turn motor1 off
  motor2.setSpeed(0);
  motor2.run(RELEASE); //turn motor2 off
}

We all know how simple it is. But what you dont know is that you didnt want help. You wanted us to do it for you. There is a difference. Learn coding first and try doing this after. You are trying to speak a language that you dont know anything about. Coding is not using somebody elses code. It is building your own that can tell a machine what to do. Just learn the language before trying to speak.

It was only simple for the OP to dig deeper into the hole he is in.

Fixing that whole code is not so simple just because of how convoluted it is and the number of mistakes it has.
But who wants to spend time polishing a turd?

I just want that robot to work because it's cool to let it work no more than that. But oke, after the teacher gives met the code tomorrow i will learn some c++. Maybe if someone would give the code i would learn from it and i would have fun learning a code but the way you guy's react on some easy(easy for you)code is just stupid. I am doing this for 4 weeks and no one can give me just that 1 gift of an code. If i knew the code and i was helping people on a forum i would just give the code and tell that guy to learn the basics after that, DONE. But how you guy's react is almost arrogant. YOU KNOW that i don't know shit about the code and than you react with some "other language". Just give that word in another language and i will get interested and learn more about the language. That's all. That's what i mean with secret society-ish, just give the damn code. I am in school and learning C++ already but i am not yet there to know how to fix this problem, jeez -_-
And i am really a nice guy so i wish you all a good day. And thanks for your time typing your stuff.

Bye bye