#include <ClickButton.h>
#include <TinyDebugKnockBang.h>
#include <UserTimer.h>
const int upPin = PB1; // ATtiny45 Pin 6
const int downPin = PB2; //ATtiny45 Pin 7
const int throttleOut = PB0; //ATtiny45 Pin 5
const int brake = PB3; //ATtiny45 Pin 2
int PWMThrottle = 0;
const int throttleTop = 218; //allows for voltage drop accross 500ohm resistor in voltage divider 3.5V finish Assuming 4.32V supply voltage
const int throttleBottom = 74;// allows for voltage drop accross 500ohm resistor in voltage divider 1.3V Start (Try 1.4 if needs extra press) Assuming 4.32V supply voltage
int throttle = 0;
int upButtonState = HIGH;
int downButtonState = HIGH;
ClickButton debounceUp(upPin, LOW, CLICKBTN_PULLUP);
ClickButton debounceDown(downPin, LOW, CLICKBTN_PULLUP);
int upButtonfunction = 0;
int downButtonfunction = 0;
int throttlemaps[20] = {74,82,90,97,105,112,120,127,135,142,150,157,165,173,180,188,195,203,210,218};
void setup()
{
//Turn off Analogue comparitor
ACSR |= _BV(ACD); //disable the analog comparator
ADCSRA &= ~_BV(ADEN); //disable ADC
//Set PWM Frequency, Only Use PB0 or PB1
UserTimer_SetToPowerup();
UserTimer_SetWaveformGenerationMode( UserTimer_(Fast_PWM_FF) );
UserTimer_ClockSelect( UserTimer_(Prescale_Value_1) );
// Frequency is F_CPU / (1 * (0xFF+1))
// (8 megahertz) / 256*8 = 3.9 31.25 kilohertz
pinMode(upPin, INPUT);
digitalWrite(upPin, HIGH);
pinMode(PB4, INPUT);
digitalWrite(PB4, HIGH);
pinMode(PB5, INPUT);
digitalWrite(PB5, HIGH);
pinMode(downPin, INPUT);
digitalWrite(downPin, HIGH);
pinMode(throttleOut,OUTPUT);
pinMode(brake,INPUT);
digitalWrite(brake, HIGH);
//setup buttons
debounceUp.maxPresses = 20; // max button multiclick count
debounceUp.debounceTime = 50; // Debounce timer in ms
debounceUp.multiclickTime = 250; // Time limit for multi clicks
debounceUp.heldDownTime = 500; // time until "held-down clicks" register
//setup buttons
debounceDown.maxPresses = 20; // max button multiclick count
debounceDown.debounceTime = 50; // Debounce timer in ms
debounceDown.multiclickTime = 250; // Time limit for multi clicks
debounceDown.heldDownTime = 500; // time until "held-down clicks" register
}
void loop()
{
if(digitalRead(brake)==LOW){
if(PWMThrottle == 0 && throttle==0){
}
else{
PWMThrottle = 0;
throttle=0;
analogWrite(throttleOut,PWMThrottle);
}
}
else{
// Update the debouncer
debounceUp.Update();
debounceDown.Update();
if (debounceUp.click != 0) upButtonfunction = debounceUp.click;
if (debounceDown.click != 0) downButtonfunction = debounceDown.click;
//Perform Actions based on button press
if(debounceUp.click == 1) Action(1);
if(debounceDown.click == 1) Action(2);
//Perform Actions based on button hold
if(upButtonfunction < -1 && debounceUp.depressed == true) Action(1);
if(downButtonfunction < -1 && debounceDown.depressed == true) Action(2);
}
}
void Action(int button)
{
if(button == 1)
{
if(throttle<20){
throttle = throttle + 1;
for(int g=0;g<4;g++){
PWMThrottle = (throttlemaps[throttle]-8)+((g+1)*2);
analogWrite(throttleOut,PWMThrottle);
delay(60);
}
}
else{
PWMThrottle = throttleTop;
analogWrite(throttleOut,PWMThrottle);
}
}
if(button == 2){
if(throttle==0){
PWMThrottle = 0;
analogWrite(throttleOut,0);
}
else{
throttle = throttle - 1;
PWMThrottle = throttlemaps[throttle];
analogWrite(throttleOut,PWMThrottle);
delay(500);
}
}
}