Code was not declared in scope

Hey everyone, im trying to help someone with this arduino project for a ultrasonic sensor robot device. Ive verified my connections several times but my servo still wont startup. I was wondering if you lovely people could help me out and determine if its the code or my connections. The code is posted below. Thank you:

#include <Servo.h> #define echoPin 2 #define trigPin 3 #define LED 7 #define Buzz 6
Servo myservo;
int Position = 0; long Durat; int Dist; int currentPosition = 0;
int firstSweep[181]; int secondSweep[181];
void setup() {
  myservo.attach(9);
  pinMode(Buzz, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
  firstScan();
}
void loop() {
  turnServo(true);
  turnServo(false);
  delay(80);
}
int checkWave() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  Durat = pulseIn(echoPin, HIGH);
  Dist = (Durat / 2) / 29.1;
  return Dist;
}
void firstScan() {
  for (Position = 0; Position < 181; Position++)
  {
    myservo.write(Position); firstSweep[Position] = checkWave();
    delay(50);
  }
  for (Position = 180; Position >= 0; Position--)
  {
    myservo.write(Position); secondSweep[Position] = checkWave();
    delay(60);
  }
}
void turnServo(boolean clockwise)
{
  for (int i = 0; i < 181; i += 1)
  {
    int index = clockwise ? i : 180 - i;
    int currentDist = clockwise ? firstSweep[index] : secondSweep[index];
    myservo.write(index);
    currentPosition = checkWave(); if (currentPosition < currentDist + 3 && currentPosition >
                                       currentDist - 3)
    {
      digitalWrite(LED, LOW);
      digitalWrite(Buzz, LOW);
    }
    else
    {
      digitalWrite(LED, HIGH);
      digitalWrite(Buzz, HIGH);
    }
    delay(60);
  }`Preformatted text`

Welcome to the forum

Start by putting each #define on its own line then look carefully at where the turnServo() function ends. Did you miss a closing curly bracket in the code or maybe just when you copied it here ?

I had copied it as is, I did an auto format since I read that sometimes solves these issues or at least makes them easier to solve. But to no avail I have been stuck pondering for a while now

Copy the code back from your post into the IDE and Auto Format it

Is the last } on the left margin ?

I suggest you to try the example> servo>sweep code of the Arduino IDE first. And see the result.

I can confirm the "}" is on the left margin after copying and pasting. I also did the first part of your suggestion and made each "#define" on their own line. It verified but now I am having issues uploading.

The example I am using is "HelloWorld" as that is what the professor recommended us.

I beg to differ. How many curly braces are there after the delay(60) at the end of the code ?

So, you fixed the problem with the #defines but now cannot upload the code

Please post your revised code and the full error message (copied using the Copy Error Message button in the IDE, that you get when trying to upload the code

Which Arduino board are you using ?

#include <Servo.h> 
#define echoPin 2 
#define trigPin 3 
#define LED 7 
#define Buzz 6
Servo myservo;
int Position = 0; long Durat; int Dist; int currentPosition = 0;
int firstSweep[181]; int secondSweep[181];
void setup() {
  myservo.attach(9);
  pinMode(Buzz, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
  firstScan();
}
void loop() {
  turnServo(true);
  turnServo(false);
  delay(80);
}
int checkWave() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  Durat = pulseIn(echoPin, HIGH);
  Dist = (Durat / 2) / 29.1;
  return Dist;
}
void firstScan() {
  for (Position = 0; Position < 181; Position++)
  {
    myservo.write(Position); firstSweep[Position] = checkWave();
    delay(50);
  }
  for (Position = 180; Position >= 0; Position--)
  {
    myservo.write(Position); secondSweep[Position] = checkWave();
    delay(60);
  }
}
void turnServo(boolean clockwise)
{
  for (int i = 0; i < 181; i += 1)
  {
    int index = clockwise ? i : 180 - i;
    int currentDist = clockwise ? firstSweep[index] : secondSweep[index];
    myservo.write(index);
    currentPosition = checkWave(); if (currentPosition < currentDist + 3 && currentPosition >
                                       currentDist - 3)
    {
      digitalWrite(LED, LOW);
      digitalWrite(Buzz, LOW);
    }
    else
    {
      digitalWrite(LED, HIGH);
      digitalWrite(Buzz, HIGH);
    }
    delay(60);
  }

Error codes:


Arduino: 1.8.20 Hourly Build 2021/12/20 07:34 (Mac OS X), Board: "Arduino Uno"











/var/folders/tq/tc5bk51d2x5b8gck1_c14msr0000gn/T/arduino_modified_sketch_76876/HelloWorld.ino: In function 'void turnServo(boolean)':
HelloWorld:64:4: error: expected '}' at end of input
    }
    ^
exit status 1
expected '}' at end of input


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

The board i am using is the UNO R3 Controller Board.

#include <Servo.h>
#define echoPin 2
#define trigPin 3
#define LED 7
#define Buzz 6
Servo myservo;
int Position = 0; long Durat; int Dist; int currentPosition = 0;
int firstSweep[181]; int secondSweep[181];
void setup() {
  myservo.attach(9);
  pinMode(Buzz, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
  firstScan();
}
void loop() {
  turnServo(true);
  turnServo(false);
  delay(80);
}
int checkWave() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  Durat = pulseIn(echoPin, HIGH);
  Dist = (Durat / 2) / 29.1;
  return Dist;
}
void firstScan() {
  for (Position = 0; Position < 181; Position++)
  {
    myservo.write(Position); firstSweep[Position] = checkWave(); 
    delay(50);
  }
  for (Position = 180; Position >= 0; Position--)
  {
    myservo.write(Position); secondSweep[Position] = checkWave();
    delay(60);
  }
}
void turnServo(boolean clockwise)
{
  for (int i = 0; i < 181; i += 1)
  {
    int index = clockwise ? i : 180 - i;
    int currentDist = clockwise ? firstSweep[index] : secondSweep[index];
    myservo.write(index);
    currentPosition = checkWave(); if (currentPosition < currentDist + 3 && currentPosition >
                                       currentDist - 3)
    {
      digitalWrite(LED, LOW);
      digitalWrite(Buzz, LOW);
    }
    else
    {
      digitalWrite(LED, HIGH);
      digitalWrite(Buzz, HIGH);
    }
    delay(60);
  }
}

The code I posted up top wasnt passing the verify stage, but this one does.

Well, well

What did I say about a missing } ?

Check the new one I just sent, this one doesent pose that error.

Please don't post a sketch and an error message from a different one

So, what happens when you upload the code now and what should happen ? How is the servo powered and what type of servo is it ?

As previously suggested by @rpiloverbd, does teh Sweep example work with you servo wired as it is ?

This is the error message I get when I attempt to upload, and sorry. Im fairly new to this stuff, I didnt mean to upset you. I believed I was copying the right stuff.

:

Arduino: 1.8.20 Hourly Build 2021/12/20 07:34 (Mac OS X), Board: "Arduino Uno"











/var/folders/tq/tc5bk51d2x5b8gck1_c14msr0000gn/T/arduino_modified_sketch_41418/HelloWorld.ino:36:65: warning: null character(s) ignored
     myservo.write(Position); firstSweep[Position] = checkWave();  
                                                                 ^
Sketch uses 4360 bytes (13%) of program storage space. Maximum is 32256 bytes.
Global variables use 951 bytes (46%) of dynamic memory, leaving 1097 bytes for local variables. Maximum is 2048 bytes.
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x00
Problem uploading to board.  See https://support.arduino.cc/hc/en-us/sections/360003198300 for suggestions.


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

As far as I am aware, the servo is powered through the tethered connection to my laptop which connects to the UNO R3.

I just tried running my code in the sweep example and I am getting a very similar, if not the same error message. I will paste below:

Arduino: 1.8.20 Hourly Build 2021/12/20 07:34 (Mac OS X), Board: "Arduino Uno"

/var/folders/tq/tc5bk51d2x5b8gck1_c14msr0000gn/T/arduino_modified_sketch_785943/Sweep.ino:36:65: warning: null character(s) ignored
     myservo.write(Position); firstSweep[Position] = checkWave();  
                                                                 ^
Sketch uses 4360 bytes (13%) of program storage space. Maximum is 32256 bytes.
Global variables use 951 bytes (46%) of dynamic memory, leaving 1097 bytes for local variables. Maximum is 2048 bytes.
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x00
Problem uploading to board.  See https://support.arduino.cc/hc/en-us/sections/360003198300 for suggestions.


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

Have you ever been able to upload a sketch to the Uno ? Does the Serial port selected in the IDE match the port that the Uno is plugged into ? Does your PC recognise the Uno when it is plugged in and create a virtual serial port ?

Yeah, so turns out the uno was not selected. Now it is, code uploaded fine. But my servo's not moving. Does this now eliminate the fact that its a code issue? Also sorry about that mistake. Ive been up a while working on this, so im more prone to making errors.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.