I have a sketch to act as a "cruise control" for my live steam locomotive. I made a change so the servo movements will be proportional to the speed, e.g. slow speed, slow servo movements. This has caused a problem.
Everything was fine when using a constant number for the amount to move the servo, e.g. servopos+=5. Changing away from that has caused problems. Variables cruiseFactor and servoMove are int variables. The compound minus seems to work OK; compound plus does not. unAuxin is basically the speed from wheel sensor. The 2nd and 3rd "If" statements are similar because i was trying different amounts of servo movement depending on the speed. Same with the 4th and 5th "Ifs". Initially i tried it w/ "cruiseFactor/10" in place of servoMove. Didn't work that way so i thought i'd try a variable. Right now at different time the servopos will go very large , above 2700 if i remember, which is outside of my parameters, and "locks up" the program.
I bet it's something simple i'm missing. Any help would be appreciated.
Marty
if (cruiseButton)//if cruiseButton is true then run CC sequence
{
cruiseFactor = map(unThrottleIn, 1130, 1900, 1, 100);
servoMove = (cruiseFactor/10);
if ((unAuxIn > (996/cruiseFactor)) && (servopos > 956)) //if speed very slow, open throttle fast.
{
servopos -= servoMove; //
servoAux.writeMicroseconds(servopos); // sets the servo position according to the scaled value
}
if ((unAuxIn > (804/cruiseFactor)) && (unAuxIn <= (996/cruiseFactor)) && (servopos > 956)) //if speed slow, open throttle.
{
// servoMove = (cruiseFactor/10);
servopos -= servoMove;
servoAux.writeMicroseconds(servopos); //
}
if ((unAuxIn < (570/cruiseFactor)) && (unAuxIn >= (502/cruiseFactor)) && (servopos < 1472)) //if speed high, close throttle.
{
//servoMove = (cruiseFactor/10);
servopos += servoMove;
servoAux.writeMicroseconds(servopos); //
}
if ((unAuxIn < (502/cruiseFactor)) && (servopos < 1472)) // If speed very fast, close throttle quicker
{
//servoMove = (cruiseFactor/10);
servopos += servoMove; //
servoAux.writeMicroseconds(servopos); //
}
Serial.print(unAuxIn);
Serial.print(" ");
Serial.print(servopos);
Serial.print(" ");
Serial.print(servoMove);
Serial.print(" ");
Serial.println(cruiseFactor);
delay(50); // measuring dwell time