Ragazzi, sto cercando di pilotare 2 stepper con un joystick ed Arduino. Per un sistema pan e tilt. Ora ho un problema: su un asse ho un home button centrale, come faccio a farglielo capire. Ho buttato giù questo sketch, che quando trova l'end stop torna indietro di qualche grado e funziona... Però io vorrei che capisse dove si trova e contasse un dato n di step nella direzione che voglio io quando scatta l'endstop.
non so se mi sono spiegato bene....
#include <Stepper.h>
const int stepsPerRevolution = 64; // change this to fit the number of steps per revolution
// initialize the stepper library for both steppers:
Stepper small_stepper(stepsPerRevolution, 8,10,9,11);
Stepper small_stepper2(stepsPerRevolution, 3,5,4,6);
const int homeButton = 2; // initialize pin X endstop button
bool inputWasLow = true;
int buttonState;
int countStep = 1000; // Step counter for position identification
void setup() {
Serial.begin(115200);
// set the speed of the motors
small_stepper.setSpeed(200); // set first stepper speed
small_stepper2.setSpeed(200); // set second stepper speed
pinMode(homeButton, INPUT); // set X endstop button
}
void loop() {
int sensorReading = analogRead(A0); // read value from joystick X-axis
buttonState = digitalRead(homeButton);
Serial.println("inizializzazione");
Serial.println(buttonState);
Serial.println(inputWasLow);
Serial.println(sensorReading);
//delay(200);
if (buttonState== 0)
{
inputWasLow = true;
if (sensorReading < 450) { small_stepper.step(1); // step left
countStep=countStep+1;}
if (sensorReading > 550) { small_stepper.step(-1); // step right
countStep=countStep-1;}
int sensorReading2 = analogRead(A1); // read value from joystick Y-axis
if (sensorReading2 < 450) { small_stepper2.step(1); } // step forward
if (sensorReading2 > 550) { small_stepper2.step(-1); } // step backward
Serial.println(countStep);
}
if (buttonState== 1)
{
// if(inputWasLow)
// {
// inputWasLow = false;}
small_stepper.step(10);
}
}