Arduino related issues

Hello ,
I have built a obstacle avoiding robot …and this is the code

#include <Ultrasonic.h>
#include <Servo.h>

#define MOTOR_1_FORWARD 17
#define MOTOR_1_BACK 16
#define MOTOR_2_FORWARD 19
#define MOTOR_2_BACK 18

#define TRIG 5
#define ECHO 4

#define HEAD_SERVO 6

#define OBSTRACLE_DISTANCE 20.0
#define TURN_DELAY 500


#define LOG false

Ultrasonic ultrasonic(TRIG, ECHO);
Servo headServo;

int servoAngle = 90;
int angleStep = 30;

float distance = 0;

void setup()
{
  pinMode(MOTOR_1_FORWARD, OUTPUT);
  pinMode(MOTOR_1_BACK,    OUTPUT);
  pinMode(MOTOR_2_FORWARD, OUTPUT);
  pinMode(MOTOR_2_BACK,    OUTPUT);
  
  stopMove();
  
  headServo.attach(HEAD_SERVO); 
  
  if(LOG) Serial.begin( 9600 );
}

void loop()
{
  updateHeadAngle();
  
  checkDistance();
  
  moove();
  
  delay(10);
}

void checkDistance()
{
  distance = ultrasonic.Ranging(CM);
  if(LOG) Serial.println(distance); 
}

void moove()
{
  if( distance > OBSTRACLE_DISTANCE )
  {
    if(LOG) Serial.println("FORWARD"); 

goForward();
delay(200);  //TURN_DELAY
  }
  else
  {
    stopMove();
    
    checkObstracle();
  }
}

void checkObstracle()
{
  int obsLeft  = 0;
  int obsRight = 0;
  
  // Count the obstacles from left and right side
  for(servoAngle = 0; servoAngle <= 180; servoAngle += 30)
  {
    headServo.write(servoAngle);
    delay(200);
    
    checkDistance();
    if(distance < OBSTRACLE_DISTANCE && servoAngle < 90) obsLeft++;
    else if(distance < OBSTRACLE_DISTANCE) obsRight++;
  }
  
  if(LOG) Serial.print("TURN"); 
    
  if(obsLeft && obsRight)
  {
    goBack();
    
    delay(TURN_DELAY * 6);
    
    if(obsLeft < obsRight) goLeft();
    else goRight();
    
    delay(TURN_DELAY*6);
  }
  else if(obsRight)
  {
    goLeft();
    
    delay(TURN_DELAY*6);
  }
  else if(obsLeft)
  {
   goLeft();   // goRight
    
    delay(TURN_DELAY*6);
  }
  else
  {
    goForward();
    
    delay(TURN_DELAY);
  }
}

void updateHeadAngle()
{
  headServo.write(servoAngle);
  
  servoAngle += angleStep;
  
  if(servoAngle >= 150)
  {
    servoAngle = 150;
    
    angleStep *= -1;
  }
  
  if(servoAngle <= 30)
  {
    servoAngle = 30;
    
    angleStep *= -1;
  }
}

void goForward()
{
  digitalWrite(MOTOR_1_FORWARD, HIGH);
  digitalWrite(MOTOR_1_BACK,    LOW);
  digitalWrite(MOTOR_2_FORWARD, HIGH);
  digitalWrite(MOTOR_2_BACK,    LOW);
}

void goBack()
{
  digitalWrite(MOTOR_1_FORWARD, LOW);
  digitalWrite(MOTOR_1_BACK,    HIGH);
  digitalWrite(MOTOR_2_FORWARD, LOW);
  digitalWrite(MOTOR_2_BACK,    HIGH);
}

void goLeft()
{
  digitalWrite(MOTOR_1_FORWARD, LOW);
  digitalWrite(MOTOR_1_BACK,    HIGH);
  digitalWrite(MOTOR_2_FORWARD, HIGH);
  digitalWrite(MOTOR_2_BACK,    LOW);
}

void goRight()
{
  digitalWrite(MOTOR_1_FORWARD, HIGH);
  digitalWrite(MOTOR_1_BACK,    LOW);
  digitalWrite(MOTOR_2_FORWARD, LOW);
  digitalWrite(MOTOR_2_BACK,    HIGH);
}

void stopMove()
{
  digitalWrite(MOTOR_1_FORWARD, LOW);
  digitalWrite(MOTOR_1_BACK,    LOW);
  digitalWrite(MOTOR_2_FORWARD, LOW);
  digitalWrite(MOTOR_2_BACK,    LOW);
}  

The above code is the fully automated code. I want to make it automated/manual mode. If signals from additional sensor (I’ll define some extra sensor in pin 10/11 ) is high, It should come to manual mode. Then I’ll control it by bolt and below is manual code.

#include <BoltIoT-Arduino-Helper.h>
# define left_motor1    17
# define left_motor2    16
# define right_motor1   19
# define right_motor2   18
# define sensor         3
String command = "";
int delay_time = 250;

String check() {
  int sensor_value = digitalRead(sensor);
  Serial.println(sensor_value);
  Serial.println(command);
  if ((sensor_value == 0) && (command == "FORWARD")) {
  digitalWrite(left_motor2,LOW);
  digitalWrite(left_motor1,LOW);
  digitalWrite(right_motor1,LOW);
  digitalWrite(right_motor2,LOW);
  command = "";
  }
  return "STOP";
}

String forward(String *data) {
  command = "FORWARD";
  digitalWrite(left_motor1,HIGH);
  digitalWrite(left_motor2,LOW);
  digitalWrite(right_motor1,LOW);
  digitalWrite(right_motor2,HIGH);
  return "Success:Forward";
}

String backward(String *data) {
  command = "BACKWARD";
  digitalWrite(left_motor1,LOW);
  digitalWrite(left_motor2,HIGH);
  digitalWrite(right_motor1,HIGH);
  digitalWrite(right_motor2,LOW);
  return "Success:Backward";
}

String right(String *data) {
  command = "RIGHT";
  digitalWrite(left_motor1,LOW);
  digitalWrite(left_motor2,HIGH);
  digitalWrite(right_motor1,LOW);
  digitalWrite(right_motor2,HIGH);
  delay(delay_time);
  digitalWrite(left_motor2,LOW);
  digitalWrite(left_motor1,LOW);
  digitalWrite(right_motor1,LOW);
  digitalWrite(right_motor2,LOW);
  return "Success:Left";
}

String left(String *data) {
  command = "LEFT";
  digitalWrite(left_motor1,HIGH);
  digitalWrite(left_motor2,LOW);
  digitalWrite(right_motor1,HIGH);
  digitalWrite(right_motor2,LOW);
  delay(delay_time);
  digitalWrite(left_motor2,LOW);
  digitalWrite(left_motor1,LOW);
  digitalWrite(right_motor1,LOW);
  digitalWrite(right_motor2,LOW);
  return "Success:Right";
}

String stopcar(String *data) {
  digitalWrite(left_motor2,LOW);
  digitalWrite(left_motor1,LOW);
  digitalWrite(right_motor1,LOW);
  digitalWrite(right_motor2,LOW);
  return "Success:Stop";
}

void setup() {
  // put your setup code here, to run once:
  pinMode(left_motor1,OUTPUT);
  pinMode(left_motor2,OUTPUT);
  pinMode(right_motor1,OUTPUT);
  pinMode(right_motor2,OUTPUT);
  pinMode(sensor,INPUT);
  boltiot.begin(Serial);
  Serial.begin(9600);
  boltiot.setCommandString("FORWARD",forward);
  boltiot.setCommandString("BACKWARD",backward);
  boltiot.setCommandString("LEFT",left);
  boltiot.setCommandString("RIGHT",right);
  boltiot.setCommandString("STOP",stopcar);
}

void loop() {
  // put your main code here, to run repeatedly:
  boltiot.handleCommand();
  check();
}

tell me how to combine these to codes so that logic is as follows
If I define some sensor and take its input and if the sensor is high then automated mode or if the sensor is low then manual mode (via BoltIoT)

I tried a lot but I am getting repeatedly error…please help

In loop, call the function which updates the sensor value. This will keep on giving you the instantaneous values. Then use a while loop based on the sensor values. If the value is below the particular threshold(distance), then call a function which will control the bot automatically. If the distance is more than the threshold, call a separate function which enables the communication between Bolt and the Bot. Keep the code as fragmented as possible. It will help in debugging the code.

@vishal.dixit79
_can u please write up the code for me ._
because i am from mechanical background and got no more idea about coding but i can understand a written code…

Hi @saimech.rymec,

If I understand your question correctly, you want to combine the 2 code, and have a pin input define whether the Arduino does code1, or does code 2.

For this, do the following things.

Create a new sketch on Arduino.

Add the following lines. You can change the mode pin to which ever pin you want to connect the mode sensor to. I have used pin 11 for this in the following code.

#define mode_pin 11
void setup_automated();
void setup_manual();
void automated();
void manual();
void setup() {
  pinMode(mode_pin, INPUT);
  setup_automated();
  setup_manual();
  // put your setup code here, to run once:

}

void loop() {
  if(digitalRead(mode_pin){
    manual();
  }else{
    automated();
  }
  // put your main code here, to run repeatedly:
}

Add 2 tabs/files named automated.cpp and manual.cpp. Click here to find out how to add tabs/files in Arduino IDE

In the file named automated.cpp, add your code for the automated robot.

In the automated.cpp, change the name of the loop function to automated so:

void loop()
changes to
void automated()

In the automated.cpp, change the name of the setup function to setup_automated so:

void setup()
changes to
void setup_automated()

In the file named manual.cpp, add your code for the manual (BoltIoT controlled) robot.

In the manual.cpp, change the name of the loop function to manual so:

void loop()
changes to
void manual()

In the manual.cpp, change the name of the setup function to manual so:

void setup()
changes to
void setup_manual()

Compile and upload the code. It should work as expected.

hello sir … i did as u said but got an error which is too long …and error is

Arduino: 1.8.9 (Windows 10), Board: “Arduino/Genuino Uno”

sketch\automated.cpp: In function ‘void setup_automated()’:

automated.cpp:30:28: error: ‘OUTPUT’ was not declared in this scope

pinMode(MOTOR_1_FORWARD, OUTPUT);

                        ^

automated.cpp:30:34: error: ‘pinMode’ was not declared in this scope

pinMode(MOTOR_1_FORWARD, OUTPUT);

                              ^

automated.cpp:35:12: error: ‘stopMove’ was not declared in this scope

stopMove();

        ^

automated.cpp:39:11: error: ‘Serial’ was not declared in this scope

if(LOG) Serial.begin( 9600 );

       ^

sketch\automated.cpp: In function ‘void automated()’:

automated.cpp:44:19: error: ‘updateHeadAngle’ was not declared in this scope

updateHeadAngle();

               ^

automated.cpp:46:17: error: ‘checkDistance’ was not declared in this scope

checkDistance();

             ^

automated.cpp:48:9: error: ‘moove’ was not declared in this scope

moove();

     ^

automated.cpp:50:11: error: ‘delay’ was not declared in this scope

delay(10);

       ^

sketch\automated.cpp: In function ‘void checkDistance()’:

automated.cpp:56:11: error: ‘Serial’ was not declared in this scope

if(LOG) Serial.println(distance);

       ^

sketch\automated.cpp: In function ‘void moove()’:

automated.cpp:63:13: error: ‘Serial’ was not declared in this scope

 if(LOG) Serial.println("FORWARD"); 

         ^

automated.cpp:65:15: error: ‘goForward’ was not declared in this scope

 goForward();

           ^

automated.cpp:66:14: error: ‘delay’ was not declared in this scope

 delay(200);  //TURN_DELAY

          ^

automated.cpp:70:14: error: ‘stopMove’ was not declared in this scope

 stopMove();

          ^

automated.cpp:72:20: error: ‘checkObstracle’ was not declared in this scope

 checkObstracle();

                ^

sketch\automated.cpp: In function ‘void checkObstracle()’:

automated.cpp:85:14: error: ‘delay’ was not declared in this scope

 delay(200);

          ^

automated.cpp:92:11: error: ‘Serial’ was not declared in this scope

if(LOG) Serial.print(“TURN”);

       ^

automated.cpp:96:12: error: ‘goBack’ was not declared in this scope

 goBack();

        ^

automated.cpp:98:25: error: ‘delay’ was not declared in this scope

 delay(TURN_DELAY * 6);

                     ^

automated.cpp:100:35: error: ‘goLeft’ was not declared in this scope

 if(obsLeft < obsRight) goLeft();

                               ^

automated.cpp:101:18: error: ‘goRight’ was not declared in this scope

 else goRight();

              ^

automated.cpp:107:12: error: ‘goLeft’ was not declared in this scope

 goLeft();

        ^

automated.cpp:109:23: error: ‘delay’ was not declared in this scope

 delay(TURN_DELAY*6);

                   ^

automated.cpp:113:11: error: ‘goLeft’ was not declared in this scope

goLeft();   // goRight

       ^

automated.cpp:115:23: error: ‘delay’ was not declared in this scope

 delay(TURN_DELAY*6);

                   ^

automated.cpp:119:15: error: ‘goForward’ was not declared in this scope

 goForward();

           ^

automated.cpp:121:21: error: ‘delay’ was not declared in this scope

 delay(TURN_DELAY);

                 ^

sketch\automated.cpp: In function ‘void goForward()’:

automated.cpp:148:33: error: ‘HIGH’ was not declared in this scope

digitalWrite(MOTOR_1_FORWARD, HIGH);

                             ^

automated.cpp:148:37: error: ‘digitalWrite’ was not declared in this scope

digitalWrite(MOTOR_1_FORWARD, HIGH);

                                 ^

automated.cpp:149:33: error: ‘LOW’ was not declared in this scope

digitalWrite(MOTOR_1_BACK, LOW);

                             ^

sketch\automated.cpp: In function ‘void goBack()’:

automated.cpp:156:33: error: ‘LOW’ was not declared in this scope

digitalWrite(MOTOR_1_FORWARD, LOW);

                             ^

automated.cpp:156:36: error: ‘digitalWrite’ was not declared in this scope

digitalWrite(MOTOR_1_FORWARD, LOW);

                                ^

automated.cpp:157:33: error: ‘HIGH’ was not declared in this scope

digitalWrite(MOTOR_1_BACK, HIGH);

                             ^

sketch\automated.cpp: In function ‘void goLeft()’:

automated.cpp:164:33: error: ‘LOW’ was not declared in this scope

digitalWrite(MOTOR_1_FORWARD, LOW);

                             ^

automated.cpp:164:36: error: ‘digitalWrite’ was not declared in this scope

digitalWrite(MOTOR_1_FORWARD, LOW);

                                ^

automated.cpp:165:33: error: ‘HIGH’ was not declared in this scope

digitalWrite(MOTOR_1_BACK, HIGH);

                             ^

sketch\automated.cpp: In function ‘void goRight()’:

automated.cpp:172:33: error: ‘HIGH’ was not declared in this scope

digitalWrite(MOTOR_1_FORWARD, HIGH);

                             ^

automated.cpp:172:37: error: ‘digitalWrite’ was not declared in this scope

digitalWrite(MOTOR_1_FORWARD, HIGH);

                                 ^

automated.cpp:173:33: error: ‘LOW’ was not declared in this scope

digitalWrite(MOTOR_1_BACK, LOW);

                             ^

sketch\automated.cpp: In function ‘void stopMove()’:

automated.cpp:180:33: error: ‘LOW’ was not declared in this scope

digitalWrite(MOTOR_1_FORWARD, LOW);

                             ^

automated.cpp:180:36: error: ‘digitalWrite’ was not declared in this scope

digitalWrite(MOTOR_1_FORWARD, LOW);

                                ^

E:\rover\rover.ino: In function ‘void loop()’:

rover:17:27: error: expected ‘)’ before ‘{’ token

if(digitalRead(mode_pin){

                       ^

rover:23:1: error: expected primary-expression before ‘}’ token

}

^

Multiple libraries were found for “Ultrasonic.h”
Used: C:\Program Files (x86)\Arduino\libraries\Ultrasonic
Not used: C:\Users\USER\Documents\Arduino\libraries\Ultrasonic-master
Not used: C:\Users\USER\Documents\Arduino\libraries\Grove_Ultrasonic_Ranger
Not used: C:\Program Files (x86)\Arduino\libraries\Ultrasonic-2.1.0
sketch\manual.cpp: In function ‘String check()’:

manual.cpp:11:40: error: ‘digitalRead’ was not declared in this scope

int sensor_value = digitalRead(sensor);

                                    ^

manual.cpp:15:28: error: ‘LOW’ was not declared in this scope

digitalWrite(left_motor2,LOW);

                        ^

manual.cpp:15:31: error: ‘digitalWrite’ was not declared in this scope

digitalWrite(left_motor2,LOW);

                           ^

sketch\manual.cpp: In function ‘String forward(String*)’:

manual.cpp:26:28: error: ‘HIGH’ was not declared in this scope

digitalWrite(left_motor1,HIGH);

                        ^

manual.cpp:26:32: error: ‘digitalWrite’ was not declared in this scope

digitalWrite(left_motor1,HIGH);

                            ^

manual.cpp:27:28: error: ‘LOW’ was not declared in this scope

digitalWrite(left_motor2,LOW);

                        ^

sketch\manual.cpp: In function ‘String backward(String*)’:

manual.cpp:35:28: error: ‘LOW’ was not declared in this scope

digitalWrite(left_motor1,LOW);

                        ^

manual.cpp:35:31: error: ‘digitalWrite’ was not declared in this scope

digitalWrite(left_motor1,LOW);

                           ^

manual.cpp:36:28: error: ‘HIGH’ was not declared in this scope

digitalWrite(left_motor2,HIGH);

                        ^

sketch\manual.cpp: In function ‘String right(String*)’:

manual.cpp:44:28: error: ‘LOW’ was not declared in this scope

digitalWrite(left_motor1,LOW);

                        ^

manual.cpp:44:31: error: ‘digitalWrite’ was not declared in this scope

digitalWrite(left_motor1,LOW);

                           ^

manual.cpp:45:28: error: ‘HIGH’ was not declared in this scope

digitalWrite(left_motor2,HIGH);

                        ^

manual.cpp:48:19: error: ‘delay’ was not declared in this scope

delay(delay_time);

               ^

sketch\manual.cpp: In function ‘String left(String*)’:

manual.cpp:58:28: error: ‘HIGH’ was not declared in this scope

digitalWrite(left_motor1,HIGH);

                        ^

manual.cpp:58:32: error: ‘digitalWrite’ was not declared in this scope

digitalWrite(left_motor1,HIGH);

                            ^

manual.cpp:59:28: error: ‘LOW’ was not declared in this scope

digitalWrite(left_motor2,LOW);

                        ^

manual.cpp:62:19: error: ‘delay’ was not declared in this scope

delay(delay_time);

               ^

sketch\manual.cpp: In function ‘String stopcar(String*)’:

manual.cpp:71:28: error: ‘LOW’ was not declared in this scope

digitalWrite(left_motor2,LOW);

                        ^

manual.cpp:71:31: error: ‘digitalWrite’ was not declared in this scope

digitalWrite(left_motor2,LOW);

                           ^

sketch\manual.cpp: In function ‘void setup_manual()’:

manual.cpp:80:23: error: ‘OUTPUT’ was not declared in this scope

pinMode(left_motor1,OUTPUT);

                   ^

manual.cpp:80:29: error: ‘pinMode’ was not declared in this scope

pinMode(left_motor1,OUTPUT);

                         ^

manual.cpp:84:18: error: ‘INPUT’ was not declared in this scope

pinMode(sensor,INPUT);

              ^

exit status 1
‘OUTPUT’ was not declared in this scope

This report would have more information with
“Show verbose output during compilation”
option enabled in File -> Preferences.

@vinayak.joshi
I am getting error …like not declared in this scope

Hi @saimech.rymec,

It seems you are facing standard C++ syntax issues.

First start out with adding the line at the start of both automated.cpp and manual.cpp

include <Arduino.h>

Next compile the code again, and you will face less errors. You have to look at these error, and accordingly fix them.

Then you will have to look at the error. I think you will face the below errors for automated.cpp. The error will say that a certin function was not declared, for example

automated.cpp:100:35: error: ‘goLeft’ was not declared in this scope

Here, you just go to the top of the automated.cpp file, and add the following line

void goLeft(void);

Ensure that you replace the correct function name.

Finally, in the code that I had shared earlier, to be put in the loop function, there is a single line correction.

change the following line

if(digitalRead(mode_pin){

to

if(digitalRead(mode_pin)){

Notice that I have added an extra closing bracket ‘)’

@vinayak.joshi thank you for reply …errors got reduced…
but these errors, i am not understanding …please help…
below is the error message…

sketch\manual.cpp.o (symbol from plugin): In function `forward(String*)’:

(.text+0x0): multiple definition of `setup’

sketch\automated.cpp.o (symbol from plugin):(.text+0x0): first defined here

sketch\manual.cpp.o (symbol from plugin): In function `forward(String*)’:

(.text+0x0): multiple definition of `loop’

sketch\automated.cpp.o (symbol from plugin):(.text+0x0): first defined here

sketch\rover.ino.cpp.o (symbol from plugin): In function `setup’:

(.text+0x0): multiple definition of `setup’

sketch\automated.cpp.o (symbol from plugin):(.text+0x0): first defined here

sketch\rover.ino.cpp.o (symbol from plugin): In function `setup’:

(.text+0x0): multiple definition of `loop’

sketch\automated.cpp.o (symbol from plugin):(.text+0x0): first defined here

collect2.exe: error: ld returned 1 exit status

Multiple libraries were found for “Ultrasonic.h”
Used: C:\Program Files (x86)\Arduino\libraries\Ultrasonic
Not used: C:\Users\USER\Documents\Arduino\libraries\Ultrasonic-master
Not used: C:\Users\USER\Documents\Arduino\libraries\Grove_Ultrasonic_Ranger
exit status 1
Error compiling for board Arduino/Genuino Uno.

multiple definition ofsetup’`

The above error means, that you have the following code in multiple places. Probably 1 in automated.cpp, 1 in manual.cpp and 1 in the main code of the new Arduino sketch.

void setup(){

As I had mentioned to you in my first post, you need to replace the above code present in automated.cpp and manual.cpp.