April 10,2022
Hi,
I am just starting to learn Arduino coding and I visited many websites on the subject.
I want to control a robot drone , not with radio, but laser KY-008 using a joystick connected to Arduino R3 UNO with rx and tx pins.
Here is my code.I ran my code on the Arduino simulator and the error was 'val' was not declared in this scope. val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
I tried to cope the error and did a search on the internet
I know I made mistakes,but if some one could give any suggestion,on my error, I would greatly appreciated
it.
Below is my code;
#include <Servo.h>
#define DETECT 2 //pin 2 input receiver
#define ACTION 8 //pin 8 action output
#define POTPIN 0 // analog pin used to connect the potentiometer
void setup()
{
Serial.begin(9600);//Begin Serial communication at a baudrate of 9600:
pinMode(DETECT,INPUT); //define detect input pin 2
pinMode(ACTION,OUTPUT); //define action output pin 8
}
void loop(){
int detected = digitalRead(DETECT);//read pin 2
if(detected==HIGH)//when laser bean detected value is high
{
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
}
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
delay(15); // waits for the servo to get there
}
delay(200);
}
Thank You,
DavidBrown70
There are a number of problems and untidy parts in your sketch. I'll try to describe them so that you can fix them.
I've marked problems with *** and a number and then *** again. The following line(s) try to explain the problem.
#include <Servo.h> #define DETECT 2 //pin 2 input receiver #define ACTION 8 //pin 8 action output #define POTPIN 0 // analog pin used to connect the potentiometer *** 1 *** *** use A0 instead of just 0 because tis is an Analogue pin void setup() { Serial.begin(9600);//Begin Serial communication at a baudrate of 9600: pinMode(DETECT, INPUT); //define detect input pin 2 pinMode(ACTION, OUTPUT); //define action output pin 8 } void loop() { int detected = digitalRead(DETECT);//read pin 2 if (detected == HIGH) //when laser bean detected value is high { Servo myservo; // create servo object to control a servo int potpin = 0; // analog pin used to connect the potentiometer *** 2 *** *** 2 *** you don't need this because you have alreadt #defined a value for the pot pin number above int val; // variable to read the value from the analog pin } *** 3 *** *** 3 *** This } terminates the code group (called scope) in which the value *** "val" was declared as an int. SO val is no longer defined *** It also marks the end of the "if (detected == HIGH)" condition *** So from now on detected may or may not be HIGH { *** 4 *** *** This { starts a new scope in which the value val is not declared val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180) delay(15); // waits for the servo to get there } delay(200); }
I'm thinking that this is what you need to start with ...
#include <Servo.h> #define DETECT 2 //pin 2 input receiver #define ACTION 8 //pin 8 action output #define POTPIN A0 // analog pin used to connect the potentiometer Servo myservo; // create servo object to control a servo void setup() { Serial.begin(9600);//Begin Serial communication at a baudrate of 9600: pinMode(DETECT, INPUT); //define detect input pin 2 pinMode(ACTION, OUTPUT); //define action output pin 8 } void loop() { int detected = digitalRead(DETECT);//read pin 2 if (detected == HIGH) //when laser beam detected value is high { int val; // variable to read the value from the analog pin val = analogRead(POTPIN); // reads the value of the potentiometer (value between 0 and 1023) val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180) delay(15); // waits for the servo to get there } delay(200); }
Note that I've moved the Servo creation out of the setup and loop code. It should be declared here so as to be available everywhere.
Anything seems possible when you don't know what you're talking about.