hi guys…
I apologize in advance for my bad english
I just bought my “Arduino uno board”…yesterday i played with this amazing microcontroller…
now i wrote a code:
#include <Servo.h> // includo la libreria sui servi
Servo s; // definisco un tipo servo di nome s
void setup() {
Serial.begin(9600); // inizializzo la comunicazione con il pc
pinMode(8, INPUT); // setto il pin 8 come input
pinMode(9, INPUT); // setto il pin 9 come input
s.attach(2); // attacco il servo al pin 2
s.write(0); // setto un angolo iniziale del servo pari a zero
delay(2000); //attendo 2 secondi
}
void loop() {
int butt1 = digitalRead(8); // butt1= valore sul pin 8
int butt2 = digitalRead(9); // butt2= valore sul pin 9
if(butt1 == HIGH){ // se premo butt1 (quando premo scorrono i 5V)
s.write(90); // porto il servo a 90°
delay(3000);
Serial.println("prenuto 1"); // stampo a video che ho premuto butt1
}
if(butt2 == HIGH){ // se premo butt2
s.write(0); // porto il servo a 0°
delay(3000);
Serial.println("premuto 2"); // stampo a video che ho premuto butt2
}
delay(10);
}
what i would do is:
when i push butt1 → servo go in position 90° and in serial monitor: “premuto1”
when i push butt2 → sero go in position 0° and in serial monitor: “premuto2”
it’s simple but it doesn’t work.
if i don’t connect my servo in my circuit i can see in my serial monitor “premuto 1” when i push butt1 and “premuto 2” when i push butt2, but if my servo is connect to my circuit nothing appears in my derial monitor and my circuit doesn’t work.
here a conceptual scheme:
How much milliampere must the servo have to work?
I think you can't connect it directly to your Arduino, because it can only source 40mA per output!!
And the most servo's use much more then that!!
Try this for the switches:
Use internal pullups, change the logic to look for a Low instead of a High.
Also try rotating the buttons 1/4 turn, often times pin 1/3 & 2/4 are wired together, or 1/2 & 3/4.
Use a multimeter to confirm which you have.
pinMode(8, INPUT); // setto il pin 8 come input
digitalWrite ( 8, HIGH); // enable internal pullup resistor <<<<<<
pinMode(9, INPUT); // setto il pin 9 come input
digitalWrite ( 9, HIGH); // enable internal pullup resistor <<<<<<
int butt1 = digitalRead(8); // butt1= valore sul pin 8
int butt2 = digitalRead(9); // butt2= valore sul pin 9
if(butt1 == LOW){ // se premo butt1 (quando premo scorrono i 5V) <<<<<<<<<<<<<<<<<<<
s.write(90); // porto il servo a 90°
delay(3000);
Serial.println("prenuto 1"); // stampo a video che ho premuto butt1
}
if(butt2 == LOW){ // se premo butt2 <<<<<<<<<<<<<
@CrossRoads
i tried to do as you suggested, but still doesn’t wok…
when i push butt1 the servo go to 90° but as soon as return to last position…like an impulse… :~ :~
so, when i push butt2 nothing appens because the servo is alrady in 0°.
So i changed my sketch:
#include <Servo.h> // includo la libreria sui servi
Servo s; // definisco un tipo servo di nome s
void setup() {
Serial.begin(9600); // inizializzo la comunicazione con il pc
pinMode(8, INPUT); // setto il pin 8 come input
digitalWrite( 8, HIGH);
pinMode(9, INPUT); // setto il pin 9 come input
digitalWrite(9, HIGH);
s.attach(2); // attacco il servo al pin 2
s.write(45,); // setto un angolo iniziale del servo pari a zero <<<<<<<<<<
delay(2000); //attendo 2 secondi
}
void loop() {
int butt1 = digitalRead(8); // butt1= valore sul pin 8
int butt2 = digitalRead(9); // butt2= valore sul pin 9
if(butt1 == LOW){ // se premo butt1 (quando premo scorrono i 5V)
s.write(90); // porto il servo a 90°
delay(1500);
Serial.println("prenuto 1"); // stampo a video che ho premuto butt1
}
if(butt2 == LOW){ // se premo butt2
s.write(0); // porto il servo a 0°
delay(1500);
Serial.println("premuto 2"); // stampo a video che ho premuto butt2
}
delay(10);
}
i setted start position of my servo to 45°.
In this way when i push butt1 the servo go to 90°but return to 45°
instead when i push butt2 servo go to 0° but return as soon as to 45°
I don’t understand this strange behavior…