#include <Servo.h>
Servo myservo; // create Servo object to control a servo
int potpin = A0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int va;
//servomotor bipolar
#define STEP 13
#define DIR 12
void setup() {
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the Servo object
pinMode(4, OUTPUT);
//servomotor bipolar
pinMode(STEP, OUTPUT);
pinMode(DIR, OUTPUT);
}
void loop() {
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
va = map(val, 0, 1023, 0, 90); // scale it for use with the servo (value between 0 and 180)
myservo.write(va);
digitalWrite(DIR, LOW);
for (int i = 0; i < 200; i++){ // para motor de 0.9° 200 pasos = 180°
digitalWrite(STEP, HIGH); // para motor de 1.8° se requieren solo 100 pasos
delay(10);
digitalWrite(STEP, LOW);
delay(10);
}
digitalWrite(DIR, LOW);
for (int i = 0; i < 200; i++){
digitalWrite(STEP, HIGH);
delay(10);
digitalWrite(STEP, LOW);
delay(10);
}
if (va>=90){
digitalWrite(4, HIGH);
}
else {
digitalWrite(4, LOW);
} // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
Serial.println(val);
}