Trying to chase down the Errors in my newly made code

I am new to Arduino coding and I am working on a project for my Engineering class. I am using a two motor, 2wd Arduino kit car. This kit came with a HC-SR04 Sensor and a small servo. So far I have successfully coded the car to move forward, detect an object in its path using the HC-SR04 sensor, stop and turn the car to avoid the obstacle. I am now trying to code the Servo to swing the servo arm 180 degrees, when the car encounters an obstacle. Yet, I am getting a couple of repeating Errors, one being " 'up' was not declared in this scope ". This makes me think I am overlooking a simple problem in my code that I cannot quite find. If anyone could help with finding the Error I have in the Coding it would be greatly appreciated.
Unfortunately, I am a new user so I am unable to post an attachment of my Coding. I will try to copy-and-paste the code, I understand it will not be in the correct format but any help would be appreciated.


// Servo Library 
#include <Servo.h>

int servoPin = 11;     // Servo Pin
int trigPin = 13;      // trig pin of HC-SR04
int echoPin = 12;     // Echo pin of HC-SR04
int revleft8 = 8;       //REVerse motion of Left motor   
int fwdleft9 = 9;       //ForWarD motion of Left motor   
int revright6 = 6;      //REVerse motion of Right motor   
int fwdright7 = 7;      //ForWarD motion of Right motor   
int speedright5 = 5;    //Right motor Speed
int speedleft10 = 10;   //Left motor Speed

long duration, distance;

void setup() {
  
  delay(random(500,2000));   // delay 
  Serial.begin(9600);
  pinMode(revleft8, OUTPUT);      // set Motor pins as output
  pinMode(fwdleft9, OUTPUT);
  pinMode(revright6, OUTPUT);
  pinMode(fwdright7, OUTPUT);
  pinMode(speedright5, OUTPUT);
  pinMode(speedleft10, OUTPUT);

  pinMode(trigPin, OUTPUT);         // set trig pin as output
  pinMode(echoPin, INPUT);          //set echo pin as input 
  pinMode(servoPin, OUTPUT);        //Servo pin output
}

void loop() {

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);   
  digitalWrite(trigPin, HIGH);     // send waves for 10ms
  delayMicroseconds(10);
  duration = pulseIn(echoPin, HIGH); // receive reflected waves
  distance = (duration/2)/29.1;   // convert to distance
  delay(10);
    
  if (distance > 46)            
  {
    digitalWrite(speedright5, 255);                 //move forward
    digitalWrite(fwdright7, HIGH);     
    digitalWrite(revright6, LOW);
    digitalWrite(fwdleft9, HIGH);                                
    digitalWrite(revleft8, LOW);
    digitalWrite(speedleft10, 255);                                                       
  }
 
  if (distance < 45)
  {
    digitalWrite(fwdright7, LOW);  //Stop                
    digitalWrite(revright6, LOW);
    digitalWrite(fwdleft9, LOW);                                
    digitalWrite(revleft8, LOW);
    delay(500);
    digitalWrite(speedright5, 255);    //movebackward
    digitalWrite(fwdright7, LOW);             
    digitalWrite(revright6, HIGH);
    digitalWrite(fwdleft9, LOW);                                
    digitalWrite(revleft8, HIGH);
    delay(500);
    digitalWrite(fwdright7, LOW);  //Stop                
    digitalWrite(revright6, LOW);
    digitalWrite(fwdleft9, LOW);                                
    digitalWrite(revleft8, LOW);  
    delay(100); 
    digitalWrite(speedright5, 255); 
    digitalWrite(fwdright7, HIGH);       
    digitalWrite(revright6, LOW);   
    digitalWrite(revleft8, LOW);                                 
    digitalWrite(fwdleft9, LOW); 
    digitalWrite(speedleft10, 255); 
    delay(500);
  }


void loop() 
;if (distance < 45)

  up();
  delay(2000);
  down();
  delay(2000);
}

void up()
  
  for(int i=0;i<15;i++)
  {
    digitalWrite(11,1);
    delayMicroseconds(1500);
    digitalWrite(11,0);
    delay(20);
  }
}

void down()
  for(int i=0;i<15;i++)
  {
    digitalWrite(11,1);
    delayMicroseconds(1000);
    digitalWrite(11,0);
    delay(20);
  }
}

Above is My Code for the Arduino Kit Car, Below is the Error Message I am Receiving.


Arduino: 1.8.16 (Windows 10), Board: "Arduino Uno"




C:\Users\Mikes-Top\Documents\MotorControlServoSensor\MotorControlServoSensor.ino: In function 'void loop()':

MotorControlServoSensor:83:3: error: 'up' was not declared in this scope

   up();

   ^~

C:\Users\Mikes-Top\Documents\MotorControlServoSensor\MotorControlServoSensor.ino:83:3: note: suggested alternative: 'u8'

   up();

   ^~

   u8

MotorControlServoSensor:85:3: error: 'down' was not declared in this scope

   down();

   ^~~~

C:\Users\Mikes-Top\Documents\MotorControlServoSensor\MotorControlServoSensor.ino:85:3: note: suggested alternative: 'pow'

   down();

   ^~~~

   pow

C:\Users\Mikes-Top\Documents\MotorControlServoSensor\MotorControlServoSensor.ino: At global scope:

MotorControlServoSensor:91:3: error: expected initializer before 'for'

   for(int i=0;i<15;i++)

   ^~~

MotorControlServoSensor:91:15: error: 'i' does not name a type

   for(int i=0;i<15;i++)

               ^

MotorControlServoSensor:91:20: error: 'i' does not name a type

   for(int i=0;i<15;i++)

                    ^

MotorControlServoSensor:98:1: error: expected declaration before '}' token

 }

 ^

exit status 1

'up' was not declared in this scope



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
void up()

What do you think that this line of code does ?

void up()

for (int i = 0; i < 15; i++)
{
  digitalWrite(11, 1);
  delayMicroseconds(1500);
  digitalWrite(11, 0);
  delay(20);
}
}

Something is very wrong here

Same here. Missing a brace or two...

void down()
  for(int i=0;i<15;i++)
  {
    digital

You have basic syntax errors.

Functions look like this example:

void loop() {
    // stuff that gets executed when loop is called
}

  • A return type, void in this case

  • The function name

  • An argument list, () emtpy in this case (no arguments)

  • An opening brace {

  • // stuff the function should do, a list of statements

  • And finalment a closing brace }

Take a look over your code, until your functions all follow the proper pattern, there is no point to talking about other errors there may be.

I see two things that look,like loop() attempts; there should only be one definition for each function. Functions have to have different names in one source file.

Look over some examples and see how loop() and setup() are defined - these are called, setup once at the start, and loop continuously, very frequently, to perform the continuous work of the program.

HTH

a7

I'm not to sure what I am doing. I was giving a simple code that only tells the Servo to move the servo arm back and forth to test the functionality of the Servo. I used that code, and tried to alter-it to fit into the coding for the car to move and avoid obstacles. I thought this line of code was needed but, I may have made a critical mistake.

Look carefully the code for the servo that you have. In particular take note of the advice in reply #4 above about the structure of a function

digitalWrite(speedleft10, 255);

This isn’t doing what you might be thinking…

Try this:

// Servo Library
#include <Servo.h>

Servo servo;

const byte ServoPin = 11;     // Servo Pin
const byte TriggerPin = 13;      // trig pin of HC-SR04
const byte EchoPin = 12;     // Echo pin of HC-SR04
const byte LeftReversePin = 8;       // REVerse motion of Left motor
const byte LeftForwardPin = 9;       // ForWarD motion of Left motor
const byte RightReversePin = 6;      // REVerse motion of Right motor
const byte RightForwardPin = 7;      // ForWarD motion of Right motor
const byte RightPWMPin = 5;    // Right motor Speed
const byte LeftPWMPin = 10;   // Left motor Speed

unsigned long int duration;
unsigned int distance;

void setup()
{

  servo.attach(ServoPin);

  delay(random(500, 2000));  // delay
  Serial.begin(9600);
  pinMode(LeftReversePin, OUTPUT);      // set Motor pins as output
  pinMode(LeftForwardPin, OUTPUT);
  pinMode(RightReversePin, OUTPUT);
  pinMode(RightForwardPin, OUTPUT);
  pinMode(RightPWMPin, OUTPUT);
  pinMode(LeftPWMPin, OUTPUT);

  pinMode(TriggerPin, OUTPUT);         // set trig pin as output
  pinMode(EchoPin, INPUT);          //set echo pin as input
}

void loop()
{

  digitalWrite(TriggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(TriggerPin, HIGH);     // send waves for 10ms
  delayMicroseconds(10);
  duration = pulseIn(EchoPin, HIGH, 30000UL); // receive reflected waves
  distance = (duration / 2) / 29.1; // convert to distance
  delay(10);

  if (distance == 0 || distance > 46)
  {
    analogWrite(RightPWMPin, 255);                 //move forward
    digitalWrite(RightForwardPin, HIGH);
    digitalWrite(RightReversePin, LOW);
    digitalWrite(LeftForwardPin, HIGH);
    digitalWrite(LeftReversePin, LOW);
    analogWrite(LeftPWMPin, 255);

    up();
    delay(2000);
    down();
    delay(2000);
  }
  else
  {
    digitalWrite(RightForwardPin, LOW);  //Stop
    digitalWrite(RightReversePin, LOW);
    digitalWrite(LeftForwardPin, LOW);
    digitalWrite(LeftReversePin, LOW);
    delay(500);
    analogWrite(RightPWMPin, 255);    //movebackward
    digitalWrite(RightForwardPin, LOW);
    digitalWrite(RightReversePin, HIGH);
    digitalWrite(LeftForwardPin, LOW);
    digitalWrite(LeftReversePin, HIGH);
    delay(500);
    digitalWrite(RightForwardPin, LOW);  //Stop
    digitalWrite(RightReversePin, LOW);
    digitalWrite(LeftForwardPin, LOW);
    digitalWrite(LeftReversePin, LOW);
    delay(100);
    analogWrite(RightPWMPin, 255);
    digitalWrite(RightForwardPin, HIGH);
    digitalWrite(RightReversePin, LOW);
    digitalWrite(LeftReversePin, LOW);
    digitalWrite(LeftForwardPin, LOW);
    analogWrite(LeftPWMPin, 255);
    delay(500);
  }
}

void up()
{
  servo.writeMicroseconds(1500);
}

void down()
{
  servo.writeMicroseconds(1000);
}

Thank you all for the help, I am still trying to understand the Coding language. I have now gotten all parts of the car to work together. I think I have fixed any Errors that were present and the car works autonomously now. But I still have a few bugs I'm trying to figure out, if anyone could take a look over my new Code that would be appreciated.

#include <Servo.h>     // Servo Library 
Servo servo1;

int servoPin = 11;     // Servo Pin
int trigPin = 13;      // trig pin of HC-SR04
int echoPin = 12;     // Echo pin of HC-SR04
int revleft8 = 8;       //REVerse motion of Left motor   
int fwdleft9 = 9;       //ForWarD motion of Left motor   
int revright6 = 6;      //REVerse motion of Right motor   
int fwdright7 = 7;      //ForWarD motion of Right motor   
int speedright5 = 5;    //Right motor Speed
int speedleft10 = 10;   //Left motor Speed

long duration, distance;

void setup() {
  
  delay(random(500,2000));   // delay 
  Serial.begin(9600);
  pinMode(revleft8, OUTPUT);      // set Motor pins as output
  pinMode(fwdleft9, OUTPUT);
  pinMode(revright6, OUTPUT);
  pinMode(fwdright7, OUTPUT);
  pinMode(speedright5, OUTPUT);
  pinMode(speedleft10, OUTPUT);

  pinMode(trigPin, OUTPUT);         // set trig pin as output
  pinMode(echoPin, INPUT);          //set echo pin as input 

servo1.attach(11);                    //Servo pin

}

void loop() {

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);   
  digitalWrite(trigPin, HIGH);     // send waves for 10ms
  delayMicroseconds(10);
  duration = pulseIn(echoPin, HIGH); // receive reflected waves
  distance = (duration/2)/29.1;   // convert to distance
  delay(10);
   
  if (distance > 46)            
  {
    digitalWrite(speedright5, 255);                 //move forward
    digitalWrite(fwdright7, HIGH);     
    digitalWrite(revright6, LOW);
    digitalWrite(fwdleft9, HIGH);                                
    digitalWrite(revleft8, LOW);
    digitalWrite(speedleft10, 255);  
    servo1.write(90);                                                     
  }
 
  if (distance < 45)
  {
    servo1.write(0);             //Swing Servo arm
    servo1.write(90);
    digitalWrite(fwdright7, LOW);     //Stop                
    digitalWrite(revright6, LOW);
    digitalWrite(fwdleft9, LOW);                                
    digitalWrite(revleft8, LOW);
    delay(500);
    digitalWrite(speedright5, 255);    //movebackward
    digitalWrite(fwdright7, LOW);             
    digitalWrite(revright6, HIGH);
    digitalWrite(fwdleft9, LOW);                                
    digitalWrite(revleft8, HIGH);
    delay(500);
    servo1.write(90);
    servo1.write(0);
    digitalWrite(fwdright7, LOW);  //Stop                
    digitalWrite(revright6, LOW);
    digitalWrite(fwdleft9, LOW);                                
    digitalWrite(revleft8, LOW);  
    delay(100); 
    digitalWrite(speedright5, 255); 
    digitalWrite(fwdright7, HIGH);       
    digitalWrite(revright6, LOW);   
    digitalWrite(revleft8, LOW);                                 
    digitalWrite(fwdleft9, LOW); 
    digitalWrite(speedleft10, 255); 
    delay(500);
  }

}

You could tell yscwhat it is doing that it shouldn't or what it isn't doing that it should…

"a few bugs" is a bit vague. :expressionless:

a7

Hi mside22,

you seem to have jumped into coding with pretty small knowledge about general things how C++-coding for Arduino works

Take a look into this tutorial to find out if it fits to your learning style:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

The car works when it the power is first turned on. After the car encounters an obstacle, the car backs up and swings the servo arm, which is what I need it to do. Yet the car then begins to spin in place rather than truing and proceeding in a different direction. I'm not exactly sure why this is happening, but I think I may have done something wrong when putting the additional coding in for the servo.

Thank you, I will definitely read through this. I am new to C++ coding, and coding in general.

Have you tried printing the values informing you logic to see if they are plausible? distance for example.

You could also test the if/else just with a switch to see if both do what you want, although you may have done that, do you have a sketch that worked before the sensor update? Post it if.

a7