Pages: [1]   Go Down
Author Topic: what does this mean 'val is not declared in this scope'?  (Read 1006 times)
0 Members and 1 Guest are viewing this topic.
india
Offline Offline
Newbie
*
Karma: 1
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#include <Servo.h>

// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>

#include <Servo.h>
 
Servo myservo1;  // create servo object to control a servo
Servo myservo2;
Servo myservo3;
Servo myservo4;
 
int potpin1 = 0;  // analog pin used to connect the potentiometer
int potpin2 = 1;
int potpin3 = 2;
int potpin4 = 3;

int val1;    // variable to read the value from the analog pin
int val2;
int val3;
int val4;

void setup()
{
  myservo1.attach(3);  // attaches the servo on pin 9 to the servo object
  myservo1.attach(5);
  myservo1.attach(6);
  myservo1.attach(9);
}
 
void loop()
{
  val1 = analogRead(potpin1);            // reads the value of the potentiometer (value between 0 and 1023)
  (it comes here)
  val1 = map(val, 0, 1023, 0, 179);      // scale it to use it with the servo (value between 0 and 180)
  myservo1.write(val);                   // sets the servo position according to the scaled value
  delay(15);                             // waits for the servo to get there
  val2 = analogRead(potpin2);            // reads the value of the potentiometer (value between 0 and 1023)
  val2 = map(val, 0, 1023, 0, 179);      // scale it to use it with the servo (value between 0 and 180)
  myservo1.write(val);                   // sets the servo position according to the scaled value
  delay(15);                 
  val3 = analogRead(potpin3);            // reads the value of the potentiometer (value between 0 and 1023)
  val3 = map(val, 0, 1023, 0, 179);      // scale it to use it with the servo (value between 0 and 180)
  myservo1.write(val);                   // sets the servo position according to the scaled value
  delay(15);                 
  val4 = analogRead(potpin4);            // reads the value of the potentiometer (value between 0 and 1023)
  val4 = map(val, 0, 1023, 0, 179);      // scale it to use it with the servo (value between 0 and 180)
  myservo1.write(val);                   // sets the servo position according to the scaled value
  delay(15);                 
Logged

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3882
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 
Code:
val1 = map(VAL, 0, 1023, 0, 179);

I added the capitals for emphasis....You mean val1?

Each of those map lines refers to val, but I guess they should be val1, val2 etc
« Last Edit: February 10, 2013, 03:16:24 am by JimboZA » Logged

The Elders of the Internet know who I am.....

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 109
Posts: 4093
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Variables have a scope, ie a place or places where they are valid to use.  Global variables declared outside of a function at the top of the code, such as val1 etc in your code, are valid to use anywhere in the program.  Variables declared inside a function are only valid to be used within that function.  I can't see any of those in your program.

Either way they have to be declared (made known to the program before they are used).  The variable val in your program is not declared anywhere, hence the error message.

Incidentally, there is a missing right curly bracket in you code too but that may be the result of you copy/pasting it and missing it from the selection.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Ontario, Ohio
Offline Offline
Full Member
***
Karma: 1
Posts: 208
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
val1 = analogRead(potpin1);            // reads the value of the potentiometer (value between 0 and 1023)
(it comes here)

Delete that second line or make it a comment. Also you have imported the servo library twice.

Logged

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3882
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think the "it comes here" was an edit to point out where the error referred to, which was the "val" as opposed to the required "val1" on the next line.
Logged

The Elders of the Internet know who I am.....

Pages: [1]   Go Up
Jump to: