Allora sono riuscito a collegare 2 stepper ed 1 servo ad un controller PS2 wireless. Tutti funziona rispondendo correttamente ai vari tasti e joystick, però il problema sono gli step dei motori, che vengono influenzati dal delay alla fine del codice:
#include <PS2X_lib.h>
#include <AccelStepper.h>
#include <Servo.h>
PS2X ps2x; // create PS2 Controller Class
int error = 0; // define error variable, initialize as no error
byte type = 0; // define controller type variable, may test removing
byte vibrate = 0; // define vibration strength, default to none
Servo myservo; // create servo object to control a servo
#define servoPin 2 //
int angle = 90; // initial angle for servo
int angleStep = 10;
const int minAngle = 90;
const int maxAngle = 170;
// $$$$$ INITIALIZE PROGRAM VARIABLES AND CONSTANTS $$$$$ //
int stickX = 128; // initialize X-Axis joystick value to the middle of 0-255 range
int stickY = 128; // initialize Y-Axis joystick value to the middle of 0-255 range
int startSwitchState = 0; // initialize start switch state as not pressed
int startLastSwitchState = 0; // initialize start last switch state as not pressed
int R1SwitchState = 0; // initialize R1 switch state as not pressed
int R1LastSwitchState = 0; // initialize R1 last switch state as not pressed
int R2SwitchState = 0; // initialize R2 switch state as not pressed
int R2LastSwitchState = 0; // initialize R2 last switch state as not pressed
int L1SwitchState = 0; // initialize L1 switch state as not pressed
int L1LastSwitchState = 0; // initialize L1 last switch state as not pressed
int L2SwitchState = 0; // initialize L2 switch state as not pressed
int L2LastSwitchState = 0; // initialize L2 last switch state as not pressed
const int stepX = 3; //pin digitale che invia i segnali di STEP al driver delle X
const int dirX = 4; //pin digitale che invia il segnale DIREZIONE al driver delle X
long speedX, valX, mapX; //variabili di gestione movimenti motore X
const int stepY = 5; //pin digitale che invia i segnali di STEP al driver delle Y
const int dirY = 6; //pin digitale che invia il segnale DIREZIONE al driver delle Y
long speedY, valY, mapY; //variabili di gestione movimenti motore Y
//variabili utilizzate dalla libreria AccelStepper
int maxSpeed = 200; //stando alla documentazione della libreria questo valore può essere impostato fino a 4000 per un Arduino UNO
int minSpeed = 0; //velocità minima del motore
const float accelerazione = 3; //numero di step al secondo in accelerazione
const int treshold = 10; //la lettura dei potenziometri non è mai affidabile al 100%, questo valore aiuta a determinare il punto da considerare come "Stai fermo" nei movimenti
long tresholdUp, tresholdDown; //variabili di servizio per espletare il compito descritto sopra
boolean abilitato, muoviX, muoviY, enable; //variabili di gestione dei movimenti
//istanzia i motori
AccelStepper motoreX(AccelStepper::DRIVER, stepX, dirX);
AccelStepper motoreY(AccelStepper::DRIVER, stepY, dirY);
void setup() {
Serial.begin(9600); // begin serial communication, set at 57600 Baud rate, may test changing
error = ps2x.config_gamepad(13, 10, 11, 12, false, false); // check for errors on PS2 controller
myservo.attach(servoPin); // attaches the servo on pin 3 to the servo object
myservo.write(angle);
//configura parametri dei motori
motoreX.setMaxSpeed(maxSpeed);
motoreX.setSpeed(minSpeed);
motoreX.setAcceleration(accelerazione);
motoreY.setMaxSpeed(maxSpeed);
motoreY.setSpeed(minSpeed);
motoreY.setAcceleration(accelerazione);
if (error == 0)
Serial.print("Found Controller, Configured Successfully.");
else if (error == 1)
Serial.println("No Controller Found.");
else if (error == 2)
Serial.println("Controller Found but not Accepting Commands.");
else if (error == 3)
Serial.println("Controller Refusing to Enter Pressures Mode, May Not Support it.");
type = ps2x.readType(); // find the type of the
switch (type) {
case 0:
Serial.print("Unknown Controller Type Found ");
break;
case 1:
Serial.print("DualShock Controller Found ");
break;
}
}
void loop() {
if (error == 1) { // skip loop if no controller is found, may test adding config_gamepad(pins)
ps2x.config_gamepad(13, 10, 11, 12, false, false); // reconfigure PS2 controller to attempt fixing error
return;
}
ps2x.read_gamepad(false, vibrate); //read controller, don't record arrow pressures, and set large motor to spin at 'vibrate' speed
R2SwitchState = ps2x.ButtonPressed(PSB_R2); // read R2 switch state
if (R2SwitchState != R2LastSwitchState) { // see if button state changed
if (R2SwitchState == HIGH) { // if the button was pressed, not released
myservo.write(maxAngle); // move the servo to desired angle
Serial.println("R2 PRESSED");
}
}
R2LastSwitchState = R2SwitchState; // save switch state for comparison
if (ps2x.ButtonPressed(PSB_RED)) {
Serial.println("Circle just pressed");
}
if (ps2x.ButtonReleased(PSB_PINK)) {
Serial.println("Square just pressed");
}
if (ps2x.NewButtonState(PSB_BLUE)) {
Serial.println("X just changed");
}
L2SwitchState = ps2x.ButtonPressed(PSB_L2); // read L2 switch state
if (L2SwitchState != L2LastSwitchState) { // see if button state changed
if (L2SwitchState == HIGH) {
Serial.println("L2 PRESSED");
myservo.write(minAngle); // move the servo to desired angle
}
}
L2LastSwitchState = L2SwitchState; // save switch state for comparison
R1SwitchState = ps2x.ButtonPressed(PSB_R1); // read R1 switch state
if (R1SwitchState != R1LastSwitchState) { // see if button state changed
if (R1SwitchState == HIGH) {
Serial.println("R1 PRESSED");
}
}
R1LastSwitchState = R1SwitchState; // save switch state for comparison
L1SwitchState = ps2x.ButtonPressed(PSB_L1); // read L1 switch state
if (L1SwitchState != L1LastSwitchState) { // see if button state changed
if (L1SwitchState == HIGH) {
Serial.println("L1 PRESSED");
}
}
L1LastSwitchState = L1SwitchState; // save switch state for comparison
startSwitchState = ps2x.ButtonPressed(PSB_START); // read Start switch state*/
if (startSwitchState != startLastSwitchState) { // see if button state changed
if (startSwitchState == HIGH) {
Serial.println("START PRESSED");
}
}
startLastSwitchState = startSwitchState; // save switch state for comparison
stickX = ps2x.Analog(PSS_LX); // set the analog value of the right stick X-Axis equal (0-255)
if (stickX != 128) {
//Serial.print("X-AXIS:");
//Serial.println(stickX, DEC);
}
stickY = ps2x.Analog(PSS_LY); // set the analog value of the left stick Y-Axis equal (0-255)
if (stickY != 127) {
//Serial.print("Y-AXIS:");
//Serial.println(stickY, DEC);
}
// $$$$$ MAP AND MOVE X-AXIS STEPPER MOTOR $$$$$ //
int mapX = stickX; // set the desired stepper position equal to the X-Axis value (0-255)
minSpeed = 0; // reset speed
if (mapX > 130) { // if the X-Axis stick position is positive
speedX = map(mapX, maxSpeed, 130, maxSpeed, minSpeed);
muoviX = true;
//Serial.println("muovo X a destra");
//Serial.println(speedX);
//Serial.println(mapX);
} else if (mapX < 126) { // if the X-Axis stick position is negative
speedX = -map(mapX, 128, minSpeed, minSpeed, maxSpeed);
muoviX = true;
//Serial.println("muovo X a sinistra");
//Serial.println(speedX);
//Serial.println(mapX);
} else {
//x sta fermo
speedX = 0;
muoviX = false;
}
if (muoviX) {
motoreX.setSpeed(speedX);
motoreX.run();
} else {
motoreX.stop();
}
// MUOVO Y-AXIS STEPPER MOTOR //
mapY = stickY; // set the desired stepper position equal to the Y-Axis value (0-255)
minSpeed = 0; // reset speed
if (mapY > 127) { // if the Y-Axis stick position is positive
speedY = map(mapY, maxSpeed, 127, maxSpeed, minSpeed);
muoviY = true;
//Serial.println("muovo Y su");
//Serial.println(speedY);
//Serial.println(mapY);
} else if (mapY < 127) { // if the Y-Axis stick position is negative
speedY = -map(mapY, 127, minSpeed, minSpeed, maxSpeed);
muoviY = true;
//Serial.println("muovo Y giu");
//Serial.println(speedY);
//Serial.println(mapY);
} else {
//y sta fermo
speedY = 0;
muoviY = false;
}
if (muoviY) {
motoreY.setSpeed(speedY);
motoreY.run();
} else {
motoreY.stop();
}
delay(10);
}
in pratica anche se cambio maxSpeed ed accelerazione poco cambia, mentre se cambio il delay cambia molto, se invece lo tolgo va proprio a scatti..... come dovrei impostarlo?