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.