Hola a esta comunidad.
Primero, no soy ni siquiera aprendiz de programador.
Rastreando datos en la web he conseguido hacer funcionar perfectamente el ejemplo 5 que aparece en la página de:
Easy Driver Examples.
Le he agregado un LCD 20x4 con el I2C.
He modificado el ejemplo "5" agregándole las cosas a mi escaso entender necesaria para que me muestre el sentido y la velocidad de rotación del motor.
El display me muestra la velocidad pero mal: hace lento el funcionamiento del motor y deja caracteres al disminuir la velocidad que convierten al dato en algo erróneo.
Adjunto Sketch y diagrama conexiones.
// Example5 code for Brian Schmalz's Easy Driver Example page, plus mod LCD I2C
// http://www.schmalzhaus.com/EasyDriver/EasyDriverExamples.html
// compilado CAP 11/10/2018 21.06
#include <AccelStepper.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
// Define the stepper and the pins it will use
AccelStepper stepper1(AccelStepper::DRIVER, 9, 8);
// Define our three input button pins
#define LEFT_PIN 4
#define STOP_PIN 3
#define RIGHT_PIN 2
// Define our analog pot input pin
#define SPEED_PIN 0
// Define our maximum and minimum speed in steps per second (scale pot to these)
#define MAX_SPEED 960
#define MIN_SPEED 8
void setup() {
// The only AccelStepper value we have to set here is the max speeed, which is higher than we'll ever go
stepper1.setMaxSpeed(10000.0);
// Set up the three button inputs, with pullups
pinMode(LEFT_PIN, INPUT_PULLUP);
pinMode(STOP_PIN, INPUT_PULLUP);
pinMode(RIGHT_PIN, INPUT_PULLUP);
lcd.begin(20,4); //initialize the lcd
lcd.clear();
lcd.setCursor(3, 0); // Move the cursor to 3d position on 1st line
lcd.print(" Mitsumi 375");
lcd.setCursor(0, 3); // Move the cursor to 1th position on 1st line
lcd.print("Speed:");
lcd.setCursor(12,3);
lcd.print("rpm");
}
void loop() {
static float current_speed = 0.0; // Holds current motor speed in steps/second
static int analog_read_counter = 1000; // Counts down to 0 to fire analog read
static char sign = 0; // Holds -1, 1 or 0 to turn the motor on/off and control direction
static int analog_value = 0; // Holds raw analog value.
// If a switch is pushed down (low), set the sign value appropriately
if (digitalRead(LEFT_PIN) == 0) {
sign = 1;
lcd.setCursor(0, 1); // Move the cursor to 7th position on 2nd line
lcd.print("Rot: <<CCW<<"); // Print CW direction
}
else if (digitalRead(RIGHT_PIN) == 0) {
sign = -1;
lcd.setCursor(0, 1); // Move the cursor to 7th position on 2nd line
lcd.print("Rot: >>>CW>>"); // Print CW direction
}
else if (digitalRead(STOP_PIN) == 0) {
sign = 0;
lcd.setCursor(0, 1); // Move the cursor to 1rst position on 2nd line
lcd.print("Rot: Stopped"); // Print Stopped
}
// We only want to read the pot every so often (because it takes a long time we don't
// want to do it every time through the main loop).
if (analog_read_counter > 0) {
analog_read_counter--;
}
else {
analog_read_counter = 3000;
// Now read the pot (from 0 to 1023)
analog_value = analogRead(SPEED_PIN);
// Give the stepper a chance to step if it needs to
stepper1.runSpeed();
// And scale the pot's value from min to max speeds
current_speed = sign * (((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED);
// Update the stepper to run at this new speed
stepper1.setSpeed(current_speed);
}
// This will run the stepper at a constant speed
stepper1.runSpeed();
}
lcd.setCursor(7, 2); // Move the cursor to 7th position on 3nd line
lcd.print((current_speed)/96*60)); // Print Speed
}
}
Cualquier ayuda o idea bienvenida. Gracias
SchmalzhausExa5-i2cLCD.txt (3.38 KB)