hi guy, I would like developing a code for move to 90° a servo when a button is HIGH. It's a draft to code, but it don't work.
#include <Servo.h>
Servo myServo;
int val = analogRead(A0);
int posizione;
void setup (){
myServo.attach(9);
pinMode(A0, INPUT);
}
void loop (){
posizione = map(val, 0, 1023, 0, 90);
myServo.write(posizione);
delay(15);
}
I have need help!!!
system
2
Don't you want to update your input inside "loop"?
(An analogue input doesn't need a pinMode.)
int val = analogRead(A0);
You can not do this outside of a function.
ok, it's a new code, when I push the botton, the servo go to 90°, but when the botton is LOW the servo it's crazy...
#include <Servo.h>
Servo myServo;
int posizione;
void setup (){
myServo.attach(9);
}
void loop (){
int val = analogRead(A0);
posizione = map(val, 0, 1023, 0, 90);
myServo.write(posizione);
delay(15);
}
Maybe something like this
//zoomkat servo button test 7-30-2011
#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;
void setup()
{
pinMode(button1, INPUT);
servo1.attach(7);
digitalWrite(4, HIGH); //enable pullups to make pin high
}
void loop()
{
press1 = digitalRead(button1);
if (press1 == LOW)
{
servo1.write(90);
}
else {
servo1.write(10);
}
delay(100); //delay for debounce
}
system
6
but when the botton is LOW the servo it's crazy
"LOW" is a description normally reserved for digital inputs.
How is your button wired?
thank you zoomkat, it work perfectly! I understand.
this is a new schema.