Hey guys, thanks for the replies.
@Grumpy_Mike, thanks for the info regarding PWM. As far as the results go, here is a sample around the breakdown area...I've omitted a lot of other data as it is working correctly.
EXPECTED (ms) |
|
ACTUAL (ms) |
. |
|
. |
. |
|
. |
. |
|
. |
1200000 |
|
1166457 |
1500000 |
|
1457923 |
1800000 |
|
208947 |
2100000 |
|
500412 |
2400000 |
|
791878 |
. |
|
. |
. |
|
. |
. |
|
. |
The data from the first three results is not entirely accurate but it's much closer than the last three results.
@kf2qd, I believe you're right about the overflow. If you check out my second post you'll see the Arduino reference entry for delayMicroseconds(). It only provides accurate values for numbers up to 16383. I see in the blink without delay example that they are using unsigned long variables but delayMicroseconds() cannot use values of that magnitude anyway.
I'll attach relevant sections of my code and maybe someone can spot what I'm doing wrong. (I have omitted certain irrelevant sections of my code...just in case you see variables or function calls with no other reference)
#include <Stepper.h>
const int swStart = 1;
const int pinStep = 5;
const int pinDir = 6;
const int pot = A0;
int potValue;
int units = 1;
int EDsleepFlag = 0;
int lockLastState;
long speedValue;
long stepsTaken;
void setup(){
Serial.begin(9600);
pinMode(swStart, INPUT);
pinMode(pinStep, OUTPUT);
pinMode(pinDir, OUTPUT);
pinMode(pot, INPUT);
}
void displaySpeed(){
lcd.setCursor(0,1);
if(units == 1){
lcd.print("seconds ");
if(potValue >= 0 && potValue < 113){lcd.setCursor(0,0); lcd.print("15 "); speedValue = 115;}
if(potValue >= 113 && potValue < 226){lcd.setCursor(0,0); lcd.print("20 "); speedValue = 207;}
if(potValue >= 226 && potValue < 339){lcd.setCursor(0,0); lcd.print("25 "); speedValue = 258;}
if(potValue >= 339 && potValue < 452){lcd.setCursor(0,0); lcd.print("30 "); speedValue = 310;}
if(potValue >= 452 && potValue < 565){lcd.setCursor(0,0); lcd.print("35 "); speedValue = 362;}
if(potValue >= 565 && potValue < 678){lcd.setCursor(0,0); lcd.print("40 "); speedValue = 413;}
if(potValue >= 678 && potValue < 791){lcd.setCursor(0,0); lcd.print("45 "); speedValue = 465;}
if(potValue >= 791 && potValue < 904){lcd.setCursor(0,0); lcd.print("50 "); speedValue = 517;}
if(potValue >= 904 && potValue < 1023){lcd.setCursor(0,0); lcd.print("55 "); speedValue = 568;}
}
if(units == 2){
lcd.print("minutes ");
if(potValue >= 0 && potValue < 68){lcd.setCursor(0,0); lcd.print("1 "); speedValue = 620;}
if(potValue >= 68 && potValue < 136){lcd.setCursor(0,0); lcd.print("2 "); speedValue = 1240;}
if(potValue >= 136 && potValue < 204){lcd.setCursor(0,0); lcd.print("3 "); speedValue = 1860;}
if(potValue >= 204 && potValue < 272){lcd.setCursor(0,0); lcd.print("4 "); speedValue = 2480;}
if(potValue >= 272 && potValue < 340){lcd.setCursor(0,0); lcd.print("5 "); speedValue = 3100;}
if(potValue >= 340 && potValue < 408){lcd.setCursor(0,0); lcd.print("10 "); speedValue = 6200;}
if(potValue >= 408 && potValue < 476){lcd.setCursor(0,0); lcd.print("15 "); speedValue = 9300;}
if(potValue >= 476 && potValue < 544){lcd.setCursor(0,0); lcd.print("20 "); speedValue = 12400;}
if(potValue >= 544 && potValue < 612){lcd.setCursor(0,0); lcd.print("25 "); speedValue = 15500;}
if(potValue >= 612 && potValue < 680){lcd.setCursor(0,0); lcd.print("30 "); speedValue = 18600;}
if(potValue >= 680 && potValue < 748){lcd.setCursor(0,0); lcd.print("35 "); speedValue = 21700;}
if(potValue >= 748 && potValue < 816){lcd.setCursor(0,0); lcd.print("40 "); speedValue = 24800;}
if(potValue >= 816 && potValue < 884){lcd.setCursor(0,0); lcd.print("45 "); speedValue = 27900;}
if(potValue >= 884 && potValue < 952){lcd.setCursor(0,0); lcd.print("50 "); speedValue = 31000;}
if(potValue >= 952 && potValue < 1023){lcd.setCursor(0,0); lcd.print("55 "); speedValue = 34100;}
}
if(units == 3){
lcd.print("hours ");
if(potValue >= 0 && potValue < 102){lcd.setCursor(0,0); lcd.print("1 "); speedValue = 37200;}
if(potValue >= 102 && potValue < 204){lcd.setCursor(0,0); lcd.print("2 "); speedValue = 74400;}
if(potValue >= 204 && potValue < 306){lcd.setCursor(0,0); lcd.print("3 "); speedValue = 111600;}
if(potValue >= 306 && potValue < 408){lcd.setCursor(0,0); lcd.print("4 "); speedValue = 148800;}
if(potValue >= 408 && potValue < 510){lcd.setCursor(0,0); lcd.print("5 "); speedValue = 186000;}
if(potValue >= 510 && potValue < 612){lcd.setCursor(0,0); lcd.print("6 "); speedValue = 223200;}
if(potValue >= 612 && potValue < 714){lcd.setCursor(0,0); lcd.print("9 "); speedValue = 334800;}
if(potValue >= 714 && potValue < 816){lcd.setCursor(0,0); lcd.print("12 "); speedValue = 446400;}
if(potValue >= 816 && potValue < 918){lcd.setCursor(0,0); lcd.print("18 "); speedValue = 669600;}
if(potValue >= 918 && potValue < 1023){lcd.setCursor(0,0); lcd.print("24 "); speedValue = 892800;}
}
}
void stepperMove(){
stepsTaken = 0;
long time1 = millis();
for(long i = 0; i < 46400; i++){
digitalWrite(pinStep, HIGH);
delayMicroseconds(speedValue);
digitalWrite(pinStep, LOW);
delayMicroseconds(speedValue);
stepsTaken++;
}
long time2 = millis();
long timeForStep = (time2 - time1);
Serial.print(timeForStep);
Serial.print(" ms");
Serial.print("\n");
}
void loop(){
potValue = analogRead(pot);
displaySpeed();
startSw.update();
if(startSw.read()){
ledBlinkFive();
stepperMove();
}
}
I'm still not sure why a regular delay won't work in between the digitalWrite() calls in my stepperMove() function.