First time Arduino-er. Please help with any coding/error help!

Ok so my nephew and I decided to embark on building an obstacle avoiding robot. This thing has literally given me ulcers. I have no idea what to do now. we have built it according to the specs, we have checked and re-checked all the wires and connections. I think the problem is with the code.

int trigPin = 9;
int echoPin = 10;
int revright = 4; //REVerse motion of Right motor
int fwdleft = 7;
int revleft= 6;
int fwdright= 5; //ForWarD motion of Right motor
int c = 0;

void setup() {
//Serial.begin(9600);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(4, OUTPUT);
pinMode(7, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// put your setup code here, to run once:

}

void loop() {
long duration, distance;
digitalWrite(trigPin,HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration=pulseIn(echoPin, HIGH);
distance =(duration/2)/29.1;
//Serial.print(distance);
//Serial.println(“CM”);
delay(10);

if((distance>25))
{
digitalWrite(5,HIGH); // If you dont get proper movements of your robot,
digitalWrite(4,LOW); // then alter the pin numbers
digitalWrite(6,LOW); //
digitalWrite(7,HIGH); //
}

else if(distance<25)
{
digitalWrite(5,HIGH);
digitalWrite(4,LOW);
digitalWrite(6,HIGH); //HIGH
digitalWrite(7,LOW);

}

}

There is the code… The error says “error compiling for board Arduino/genuino uno”.
again this is our first time. so we really have no idea what we are doing. we even ditched the servo motor to try to simplify. oh and yes its an Arduino uno. we have a motor driver and the supersonic sensor.

Thank you guys for even reading this.

int trigPin = 9;
int echoPin = 10;
int revright = 4;      //REVerse motion of Right motor
int fwdleft = 7;
int revleft= 6;
int fwdright= 5;       //ForWarD motion of Right motor
int c = 0;

void setup() {
  //Serial.begin(9600); 
   pinMode(5, OUTPUT);
   pinMode(6, OUTPUT);
   pinMode(4, OUTPUT);
   pinMode(7, OUTPUT);
   pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  // put your setup code here, to run once:

}

void loop() {
  long duration, distance;
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration=pulseIn(echoPin, HIGH);
  distance =(duration/2)/29.1;
  //Serial.print(distance);
  //Serial.println("CM");
  delay(10);
 

  if((distance>25))
 {
  digitalWrite(5,HIGH);                               //       If you dont get proper movements of your robot,
   digitalWrite(4,LOW);                               //        then alter the pin numbers
   digitalWrite(6,LOW);                               //
   digitalWrite(7,HIGH);                              //
 }
 
  else if(distance<25)
 {
   digitalWrite(5,HIGH);
   digitalWrite(4,LOW);
   digitalWrite(6,HIGH);                                  //HIGH
   digitalWrite(7,LOW);

 }
 
}

I copied the code into the IDE and built - no error while compiling; IDE 1.8.5
Any additional info?

hmm... oh that's good news I think...well then I have no idea what we could be doing wrong. no unfortunately that's all the error said. is there somewhere else that would show me more info?

Like JMeller, I loaded you code into my IDE, selected the Uno and it compiled without error.

If you still get the orange bar of death, click the “Copy error messages” button at the bottom right and paste the here (use code tags…)

I looked at your code and note a few things:

  1. Your ultrasound trigger code is unorthodox and probably not correct:
    digitalWrite(trigPin,HIGH);
    delayMicroseconds(1000);
    digitalWrite(trigPin, LOW);
.
.
.

The “generally accepted” way to drive the trigger pin is:

    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
.
.
.
  1. The value of duration will be 0 (zero) when no echo is seen; that could be if the way is clear or if something is very, very close to the sensor. You should account for that in your check:
.
.
.
    else if(distance<25)
    {
        digitalWrite(5,HIGH);
.
.
.
  1. pulseIn returns an unsigned long; technically, you should declare duration as an unsigned long.

  2. It’s a good idea to use descriptive pin names. Trying to read this:

        digitalWrite(5,HIGH);                               //       If you dont get proper movements of your robot,
        digitalWrite(4,LOW);                               //        then alter the pin numbers
        digitalWrite(6,LOW);                               //
        digitalWrite(7,HIGH);

with just pin numbers (instead of the pin names you declared) will get frustrating quickly. Use the names you declared:

        digitalWrite(fwdright,HIGH);                               //       If you dont get proper movements of your robot,
        digitalWrite(revright,LOW);                               //        then alter the pin numbers
        digitalWrite(revleft,LOW);                               //
        digitalWrite(fwdleft,HIGH);                              //

Blackfin:
Like JMeller, I loaded you code into my IDE, selected the Uno and it compiled without error.

If you still get the orange bar of death, click the “Copy error messages” button at the bottom right and paste the here (use code tags…)

I looked at your code and note a few things:

  1. Your ultrasound trigger code is unorthodox and probably not correct:
    digitalWrite(trigPin,HIGH);

delayMicroseconds(1000);
   digitalWrite(trigPin, LOW);
.
.
.




The "generally accepted" way to drive the trigger pin is:



digitalWrite(trigPin, LOW);
   delayMicroseconds(2);
   digitalWrite(trigPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(trigPin, LOW);
.
.
.




2) The value of duration will be 0 (zero) when no echo is seen; that could be if the way is clear or if something is very, very close to the sensor. You should account for that in your check:



.
.
.
   else if(distance<25)
   {
       digitalWrite(5,HIGH);
.
.
.




3) pulseIn returns an unsigned long; technically, you should declare duration as an unsigned long.

4) It's a good idea to use descriptive pin names. Trying to read this:



digitalWrite(5,HIGH);                               //       If you dont get proper movements of your robot,
       digitalWrite(4,LOW);                               //        then alter the pin numbers
       digitalWrite(6,LOW);                               //
       digitalWrite(7,HIGH);




with just pin numbers (instead of the pin names you declared) will get frustrating quickly. Use the names you declared:



digitalWrite(fwdright,HIGH);                               //       If you dont get proper movements of your robot,
       digitalWrite(revright,LOW);                               //        then alter the pin numbers
       digitalWrite(revleft,LOW);                               //
       digitalWrite(fwdleft,HIGH);                              //

should I also use the descriptive names for this section of the code?

"else if(distance<25)
{
digitalWrite(5,HIGH);
digitalWrite(4,LOW);
digitalWrite(6,HIGH); //HIGH
digitalWrite(7,LOW);

}"

I tried it and I got an error message…

Arduino: 1.8.8 (Windows Store 1.8.19.0) (Windows 10), Board: “Arduino/Genuino Uno”

C:\Users\monke\Documents\Arduino\sketch_mar04b\sketch_mar04b.ino: In function ‘void loop()’:

sketch_mar04b:49:17: error: ‘Fwdleft’ was not declared in this scope

digitalWrite(Fwdleft,LOW);

^

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

what does that mean and how do I fix it?

Not easy to tell without seeing your latest complete code but the language is case sensitive so fwdleft, fwdLeft, Fwdleft and FwdLeft are all completely different names. Check that your spelling is consistent.

Steve

Jejjybean:
should I also use the descriptive names for this section of the code?

Yes. Use the pin name variables everywhere you reference the pin number in your code. In addition to making your code easier to understand, it also makes it simple to change the pin number by editing only a single line of code (the pin name variable definition), rather than having to hunt all through your code to change every use of the pin number.

Slightly OT

Blackfin:
The "generally accepted" way to drive the trigger pin is:

    digitalWrite(trigPin, LOW);

delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

Does anyone know where this faintly silly "generally accepted" idiom comes from?
I see it repeated all over the place; LOW-HIGH-LOW
Why?
If the pin is already LOW, there's no need to write it LOW again.
Is it some extreme form of defensive programming?

AWOL:
Slightly OTDoes anyone know where this faintly silly "generally accepted" idiom comes from?
I see it repeated all over the place; LOW-HIGH-LOW
Why?
If the pin is already LOW, there's no need to write it LOW again.
Is it some extreme form of defensive programming?

Meh, seems harmless enough. Not unlike declaring INPUT pins during setup (they're input by default); I still do it anyway.

At the very least I'd ensure trig is set low during setup to establish that state (even if that's the default...see above) then rely on every ultrasonic sequence to leave it low thereafter.

here is the changed code. there are no errors which is great. it uploads to the Arduino but nothing. no movement, nothing. I have no idea what to do.

int trigPin = 9;
int echoPin = 10;
int revright = 4; //Reverse motion of Right motor
int fwdleft = 7;
int revleft= 6;
int fwdright= 5; //Forward motion of Right motor
int c = 0;

void setup() {
//Serial.begin(9600);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(4, OUTPUT);
pinMode(7, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// put your setup code here, to run once:

}

void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration=pulseIn(echoPin, HIGH);
distance =(duration/2)/29.1;
//Serial.print(distance);
//Serial.println(“CM”);
delay(10);

if((distance>25))
{
digitalWrite(fwdright,HIGH); // If you dont get proper movements of your robot,
digitalWrite(revright,LOW); // then alter the pin numbers
digitalWrite(revleft,LOW); //
digitalWrite(fwdleft,HIGH); //
}

else if(distance<25)
{
digitalWrite(fwdright,HIGH);
digitalWrite(revright,LOW);
digitalWrite(revleft,HIGH); //HIGH
digitalWrite(fwdleft,LOW);

}

}

How are your motors powered? Can you use that power supply to verify that it can run those motors? How is the Arduino turning the motors on?

You say you built the robot according to the specs but you haven't shared these specs with us. It's useful to know exactly what components (sensor, motors and motor driver) you're using as well as what is powering everything. As wildbill suggests many problems are connected with the power...e.g. motors will usually not run from a little 9V rectangular battery.

And it may be worth uncommenting the Serial.begin() and Serial.print() lines (just remove the //) so you can check that the sensor is doing what you expect.

Steve

ok so we have an Arduino uno, an hc - sr04 supersonic sensor, a breadboard now, a servo motor, and 2 wheels and we are using a 12v 1.3ah battery. also here is our updated code… we no longer get an error which is good. however the robot only looks right never left, and the wheels don’t spin at all. so not sure what the problem is now.

#include <Servo.h> // Include Servo Library
#include <NewPing.h> // Include Newping Library
// L298N Control Pins
const int LeftMotorForward = 7;
const int LeftMotorBackward = 5;
const int RightMotorForward = 4;
const int RightMotorBackward = 3;
#define TRIGGER_PIN 13 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 12 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 250 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 250cm.
Servo servo_motor; // Servo’s name
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
boolean goesForward = false;
int distance = 150;
void setup()
{
// Set L298N Control Pins as Output
pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);
servo_motor.attach(11); // Attaches the servo on pin 11 to servo object.
servo_motor.write(90); // Set at 90 degrees.
delay(500); // Wait for 500ms.
distance = readPing(); // Get Ping Distance.
delay(200); // Wait for 200ms.
}
void loop()
{
int distanceRight = 0;
int distanceLeft = 0;
delay(50);
if (distance <= 30)
{
moveStop();
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() // Look Right Function for Servo Motor
{
servo_motor.write(145);
delay(500);
int distance = readPing();
delay(500);
servo_motor.write(90);
return distance;
}
int lookLeft() // Look Left Function for Servo Motor
{
servo_motor.write(35);
delay(500);
int distance = readPing();
delay(500);
servo_motor.write(90);
return distance;
}
int readPing() // Read Ping Function for Ultrasonic Sensor.
{
delay(75); // Wait 100ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
int cm = sonar.ping_cm(); //Send ping, get ping distance in centimeters (cm).
if (cm==0)
{
cm=250;
}
return cm;
}
void moveStop() // Move Stop Function for Motor Driver.
{
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorBackward, LOW);
}
void moveForward() // Move Forward Function for Motor Driver.
{
digitalWrite(RightMotorForward, HIGH);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
}
void moveBackward() // Move Backward Function for Motor Driver.
{
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorBackward, HIGH);
}
void turnRight() // Turn Right Function for Motor Driver.
{
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
}
void turnLeft() // Turn Left Function for Motor Driver.
{
digitalWrite(RightMotorForward, HIGH);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorBackward, HIGH);
}

If you run this loop, does the servo move left and right?

void loop()
{
#if 1
    servo_motor.write(145);
    delay(1000);
    servo_motor.write(35);
    delay(1000);
    
#else
    int distanceRight = 0;
    int distanceLeft = 0;
    
    delay(50);
    if (distance <= 30)
    {
        moveStop();
        moveBackward();
        delay(900);
        moveStop();
        delay(300);
        distanceRight = lookRight();
        delay(200);
        distanceLeft = lookLeft();
        delay(200);
        if (distanceRight >= distanceLeft)
        {
            turnRight();
            delay(400);
            moveStop();
            
        }//if
        else
        {
            turnLeft();
            delay(400);
            moveStop();
            
        }//else
    }
    else
    {
        moveForward();
        
    }//else
    
    distance = readPing();
#endif

}//loop

do I just try that part by itself?

so no because I tried and an error message occurred. im not sure how to just do that part by itself.

Just copy the content of that post over your existing loop().

I tried that piece in place of the existing loop and still nothing. at all. we thought we had success. I bought a new Arduino uno (it has an infinity symbol on it) thinking maybe it was our components. once we got everything re hooked up and the code uploaded it took a second but the head started to turn and the wheels started to spin. we got excited. once we glued everything down and tried again… nothing. I don’t understand what’s happening. i can upload another video of it doing nothing. if you need to see that. its so irritating. i have no idea what is wrong.