# Reading senor vs just entering a number

rotSpeed = map(val, 0, 1023, 100, 45000);            //map the value from val to the valuse that I want for the stepper speeds
//
Serial.println(rotSpeed);                           //prints the value for debugging purposes
digitalWrite(dirPin, HIGH);                         //sets the rotation direction

digitalWrite(stepPin, HIGH);                        //next three lines pulse at a given speed to make the stepper turn
delayMicroseconds(rotSpeed);                        //
digitalWrite(stepPin, LOW);                         //

Quote from: Paul_KD7HB on Jan 20, 2018, 11:48 am
The documentation has this warning:

Currently, the largest value that will produce an accurate delay is 16383.

Paul

Why is that?

As part of converting from the input microsceconds to processor cycles, the delayMicroseconds() function multiplies the input value by 4 and the value overflows the max value of unsigned int.

17asleep:
I have previously stated that the schematic is wrong. I’m not actually taking power from the 12 volt rail to power the pot. I simply drew the lines in wrong.

Ok. That's cleared that up. Though I would still recommend at least a 10k pull down resistor on your switch. Reading from a floating input is going to catch you out at some point.

Let's clear up another couple of things:-

val is defined as an int. This is fine as it is used to store the output of the ADC which ranges from 0 to 1023 (well within the range for an int). This is then mapped to unsigned int rotSpeed with a range of 100 to 45000 this fall safely within the acceptable range for an unsigned int (0 - 65535) so no problems there!

I think you could be having problems with large values of delayMicroseconds().

to quote the info page on delayMicroseconds() delayMicroseconds() - Arduino Reference

Currently, the largest value that will produce an accurate delay is 16383. This could change in future Arduino releases. For delays longer than a few thousand microseconds, you should use delay() instead.

For slightly under 2/3rds of your pot rotation you are generating values beyond this limit.

you could replace your delayMicroseconds() call with the following code

if (rotSpeed > 16000) {
rotSpeed = rotSpeed/1000;
delay(rotSpeed);
}
else {
delayMicroseconds(rotSpeed);
}

You will lose a little smoothness in speed change but it should do the trick.

The other thing to consider is to drive the motor controller with a frequency using the tone() function.

I took a shot at using tone instead of what I had before and that fixed everything. The only thinngs I changed were the scale values in the map function and it works flawlessly. Now it bugs me that it didn’t work the way I had it written.