Robin thank you!
As I am getting better with all of this, I still need guidance are you are helping so much.
From my experience though with this, I found that putting an LCD readout into the loop slowed the Arduino (and motor) down as it was writing to the LCD and sending pulses to the motor at the same time.
What I have done is just put my read button function in the loop, the button calls the run motor function, button off calls the LCD and pauses the motor.
What the LCD shows is the POT reading converted to minutes which is exactly what I need. It is a variable called "spd".
Having said that, do you know of a way to count down the minutes once the track is running? Can be on a new line on the LCD and if i pause the motor the counter pauses too.
You will see what I have tried with the timer function, if I could get it to use "spd" and count it down it would be awesome!
Here is my code (sorry its a bit messy right now)
#include <elapsedMillis.h>
elapsedMillis timer0;
#define interval 1000 // the interval in mS
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(10, 9, 8, 7, 6, 5);
/////// button //////////////////////////////////////
const int buttonPin = 12; // the number of the pushbutton pin
const int ledBTNPin = 11; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
/////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
#define DIR_PIN 2
#define STEP_PIN 3
#define switchPin 4
// show me steps and delay
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
int fwdstepCount = 0; // number of steps the motor has taken forward
int revstepCount = 0; // number of steps the motor has taken forward
const int stepsPerRevolution = 1600;
int stepcount = 3200;
int spd;
int Scount = 0; // count seconds
int Mcount = 0; // count minutes
int Hcount =0; // count hours
int Dcount = 0; // count days
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.clear();
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode (switchPin, INPUT);
// Show me power on LED
pinMode(ledPin, OUTPUT);
// ellapsed timer
timer0 = spd; // clear the timer at the end of startup
}
void loop(){
BTN();
}
void timer(){ // I need to convert timer0 to the spd value and have it change and count down!!!
if (timer0 > interval) {
timer0 -= interval; //reset the timer
// int ledPin = digitalRead(led);
// read the current state and write the opposite
lcd.setCursor(0, 1);
lcd.print("T>");
lcd.setCursor(2, 1);
lcd.print(timer0);
}
}
void rotate(){
/// LED lights with 9volt power on
digitalWrite(ledPin, HIGH);
if (digitalRead(switchPin)){ //if the switch is HIGH, rotate clockwise
digitalWrite(DIR_PIN,HIGH);
lcd.setCursor(13, 0);
lcd.print("REV");
}
else { // if the swtich is LOW, rotate counter clockiwise
digitalWrite(DIR_PIN,LOW);
lcd.setCursor(13, 0);
lcd.print("FWD");
}
for(int i=0; i < 1600 * .0125; i++){
int usDelay = map(analogRead(A0), 10, 1000, 10, 7000);
usDelay = constrain(usDelay, 10, 7000);
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(usDelay);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(usDelay);
// each increment of 10 is about 3.4 mins
// below is POT val x .03428 - 3.4 mins is fastest runtime
//or 4Hrs 240 mins / 70000 = 0.03428 - LCD displays 240 mins :)
spd=usDelay*.03428; // how do I conver this to hours mins and secs?
// need a step amount to calculate spd by steps = duration
}
}
void duration(){
int usDelay = map(analogRead(A0), 10, 1000, 10, 7000);
usDelay = constrain(usDelay, 10, 7000);
spd=usDelay*.03428;
lcd.setCursor(0,0); // sets cursor to 3rd line
lcd.print ("SPD:");
lcd.print(spd);
lcd.setCursor(8,0);
lcd.print("mins");
lcd.setCursor(6, 1);
lcd.print("PAUSE");
}
void BTN(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// BUTTON PUSHED track pause track
digitalWrite(ledBTNPin, LOW); // may not need this
// pot();
//LCD(); // displays values from LCD():
//lcd.setCursor(0,0); // sets cursor to 1st line
//lcd.print ("+SPD:");
//lcd.setCursor(5,0);
//lcd.print(spd);
duration(); // allows pot rotation and values / pauses motor
//rotate();
}
else {
// BUTTON NOT PUSHED track running
digitalWrite(ledBTNPin, HIGH); // may not need this
// TURN LCD off in final here - no display while tracking
//LCD(); // displays values from LCD();
//lcd.clear();
lcd.setCursor(0,0); // sets cursor to 3rd line
lcd.print ("SPD:");
lcd.print(spd);
lcd.setCursor(8,0);
lcd.print("mins");
lcd.setCursor(6, 1);
lcd.print("TRACKING");
rotate();// runs the motor using pot value from duration();
}
}
void LCD(){
// what is doing for me?
if (digitalRead(switchPin)){
// BUTTON NOT PUSHED track running
lcd.setCursor(0,0); // sets cursor to 1st line
lcd.print ("+SPD:");
lcd.setCursor(5,0);
lcd.print(spd);
// lcd.setCursor(15, 0);
// lcd.print("R");
// timer();
}
else { // if the swtich is LOW, rotate counter clockiwise
//BUTTON PRESSED - pause track
// timer();
lcd.setCursor(0,0); // sets cursor to 1st line
// lcd.print ("SPD:");
// lcd.setCursor(5,0);
// lcd.print(spd);
// lcd.setCursor(15, 0);
// lcd.print("R");
// lcd.setCursor(0, 1);
// lcd.print("S>");
// lcd.setCursor(2, 1);
// lcd.print(stepsPerRevolution);
// lcd.setCursor(8, 1);
// lcd.print("A>");
lcd.setCursor(6, 1);
lcd.print("PAUSE");
}
}