I've happened be introduce to Arduino in a magazine and have been hook ever since.
Currently learning Arduino programming through youtube channels.
However, I'm having a hard time as I've almost zero programming experience.
I would like post a basic question that I could not find on the net.
What is the difference between declaring the integer outside the void setup VS declaring the integer inside the void setup.
I was told that void setup only run once and I don't understand it.
For starter kit project 03 (love-o-meter)
the integer is declared outside...
but for servo the intger is declared inside.
When I switch it around, it doesn't run.
Can help explains what is going on?
#include <Servo.h>
Servo myServo;
int const potPin = A0;
int potVal;
int angle;
void setup()
{
myServo.attach(9);
Serial.begin(9600);
}
void loop()
{
potVal = analogRead(potPin);
Serial.print("potVal: ");
Serial.print(potVal);
angle = map(potVal,0 ,1023, 0 ,179);
Serial.print("angle: ");
Serial.println(angle);
myServo.write(angle);
delay(15);
}
vs
#include <Servo.h>
Servo myServo;
int const potPin = A0;
void setup()
{
int potVal;
int angle;
myServo.attach(9);
Serial.begin(9600);
}
void loop()
{
potVal = analogRead(potPin);
Serial.print("potVal: ");
Serial.print(potVal);
angle = map(potVal,0 ,1023, 0 ,179);
Serial.print("angle: ");
Serial.println(angle);
myServo.write(angle);
delay(15);
}