I've been working on this for a bit, and going in circles, so I'd like to ask you guys for help on something that's out of my knowledge.
I have a large DC motor that I've attached a linkage to. As the motor turns, and the linkage is at its maximum distance it hits a button, button0, and at its minimal distance it hits another button, button1. My code records the amount of time it takes to go from button0 to button1, and from button1 to button0 (they are along a sprocket) and stores it as two independent variables. What I would like to do is use those variables to smooth the speed of my motor as it reaches the maximum and minimal distance for the linkage. i.e. if it takes 3000 milliseconds to go from button0 to button1, I would like the motor to start slow at millisecond 0, ramp up to maximum speed at say 1500 milliseconds, then ramp down as it approaches the 3000 millisecond mark. Thoughts?
Here's my code:
void loop() {
// int potValue = analogRead(analogInPin);
// motorSpeed = map(potValue, 0, 1023, 180, 255);
// analogWrite(motorSpeedPin, motorSpeed);
analogWrite(motorSpeedPin, 252);
//Debounce code for the two buttons
int button0Reading = digitalRead(button0);
int button1Reading = digitalRead(button1);
int section0;
int section1;
if( (millis() - lastDebounceTime0) > debounceDelay0){
if(button0Reading == LOW){
lastDebounceTime0 = millis();
Serial.print("button0 activated | ");
reading0 = millis();
//time from button1 to button0
section0 = (reading0 - reading1);
Serial.print("Time from button1 to button0 (section0) = ");
Serial.print(section0);
Serial.print(" | Motor speed = ");
Serial.println(motorSpeed);
isSection0 = true;
}
}
if( (millis() - lastDebounceTime1) > debounceDelay1){
if(button1Reading == LOW){
lastDebounceTime1 = millis();
Serial.print("button1 activated | ");
reading1 = millis();
//time from button0 to button1
section1 = (reading1 - reading0);
Serial.print("Time from button0 to button1 (section1) = ");
Serial.print(section1);
Serial.print(" | Motor speed = ");
Serial.println(motorSpeed);
isSection0 = false;
}
}
}