My robot is not working

Hi,
I am a beginner in arduino.
I was creating a robot i connected all wires and uploaded the code as given below.
I think that arduino uno is not taking code and not responding but light is glowing.

#include <AFMotor.h>
#include <NewPing.h>
#include<Servo.h>
#define TRIGGER_PIN A1
#define ECHO_PIN A0
#define MAX_DISTANCE 300
#define IR A5

AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);



NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

Servo myservo;

String voice;

void setup() {
Serial.begin(32256);
myservo.attach(10);
myservo.write(90);
pinMode(IR, INPUT);

}

void loop() {
  int distance = sonar.ping_cm();
  //int IR1 = digitalRead(IR);
  //Serial.println(IR1);
  
if(Serial.available()>0) {
  voice="";
  delay(2);
  voice = Serial.readString();
  delay(2);
  Serial.println(voice);

  

if (voice == "turn left") {
  left();
}else if (voice == "left") {
  left();
}else if(voice == "turn right") {
  right();
}else if(voice == "right") {
  right();
}
 
}
while(voice == "move forward") {
  forward();
}
while(voice == "move backward") {
  backward();
}
}



void forward() {
  int distance = sonar.ping_cm();
  
 if(distance < 10){
  Stop();
  voice="";
 }else {
  motor1.setSpeed(255); 
  motor1.run(FORWARD); 
  motor2.setSpeed(255);
  motor2.run(FORWARD);
  motor3.setSpeed(255); 
  motor3.run(FORWARD); 
  motor4.setSpeed(255); 
  motor4.run(FORWARD); 
}
}
void backward() {
  int IR_Sensor = digitalRead(IR);
  if(IR_Sensor == 0) {
    Stop();
    voice="";
  }else {
  motor1.setSpeed(255); 
  motor1.run(BACKWARD); 
  motor2.setSpeed(255); 
  motor2.run(BACKWARD); 
  motor3.setSpeed(255); 
  motor3.run(BACKWARD);
  motor4.setSpeed(255); 
  motor4.run(BACKWARD); 
 
}
}
void left() {
  myservo.write(180);
  delay(500);
  myservo.write(90);
  delay(500);
  motor1.run(BACKWARD);
  motor1.setSpeed(255);
  motor2.run(BACKWARD);
  motor2.setSpeed(255);
  motor3.run(FORWARD);
  motor3.setSpeed(255);
  motor4.run(FORWARD);
  motor4.setSpeed(255);
  delay(700);
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}

void right() {
  myservo.write(0);
  delay(500);
  myservo.write(90);
  delay(500);
  motor1.run(FORWARD);
  motor1.setSpeed(255);
  motor2.run(FORWARD);
  motor2.setSpeed(255);
  motor3.run(BACKWARD);
  motor3.setSpeed(255);
  motor4.run(BACKWARD);
  motor4.setSpeed(255);
  delay(700);
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}

void Stop() {
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}

It is not working .
here is its image below

Please advice me .
Thank you
Chirayu

The IDE will tell you if the upload is successful or not.

Did it?

"It is not working" is just about the most annoying, pointless thing you could post on this forum

(I would edit-out your email address if I were you)

IDE was showing that code was successfully uploaded .but Not taking voice commands and not working

If the IDE told you that the code had uploaded,why did you write

?

Again, really really annoying.

Get your code to tell you what it is doing, by adding some serial prints, or some blinking lights

Please guide how to write some serial prints or some blinking lights .
Because i am a 7th class student and doing for the first time

That's a very unusual rate - did you write this code?
If not, do you have a link to where you found it?

Hi, @chirayu1609robot
Welcome to the forum.
Thankyou for using code tags to format your posted code.

How do you talk to the robot, using the IDE monitor?
If so check that its baud rate is set to 32256 that is declared in the code.

Did you write this code or get it and the robot details elsewhere?
If so, can you post links to where your code and robot details came from?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

i found this through YouTube
link : https://youtu.be/1lrf50hBsHA
and code is in its YouTube description

Yes in IDE monitor and also checked through application but didn't got response

I found code in YouTube
link : https://youtu.be/1lrf50hBsHA

Is the unusual serial bit rate an anti-plagiarism marker?

Before this did you try any other code on your robot, to check out the individual subsystems independently?

HI,
Change

Serial.begin(32256);

To

Serial.begin(115200);

And change the IDE monitor speed as well.
When you type left turn
then press enter, your should get that line back in the receive screen.

Tom... :grinning: :+1: :coffee: :australia:

1 Like

I am sorry ,
I tried but no luck
image given below

I talk using Bluetooth voice command

Hi,
So the code is responding, it has send back your command.

Can you please post a circuit diagram of your connections.
A pen(cil) and paper will be fine, make sure it is of your project, reverse engineer.

What are your motors?
Do you have a DMM?

Tom... :grinning: :+1: :coffee: :australia:

ok just a second

HI,
Try this edited code, it should make the motors;
Go forward for 1s
Stop for 1/2s
Go backwards for 1s
Stop for 1/2s
and repeat.

#include <AFMotor.h>
#include <NewPing.h>
#include<Servo.h>
#define TRIGGER_PIN A1
#define ECHO_PIN A0
#define MAX_DISTANCE 300
#define IR A5

AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);



NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

Servo myservo;

String voice;

void setup()
{
  Serial.begin(115200);
  Serial.println("===motor driver test===");
  myservo.attach(10);
  myservo.write(90);
  pinMode(IR, INPUT);
}

void loop()
{
  forward();
  Serial.println("Motor Runs Forward");
  delay(1000);
  Stop();
  Serial.println("Motor Stopped");
  delay(500);
  backward();
  Serial.println("Motor Runs Backward");
  delay(1000);
  Stop();
  Serial.println("Motor Stopped");
  delay(500);
}

//=========================================

void forward()
{
  /*  int distance = sonar.ping_cm();
    if (distance < 10)
    {
      Stop();
      voice = "";
    }
    else
    {
  */
  motor1.setSpeed(255);
  motor1.run(FORWARD);
  motor2.setSpeed(255);
  motor2.run(FORWARD);
  motor3.setSpeed(255);
  motor3.run(FORWARD);
  motor4.setSpeed(255);
  motor4.run(FORWARD);
  //  }
}
//======================================
void backward()
{
  /* int IR_Sensor = digitalRead(IR);
    if (IR_Sensor == 0)
    {
     Stop();
     voice = "";
    }
    else
    {
  */
  motor1.setSpeed(255);
  motor1.run(BACKWARD);
  motor2.setSpeed(255);
  motor2.run(BACKWARD);
  motor3.setSpeed(255);
  motor3.run(BACKWARD);
  motor4.setSpeed(255);
  motor4.run(BACKWARD);
  //  }
}
//===============================
void left()
{
  myservo.write(180);
  delay(500);
  myservo.write(90);
  delay(500);
  motor1.run(BACKWARD);
  motor1.setSpeed(255);
  motor2.run(BACKWARD);
  motor2.setSpeed(255);
  motor3.run(FORWARD);
  motor3.setSpeed(255);
  motor4.run(FORWARD);
  motor4.setSpeed(255);
  delay(700);
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}
//===================================
void right()
{
  myservo.write(0);
  delay(500);
  myservo.write(90);
  delay(500);
  motor1.run(FORWARD);
  motor1.setSpeed(255);
  motor2.run(FORWARD);
  motor2.setSpeed(255);
  motor3.run(BACKWARD);
  motor3.setSpeed(255);
  motor4.run(BACKWARD);
  motor4.setSpeed(255);
  delay(700);
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}
//==================================
void Stop()
{
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}

Tom... :grinning: :+1: :coffee: :australia:

Thanks
Motors are working
But can you guide how to integrate bluetooth module and operate through voice commands

Hi,
Don't you need an app on the device that the Bluetooth will be communicating with?
I think there is a lot more to this project than you have been presented with.
Copy and pasting is not the best way tp learn Arduino for a beginner, especially something this ambitious.
I am not a Bluetooth expert, you may get another member to help you, but your Subject for this thread;
"My robot is not working" will not attract many BT experts.
You should be able to edit the subject to something like;
"Robot not working with Bluetooth"
or something like that.

Tom.. :grinning: :+1: :coffee: :australia:

1 Like

Thank you very much