'myservo' was not declared in this scope eror in code

I wont to make servo goes 90 degrees when button is pressed and then back up to 0 degrees when button is released but when I write my code with (myservo.write(90); or myservo.attach(9):wink: it says( ‘myservo’ was not declared in this scope). But I imported #include <Servo.h>. Do you have any suggestions what is problem?

I'm confused. Are you asking for help with code that you haven't shown us? How do you expect that to work?

Here's some actual help rather than snark.

You probably left this out:

Servo myservo;
#include <Servo.h>

void setup() {
Servo myservo;  
pinMode(A0,INPUT);
pinMode(12, OUTPUT);
myservo.attach(9);
}

void loop() {
if(A0 == HIGH){
  myservo.write(90);
   digitalWrite(12, HIGH);
  }
if(A0 == LOW){
  myservo.write(0);
digitalWrite(12, LOW);
  } 
}

I am sory for not putting my code. I wrote this but it says again ‘myservo’ was not declared in this scope.

You have myservo defined inside the setup() function. So it is a local variable to setup(). Note the word “scope” in the error message. Google “Variable Scope” to find more complete explanations.

To solve your problem, make it a global variable:

#include <Servo.h>

Servo myservo;

void setup() { 
  pinMode(A0, INPUT);
  pinMode(12, OUTPUT);
  myservo.attach(9);
}

void loop() {
  if (A0 == HIGH) {
    myservo.write(90);
    digitalWrite(12, HIGH);
  }
  if (A0 == LOW) {
    myservo.write(0);
    digitalWrite(12, LOW);
  }
}

You have problems with scope.
Variables declared inside of a routine are only available within that routine.
You declared your myservo object inside of the setup routine.
Therefore it is only in scope within the setup routine.

If instead of declaring myservo inside of the setup routine, you declared it outside of the setup routine, it will be in a global scope, available to all routines.

Move the declaration right after the include for the library.

#include <Servo.h>#include <Servo.h>

void setup() {
Servo myservo; 
pinMode(A0,INPUT);

void setup() {
Servo myservo; 
pinMode(A0,INPUT);

Edit: gfvalvo beat me by a few seconds… :wink:

jelkoamni:

#include <Servo.h>

void setup() {
Servo myservo;  
pinMode(A0,INPUT);
pinMode(12, OUTPUT);
myservo.attach(9);
}

void loop() {
if(A0 == HIGH){
 myservo.write(90);
  digitalWrite(12, HIGH);
 }
if(A0 == LOW){
 myservo.write(0);
digitalWrite(12, LOW);
 }
}




I am sory for not putting my code. I wrote this but it says again 'myservo' was not declared in this scope.

That is because you have declared your myservo object inside the setup() function therefore it only exists while the setup() function is being executed. It does not exist outside the scope of the setup() function and thus is not a valid object in the loop() function.

if you put the

Servo myservo;

line outside the setup() function it becomes a global object accessible from all functions.

By the way your conditionals should be a test of the value returned by a digital read from pin A0 i.e.

if (digitalRead(A0) == LOW) ...
and 
if (digitalRead(A0) == HIGH) ...

Not

if (A0 == LOW) ...
and 
if (A0 == HIGH) ...

Hope this helps
Ian

Thank you very much!

Are you happy with the codes/sketch borrowed from Post#4? Is it working now as you have expected?

#include <Servo.h>

Servo myservo;

void setup() {
  pinMode(A0, INPUT);
  pinMode(12, OUTPUT);
  myservo.attach(9);
}

void loop() {
  if (A0 == HIGH) {
    myservo.write(90);
    digitalWrite(12, HIGH);
  }
  if (A0 == LOW) {
    myservo.write(0);
    digitalWrite(12, LOW);
  }
}

Note:

if (A0 == HIGH) 
{
    myservo.write(90);
    digitalWrite(12, HIGH);
  }

Can you test a symbolic named variable like A0 without assigning any value to it?

GolamMostafa:
Are you happy with the codes/sketch borrowed from Post#4? Is it working now as you have expected?

#include <Servo.h>

Servo myservo;

void setup() {
pinMode(A0, INPUT);
pinMode(12, OUTPUT);
myservo.attach(9);
}

void loop() {
if (A0 == HIGH) {
myservo.write(90);
digitalWrite(12, HIGH);
}
if (A0 == LOW) {
myservo.write(0);
digitalWrite(12, LOW);
}
}




**Note:**


if (A0 == HIGH)
{
myservo.write(90);
digitalWrite(12, HIGH);
}




Can you test a symbolic named variable like **A0** without assigning any numerical value to it?

A0 is provided by the Arduino framework.
On Uno, it has the value 14.

TheMemberFormerlyKnownAsAWOL:
A0 is provided by the Arduino framework.
On Uno, it has the value 14.

A0 (DPin-14) is an input pin with external pull-down (most probably). He (the OP) has a button connected with A0-pin.

In fact, I wanted to say if OP needs to moderate his codes as follows:

bool n = digitalRead(A0);  //or digitalRead(14);
if(n == HIGH)
{
  //.........
}

…but you didn’t say that.
You said

Can you test a symbolic named variable like A0 without assigning any value to it?

TheMemberFormerlyKnownAsAWOL:
...but you didn't say that.
You said

Why my expression did appear flawed -- at the very first visit of OP's this code: if (A0 == HIGH){//---}, my mind took A0 as a variable, which is actually a pre-defined pin of UNO.