I have a stepper motor (1 step = 0.0125 mm) that will have:
- Rotate at a speed set between 0.00028 mm / s and 1.25 mm / s
- And make a certain number of turns, a distance (between 0 and 500 mm).
The adjustment is made on an LCD screen and using 3 potentiometers:
- 2 for the speed setting broad and finer to adjust. The display will be in mm / s.
- 1 for the distance, the display will be in mm.
When the switch is OFF, you can adjust the speed and distance.
When the switch is ON, the motor rotates in the previous settings.
During engine operation, it must be able to stop without waiting for the end of the delay by returning the switch to OFF.
I have a problem for calculating the rotation time.
Can you help me?
#include <Stepper.h>
#include <LiquidCrystal.h>
const int stepsPerRevolution = 200; // the number of steps per revolution
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9,10,11);
// initialize the LCD pin
LiquidCrystal lcd(13,12,5,4,3,2);
// this constant won't change:
const int buttonPin = 7; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
unsigned long t1, t2, time;
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
unsigned long turnTime; // temporisation rotation moteur
unsigned long stepTime; // temporisation pas moteur
float speedMoteurScreen;
void setup() {
// LCD sizes. We're using one that's 2 lines of 16 characters,
lcd.begin(16, 2);
// Let's clear the LCD
lcd.clear();
// contrast for LCD screen
analogWrite(6, 120);
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// read the sensorSpeedLarge value:
int sensorSpeedLarge = analogRead(A0);
// map it to a range from 0 to 1000:
double speedMoteurLarge = map(sensorSpeedLarge, 0, 1023, 28, 125000);
// read the sensorSpeedLarge value:
int sensorSpeedFine = analogRead(A1);
// map it to a range from 0 to 1000:
int speedMoteurFine = map(sensorSpeedFine, 0, 1023, -50, 50);
// read the sensorShortening value:
int sensorShortening = analogRead(A2);
// map it to a range from 0 to 1000:
int disTance = map(sensorShortening, 0, 1023, 0, 500);
double speedMoteur = speedMoteurLarge + speedMoteurFine;
speedMoteurScreen=speedMoteur/100000; //100000 pour affichage en mm/s
stepTime = (0.0125/speedMoteurScreen)*1000; //*1000 car secondes en entree
turnTime = (disTance/speedMoteurScreen)*1000; //*1000 car secondes en entree
lcd.setCursor(0,0);
lcd.print("v = " );
lcd.print(speedMoteurScreen);
lcd.print (" mm/s");
lcd.setCursor(0,1);
lcd.print("d = " );
lcd.print(disTance);
lcd.print (" mm");
delay(100);
lcd.clear();
t1= millis();
// compare the buttonState to its previous state
if ((buttonState != lastButtonState) && (buttonState == HIGH))
{
digitalWrite(ledPin, HIGH);
while ((buttonState == HIGH) && (time < turnTime))
{
t2 = millis();
myStepper.step(-1);
delay(stepTime);
buttonState = digitalRead(buttonPin);
time = t2-t1;
}
}
digitalWrite(ledPin, LOW);
lastButtonState = buttonState;
time=0;
}