I can't seem to find the error I made; can anyone help me?
The error message: 'Servo_Pointer' was not declared in this scope
I get the error at the end of the code with the line: Servo_Pointer.write(servo_pos)
The code:
const int photo_left = A0; // select the input pins for the photoresistor
const int photo_right = A1;
const int ledPin_1 = 2; // These leds will be used for the testing of the pointing of the servo motor
const int ledPin_2 = 4;
const int ledPin_3 = 7;
const int ledPin_4 = 8;
const int ledPin_5 = 12;
const int photoValue = 0; // variable to store the value coming from the photoresistor
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin_1, OUTPUT);
pinMode(ledPin_2, OUTPUT);
pinMode(ledPin_3, OUTPUT);
pinMode(ledPin_4, OUTPUT);
pinMode(ledPin_5, OUTPUT);
Serial.begin(9600);
#include <Servo.h>
Servo Servo_Pointer;
Servo_Pointer.attach(9);
int servo_pos = 0;
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading ) {
readings[thisReading] = 0;
}
}
void loop() {
Servo_Pointer.write(servo_pos); // tell servomotor to go to position in variable 'servo_pos'
delay(15); // waits 15ms for the servo to reach the position
// turn the ledPin on
delay(15);
}
-
1Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under the CC BY-SA 4.0 license for SE to distribute that content. By SE policy, any vandalism will be reverted. If you want to know more about deleting a post, consider taking a look at: How does deleting work?Glorfindel– Glorfindel11/10/2020 21:25:12Commented Nov 10, 2020 at 21:25
2 Answers 2
You should make Servo Servo_Pointer;
global, i.e. place it outside the setup
function, e.g. after the line int average = 0;
.
In your code, it is a local variable within the setup
function. When this function ends, the local variables within this function (such as Servo_Pointer
) do not exist anymore.
By making the variable global, you can use it anywhere. However, it's not a good coding practice to use global variables when not needed.
A benefit of making the variable global is that you can see the memory consumption of the instance of Servo_Pointer
when compiling.
Also see the comment of timemage below: Don't put #include
statements in the middle of your code, put them in the beginning of your code.
-
1Might be worth adding that it's at least unconventional to have an #include directive in the middle of your function.timemage– timemage11/10/2020 02:22:58Commented Nov 10, 2020 at 2:22
-
@timemage Good catch, I did a search on Servo_Pointer missing that line.Michel Keijzers– Michel Keijzers11/10/2020 08:27:27Commented Nov 10, 2020 at 8:27
You need to place the decleration outside of your setup()
function. In other words, you need to move the following 3 lines and place then above your setup()
function:
#include <Servo.h>
Servo Servo_Pointer;
int servo_pos = 0;
Your code should look like this:
#include <Servo.h>
const int photo_left = A0; // select the input pins for the photoresistor
const int photo_right = A1;
const int ledPin_1 = 2; // These leds will be used for the testing of the pointing of the servo motor
const int ledPin_2 = 4;
const int ledPin_3 = 7;
const int ledPin_4 = 8;
const int ledPin_5 = 12;
const int photoValue = 0; // variable to store the value coming from the photoresistor
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int servo_pos = 0;
Servo Servo_Pointer;
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin_1, OUTPUT);
pinMode(ledPin_2, OUTPUT);
pinMode(ledPin_3, OUTPUT);
pinMode(ledPin_4, OUTPUT);
pinMode(ledPin_5, OUTPUT);
Serial.begin(9600);
Servo_Pointer.attach(9);
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading ) {
readings[thisReading] = 0;
}
}
void loop() {
Servo_Pointer.write(servo_pos); // tell servomotor to go to position in variable 'servo_pos'
delay(15); // waits 15ms for the servo to reach the position
// turn the ledPin on
delay(15);
}
Explore related questions
See similar questions with these tags.