Go Down

Topic: what does this mean 'val is not declared in this scope'? (Read 1 time) previous topic - next topic

loogan211

Code: [Select]

#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);                 

JimboZA

#1
Feb 10, 2013, 09:14 am Last Edit: Feb 10, 2013, 09:16 am by JimboZA Reason: 1
Code: [Select]
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
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

UKHeliBob

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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

TeslaIaint

Code: [Select]
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.


JimboZA

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.
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Go Up