Convert POT value to duration

Hi all

I am a timelspe track that I am using a pot to control speed with.
My goal is to set a defined number of steps and calculate the tracking durating - convert into hours/mins/secs.
Im displaying the value on an LCD. So turn the POT hit a button to update the LCD and it will show duration - release button and the stepper starts (this is done without the duration and number of steps - the reading i currently get is just the usDelay value or spd)

The function below is what I am using, does anyone have any thoughts?

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(15, 0);   
    lcd.print("R");
  }
  else { // if the swtich is LOW, rotate counter clockiwise
    digitalWrite(DIR_PIN,LOW);
     lcd.setCursor(15, 0);   
    lcd.print("F");
  }

  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);
    spd=usDelay/10; // how do I conver this to hours mins and secs?
    // need a step amount to calculate spd by steps = duration

  }

}

Posting the full code, would be nice. Also why have "for(int i=0; i < 1600 * .0125; i++){" and not simply 20? Why have a FOR loop at all, if you not averaging the readings together or anything that requires you to get 20 readings?

What duration should 0 correspond to? What duration should 1023 correspond to? Figure out those two values, and mapping from the value you get from analogRead() to a duration is trivial.

RE duration.
The duration is variable as the pot runs the dolly from 5mins to 4hours.

I was looking for a calculation that could tell me how long the track would run based on the pot reading.

So maybe this is sort of what id be needing help with

Number of Steps (full track length) calculated with pot reading = duration

So lets assume a number for steps in total at 160000 and a pot reading of 516 - how could I resolve this into hour mins and secs?

Here is my full sketch so far

#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);
  pinMode(DIR_PIN, OUTPUT);
  pinMode(STEP_PIN, OUTPUT);
  pinMode (switchPin, INPUT);

  // Show me power on LED
  pinMode(ledPin, OUTPUT);    
}

void loop(){
 // rotate();//will rotate 2 times, with Pot control 
BTN();
}

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(15, 0);   
    lcd.print("R");
  }
  else { // if the swtich is LOW, rotate counter clockiwise
    digitalWrite(DIR_PIN,LOW);
     lcd.setCursor(15, 0);   
    lcd.print("F");
  }
//duration();
  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);
    spd=usDelay/10; // 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/10;
         lcd.setCursor(0,0);                // sets cursor to 3rd line
  lcd.print ("SPD:");
        lcd.setCursor(5,0);
            lcd.print(spd);
}



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) {     
    // turn LED on:  POT  
    digitalWrite(ledBTNPin, LOW); 

  //  pot();
   duration();
   //rotate();
  } 
  else {
    // turn LED off: RAMP
    digitalWrite(ledBTNPin, HIGH); 

    LCD();
    rotate();//will rotate a 1/2 turn, with Pot control 
  //  ramp();
  //  stepper.run();  

  }
}



void LCD(){
        
  if (digitalRead(switchPin)){ //if the switch is HIGH, rotate clockwise
         lcd.setCursor(0,0);                // sets cursor to 3rd 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(10, 1);
    lcd.print("ON");
  }
  else { // if the swtich is LOW, rotate counter clockiwise
         lcd.setCursor(0,0);                // sets cursor to 3rd 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(10, 1);
    lcd.print("OFF");
  }
}

As for this code, I am not sure why I have this. I had help with this. All I have deduced is that 1600 is steps per rev, .0125 is amout of rotation before driection chenge with the switch.
The idea of this code was to get the largest step range for fast and slow stepping

for(int i=0; i < 1600 * .0125; i++){
    int usDelay = map(analogRead(A0), 10, 1000, 10, 7000);
    usDelay = constrain(usDelay, 10, 7000);

OK, I am a bit confusd, so the answer is what I guess the question is. As usual. You have a pot that when turned is to correspond the values 0.125 to 4.00. Is the relation to be linear?

That would be - as PaulS writes - trivial, and you have done so already: = map(analogRead(A0), 10, 1000, 10, 7000); just put 125,400 as the last two parameters.

The for-loop mentioned by HazardsMind is just a short 20 step test? But it also seems you want the reciprocal of the 0-125-4.00 interval to work out the rate you want to step. And in your code you're ding the opposite, ie pot-value -> map() -> step duration (=rate of turning) -> inverse -> duration to do set number of steps. I do not think spd=usDelay/10; is the right conversion. Certainly the integer division is of no help here, add a decimal point to the "10".

So whe that is done, and we have a value bewteen 0.125 and 4.00, scaled to integer arithmetic - 125 to 400, integerdivde by 60 to get number of minutes and do modulus to get number of seconds.

unless you asked about something else.

if the mapping is non-linear you might have a look at multimap - Arduino Playground - MultiMap -

Msquared - Thanks so much
(where are you getting the 4 from in this 0.125 to 4.00.?
Reason for the .0125 initially was so that the Arduino wasnt checking in the loop on every step, but I am trying to take this out of the loop alltogether so that I can run the stepper for a set number of steps then stop.

If you can simplify the for statement by all means show me how - this is the code I was given and it works perfectly to run the slider at the speeds I need - I have never fully understood why the For statement and "i" is there!
If anyone could break these values down to my dummy level, i'd love it!
10,1000,10,7000 < I gather is remapping the typical 0-1023 on a pot - I think

  for(int i=0; i < 1600 * .0125; i++){
    int usDelay = map(analogRead(A0), 10, 1000, 10, 7000);
    usDelay = constrain(usDelay, 10, 7000);

spd=usDelay/10 was just a test at my end, I dont claim to even have attempted to figure out that equation lol

I wonder if you could replace my values with yours in the code so that I can test? I'd owe you BIG time!

(where are you getting the 4 from in this 0.125 to 4.00.?

I thought I read you wanted something from 5 minutes to four hours. 5 minutes is 0.125 of an hour... oops. That is 0.08333 . Well, it doens invalidate any of the suggestions, just the values.

for : http://arduino.cc/en/Reference/For The "i" is the "counter", even though you do not use it.
map : map() - Arduino Reference

The whole point of DIY, Arduino and so on is that you can try it! What does happen if you put in differnet values? You can watch a stepper turn even if it isnt hooked up to the actual hardware to see if the program makes sense. You can add more Serial.print() to check intermediate values.

Feel free to ask additional questions if the program does something you can not understand at all. (include code, input and output)