Here's the code
I think this is right, but the friendly editor keeps trying to make a mess of it.
/********
Throttle 1.1 P Q Elsea May 20, 2013
An Arduino program to control the pulse width at pin 9 with adjustable fade times.
This can be used, with suitable driver electronics, to power leds, lamps or motors
The output frequency is 31250 to avoid throttle noise
there are two control inputs
A0 sets fade time
A1 sets power on duty cycle
there are three contol pins - pull low to activate
pin 2 starts a fade up to target duty cycle
pin 3 starts a fade down to 0
pin 4 is an emergency stop
outputs
pin 9 is pulse width modulated, active high
pin 13 shows running status- active high to work with built-in LED
*/
const int upPin = 2; // the number of the faster pin
const int downPin = 3; // the number of the slower pin
const int stopPin = 4; // the number of the emergency stop pin
const int statusPin = 13; // will light when unit reaches full speed
const int momentumPin = A0; // Analog input pin that the potentiometer is attached to
const int speedPin = A1; // Analog input pin that the potentiometer is attached to
const int outPin = 9; // Analog output pin that the LED is attached to
const int STOPNOW = 0; // poor man's enum
const int RUNNING = 1; // these modes control operation via runMode
const int SPEEDUP = 2; //
const int SLOWDOWN = 3; //
int runMode = 0; // buttons set this to one of the above
unsigned int rate = 0; // contains output duty * 32 for counting
unsigned int target = 0; //contains target output duty * 32 for comparison
int buttonState = 0; //read buttons into here
unsigned int momentum = 64; // value read from momentum pot
unsigned int stepValue = 1; // value added to rate for ramp
int outValue = 0; // value output to the PWM (analog out)
void setup() {
TCCR1B = TCCR1B & 0b11111000
|
pinMode(stopPin, INPUT);
digitalWrite(stopPin,HIGH); // turn on pullup
pinMode(upPin, INPUT);
digitalWrite(upPin,HIGH); // turn on pullup
pinMode(downPin, INPUT);
digitalWrite(downPin,HIGH); // turn on pullup
pinMode(statusPin, OUTPUT);
digitalWrite(statusPin,LOW); // turn off running light
}
void loop() {
//check the buttons
buttonState = digitalRead(stopPin); // emergency stop!
if(buttonState == LOW){ //read buttons when low makes then easy to wire
rate = 0;
analogWrite(outPin, 0);
runMode = STOPNOW;
}
buttonState = digitalRead(upPin); // speed up to target
if(buttonState == LOW){
runMode = SPEEDUP;
}
buttonState = digitalRead(downPin); // slow to stop
if(buttonState == LOW){
runMode = SLOWDOWN;
}
//acquire target speed and momentum from pots
target = analogRead(speedPin)< < 3; // 32 loops per step at max momentum
momentum = analogRead(momentumPin);
stepValue = map(momentum,0,1023,128,1); // adjust map to change momentum range
switch(runMode){
case SLOWDOWN:
if(rate> stepValue) {
rate -= stepValue;
}else{
rate = 0;
runMode = STOPNOW;} // edit 5-20 caught a bug
break;
case SPEEDUP :
if(rate < target) {
rate += stepValue;
}else{
rate = target;
runMode = RUNNING;
}
break;
case RUNNING: // follow change in speed knob
if(rate> target) rate -= stepValue;
else if (rate < target) rate += stepValue;
break;
}
if(runMode == RUNNING) // turn on a light when speed matches target.
digitalWrite(statusPin, HIGH);
else
digitalWrite(statusPin, LOW);
outValue = rate 5; // right shift is 2 more than left shift on target
// make load compensations here
analogWrite(outPin, outValue);
//2mS is the minimum loop time if you want to do analog reads
//loop time is delay mS + 250 uS
delay(2);
}