Hi all,
** Edit - there's an update to this post at the bottom **
I'm using my Arduino Mega in a CNC machine that I'm building. I'm using buttons to drive the machine manually. I would like to increase the speed of the machine the longer you hold the button.
I've managed to write three functions that set the PWM output slow / med / fast.
Now I want to basically say....
if ( digitalRead(myButton) == HIGH ){
// Start a timer of some sort?
if( timeVal > 5 seconds ){
setSpeedFast();
} else if( timeVal > 2 seconds ){
setSpeedMed();
} else {
setSpeedSlow();
}
}
Is something like this possible?
My other thought was to just have something incrementing into a "Long" while a button was pressed and then just compare the Long value to set different speeds? But, in the interests of learning I wondered if there was a more eloquent solution?
I've checked out the reference but that refers to millis(), micros(), delay() etc which I don't think are suitable here?
Thanks for any help you can give me.
** Update to post **
I've been playing around and I've come up with this solution but it doesn't work for some reason...
void runAxisBasedOnInputs(int directionButtonOne, int directionButtonTwo, int outRelayDirection, int outRelayPulse, int axisDirectionBit){
if( (digitalRead(directionButtonOne) == HIGH) || (digitalRead(directionButtonTwo) == HIGH) ){
incrementSpeedBasedOnDurationButtonPress(myTimingSpeedCount);
digitalWrite(outRelayDirection, LOW); // THESE BITS WORK FINE
digitalWrite(outRelayPulse, LOW); // THESE BITS WORK FINE
} else {
myTimingSpeedCount = 0; // IF I COMMENT THIS OUT IT WORKS? BUT ONCE AT FAST SPEED
// IT NEVER GOES BACK TO SLOW SPEED
digitalWrite(outRelayDirection, HIGH); // THESE BITS WORK FINE
digitalWrite(outRelayPulse, HIGH); // THESE BITS WORK FINE
}
}
void incrementSpeedBasedOnDurationButtonPress(){
myTimingSpeedCount++;
if( myTimingSpeedCount > 50000 ){
setSpeedToFast();
} else if( myTimingSpeedCount > 20000 ){
setSpeedToMed();
} else {
setSpeedToSlow();
}
}
(BTW - "myTimingSpeedCount" is declared as a "long" at the start of the code).
I've tried passing in "myTimingSpeedCount" as a variable into the function etc but to no avail.
It's like the "else" part of the "if( (digitalRead(directionButtonOne) == HIGH) || (digitalRead(directionButtonTwo) == HIGH) )" is being triggered even if the button is held down? Is this normal behaviour?
If anyone can shed any light on this it would be appreciated