Buongiorno,
Vorrei come da titolo, comandare uno stepper ULN 2003 tramite Joystick.
Fin qui tutto bene, alquanto facile, ho provato una varietà di sketch presi in rete, per per trovare quello più adatto alle mie esigenze.
Purtroppo, caricando uno sketch è successo un disastro.
Non solo, non funziona, ma ancora peggio, quando collego il nano al pc, le linee TX e RX; restano occupate a ricevere ed inviare chissà quali dati. Impedendomi di caricare qualsiasi altro sketch o di compiere; una qualsivoglia azione, come semplicemente aprire il monitor seriale.
Questi sono tre degli sketch molto simili tra loro, praticamente uguali, solo che uno di questi e forse neanche per colpa di uno di loro, quando collego le linee sono intasate.
Primo:
#include <Stepper.h>
#define STEPS 32
// define stepper motor control pins
#define IN1 2
#define IN2 3
#define IN3 4
#define IN4 5
// initialize stepper library
Stepper stepper(STEPS, IN4, IN2, IN3, IN1);
// joystick pot output is connected to Arduino A0
#define joystick A2
void setup()
{
Serial.begin(9600);
}
void loop()
{
// read analog value from the potentiometer
int val = analogRead(joystick);
// if the joystic is in the middle ===> stop the motor
if( (val > 500) && (val < 530) )
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
Serial.print("Sono Fermo");
Serial.print("\n");
delay(500);
}
else
{
// move the motor in the first direction
while (val >= 500)
{
Serial.print("val prima ");
Serial.print(val);
Serial.print("\n");
// map the speed between 5 and 500 rpm
int speed_ = map(val, 1000, 1023, 200, 1000);
Serial.print("speed_ prima ");
Serial.print(speed_);
Serial.print("\n");
// set motor speed
stepper.setSpeed(speed_);
// move the motor (1 step)
stepper.step(1);
val = analogRead(joystick);
}
// move the motor in the other direction
while (val <= 530)
{
Serial.print("val-500 prima ");
Serial.print(val);
Serial.print("\n");
// map the speed between 5 and 500 rpm
int speed_ = map(val, 530, 0, 200, 1000);
Serial.print("speed_-500 prima ");
Serial.print(speed_);
Serial.print("\n");
// set motor speed
stepper.setSpeed(speed_);
// move the motor (1 step)
stepper.step(-1);
val = analogRead(joystick);
}
}
}
Secondo:
// include Arduino stepper motor library
#include <Stepper.h>
// define number of steps per revolution
#define STEPS 32
// define stepper motor control pins
#define IN1 2
#define IN2 3
#define IN3 4
#define IN4 5
// initialize stepper library
Stepper stepper(STEPS, IN4, IN2, IN3, IN1);
// joystick pot output is connected to Arduino A0
#define joystick A2
void setup()
{
Serial.begin(9600);
}
void loop()
{
// read analog value from the potentiometer
int val = analogRead(joystick);
// if the joystic is in the middle ===> stop the motor
if( (val > 500) && (val < 523) )
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
Serial.print("Sono Fermo");
Serial.print("\n");
delay(500);
}
else
{
// move the motor in the first direction
while (val >= 523)
{
// map the speed between 5 and 500 rpm
int speed_ = map(val, 523, 1023, 5, 500);
Serial.print("speed_ prima ");
Serial.print(speed_);
Serial.print("\n");
// set motor speed
stepper.setSpeed(speed_);
// move the motor (1 step)
stepper.step(1);
val = analogRead(joystick);
}
// move the motor in the other direction
while (val <= 500)
{
// map the speed between 5 and 500 rpm
int speed_ = map(val, 500, 0, 5, 500);
// set motor speed
stepper.setSpeed(speed_);
// move the motor (1 step)
stepper.step(-1);
val = analogRead(joystick);
}
}
}
Terzo:
/*
* Unipolar stepper motor speed and direction control with Arduino
* and joystick
* This is a free software with NO WARRANTY.
* http://simple-circuit.com/
*/
// include Arduino stepper motor library
#include <Stepper.h>
// define number of steps per revolution
#define STEPS 32
// define stepper motor control pins
#define IN1 2
#define IN2 3
#define IN3 4
#define IN4 5
// initialize stepper library
Stepper stepper(STEPS, IN4, IN2, IN3, IN1);
// joystick pot output is connected to Arduino A0
#define joystick A2
void setup()
{
}
void loop()
{
// read analog value from the potentiometer
int val = analogRead(joystick);
// if the joystic is in the middle ===> stop the motor
if( (val > 500) && (val < 523) )
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
else
{
// move the motor in the first direction
while (val >= 523)
{
// map the speed between 5 and 500 rpm
int speed_ = map(val, 523, 1023, 5, 500);
// set motor speed
stepper.setSpeed(speed_);
// move the motor (1 step)
stepper.step(1);
val = analogRead(joystick);
}
// move the motor in the other direction
while (val <= 500)
{
// map the speed between 5 and 500 rpm
int speed_ = map(val, 500, 0, 5, 500);
// set motor speed
stepper.setSpeed(speed_);
// move the motor (1 step)
stepper.step(-1);
val = analogRead(joystick);
}
}
}
Se potreste aiutarmi a capire, se e quale sketch abbia combinato qualcosa ed a capire dove ho sbagliato; per poter rimediare.
Ve ne sarei molto grato.
Cordiali Saluti
Manuel