The idea is to set the "step-by-turn" VMAX to calibrate the range in which I can move with the potentiometer.
If A0 (the potentiometer) is at 0
and keep the SET button pressed for 3s then I enter the calibration program.
I can move the motor step by step with the "FF" and "REV" buttons, and for every pulse the Pololu receives, +1 is added to the "CON" variable
Once the range of motion is calibrated, the SET button is pressed again for three seconds and the VMAX value (200 by default) is replaced by the "CON" value.
Can I explain?
My dude is:
I am following the logic in the programming well?
I leave the code.
PD: Something that I am missing in the code is that when it finishes setting the engine it returns to the starting point.
#include "AccelStepper.h"
// Library created by Mike McCauley at http://www.airspayce.com/mikem/arduino/AccelStepper/
// AccelStepper Setup
AccelStepper stepper(1, 8, 5); // 1 = Easy Driver interface
// Nano Pin 8 connected to STEP pin of Easy Driver
// Nano Pin 5 connected to DIR pin of Easy Driver
// Variables to store current, previous and move position
int val = 0;
int previous = 0;
int long newval = 0;
int VMIN = 0;
int VMAX = 200;
int CONT = 0;
int SETPIN = 1;
int FFPIN = 2;
int REVPIN = 6;
unsigned long tiempo; //Variable para el control
unsigned long tiempo_max= 3000; //Variable para el control
boolean estado= false;
void setup() {
Serial.begin(9600);
pinMode(SETPIN,INPUT);
pinMode(FFPIN,INPUT);
pinMode(2,OUTPUT);
digitalWrite (2, HIGH);
stepper.setMaxSpeed(4800); // Set speed fast enough to follow pot rotation
stepper.setAcceleration(4800); // High Acceleration to follow pot rotation
digitalWrite(SETPIN,HIGH); //Activa PullUp resistor
digitalWrite(FFPIN,HIGH);
}
void loop() {
while(digitalRead(SETPIN)==LOW){
if(millis()-tiempo >= tiempo_max){
estado = true;
}
}
if (estado == true){analog();}
digitalWrite(SETPIN,HIGH); //Activa PullUp resistor
tiempo= millis();
}
void calibracion(){
while(digitalRead(FFPIN)==HIGH){
digitalWrite(8, HIGH);
delay(50);
digitalWrite(8, LOW);
int cont = (cont+1);
}
while(digitalRead(SETPIN)==LOW){
if(millis()-tiempo >= tiempo_max){
estado = true;
}
}
if (estado == true)
{ int cont = VMAX;
digitalWrite(SETPIN,HIGH); //Activa PullUp resistor
tiempo= millis();
{giro();}
}
}
void analog(){
Serial.println(A0);
val = analogRead(A0); // Read Potentiometer current value
if (val = 0) {calibracion();}
}
void giro () {
Serial.println(A0);
val = analogRead(A0); // Read Potentiometer current value
if ((val > previous+6) || (val < previous-6)) { // Check that a move of the pot is at least > or < than 6
newval = map(val, 0, 1023, VMIN, VMAX); // Map value (1600 = 1 stepper shaft rotation)
stepper.runToNewPosition(newval); // Move stepper to new position
previous = val; // save current value into variable previous
}
}