//Torque converter control module v1.2
//60% duty cycle, 30hz, mech. On/Off switch
//Debounce'd switch
//speedPin = master switch (LOW = system disabled, HIGH = system enabled)
int switchPin = 3;
int auxPin = 9;
boolean lastButton = LOW;
int auxLevel = 0; //duty cycle 0-255
boolean currentButton = LOW;
int speedPin = 1;
void setup()
{
TCCR1B = TCCR1B & B11111000 | B00000101; // for PWM frequency of 30.64 Hz
pinMode (switchPin, INPUT);
pinMode (auxPin, OUTPUT);
pinMode (speedPin, INPUT);
}
boolean debounce (boolean last)
{
boolean current = digitalRead(switchPin);
if (last != current)
{
delay (5);
current = digitalRead(switchPin);
}
return current;
}
void loop()
{
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH)
{
auxLevel = auxLevel + 153; //duty cycle set to 60% (starts @ "0" then increases by 153 every button press)
}
lastButton = currentButton;
if (auxLevel > 255) auxLevel = 0; //sets auxLevel back to "0" if 255 is exceeded
analogWrite(auxPin, auxLevel);
if (digitalRead(speedPin) == LOW && switchPin == HIGH) auxLevel = 0; //checks HIGH/LOW of speedPin and switchPin and sets auxLevel = 0 If LOW
}