I think this is all the code that Bounce is relevant to
#define DEBOUNCE_TIME 500 // Debounce stable ON time in ms
Bounce calibrateButton; // Debounce on calibration button
// Current state of the calibration mode
// 0 - Not calibrating , LEDS OFF
// 1 - Throttle Min Green ON Red OFF
// 2 - Throttle Max Green Blink Red OFF
// 3 - Brake Min Green OFF Red ON
// 4 - Brake Max Green OFF Red Blink
int calibrateState = 0;
void setup()
{
#ifdef _DEBUG_ALL_
// Initialise Serial connections
Serial.begin( SERIAL_SPEED );
Serial.println( "setup() Started" );
#endif
// Read config from EEPROM
readEEPROM();
// Setup the calibration button, enable internal pullup
pinMode( CALIBRATE_PIN , INPUT_PULLUP );
// And the Throttle/Brake Invert DIP Switches
// No need to debounce, static DIP switch
// Enable internal pullup, floating bitch ;-)
pinMode( THROTTLE_INVERT_PIN , INPUT_PULLUP );
pinMode( BRAKE_INVERT_PIN , INPUT_PULLUP );
// After setting up the button, setup Bounce object, using STABLE ON not LOCK-OUT
// Signal needs to be stable for DEBOUNCE_INTERVAL amount of milliseconds
calibrateButton.attach( CALIBRATE_PIN );
calibrateButton.interval( DEBOUNCE_TIME );
// Make sure the LEDS are OFF
pinMode( ledState[RED_LED].pin , OUTPUT );
pinMode( ledState[GREEN_LED].pin , OUTPUT );
setLED( RED_LED , LOW );
setLED( GREEN_LED , LOW );
// Be explicit, not necessary but just in case.
// If set to EXTERNAL will short VCC to AREF on first readAnalog()!!!!!
analogReference( DEFAULT );
// For Adafruit MCP4725A1 the address is 0x62 (default) or 0x63 (ADDR pin tied to VCC)
// For MCP4725A0 the address is 0x60 or 0x61
// For MCP4725A2 the address is 0x64 or 0x65
#ifdef USE_DAC
// Initialise the throttle and brake 12 Bit DAC's
throttleDAC.begin( 0x62 );
brakeDAC.begin( 0x63 );
#endif
// Should have been enough time to let DIP switches settle
// LOW = Invert ON since internal pullups enabled.
// Too lazy to solder extra pull downs ;p
if ( digitalRead( THROTTLE_INVERT_PIN ) == LOW )
{ throttleStart = 4095; throttleEnd = 0; }
else
{ throttleStart = 0; throttleEnd = 4095; }
if ( digitalRead( BRAKE_INVERT_PIN ) == LOW )
{ brakeStart = 4095; brakeEnd = 0; }
else
{ brakeStart = 0; brakeEnd = 4095; }
#ifdef _DEBUG_ALL_
Serial.println( "setup() Finished" );
#endif
}
void loop()
{
unsigned int t;
unsigned int b;
// Check for Calibration first
if ( checkCalibrationState() != 0 ) return;
// Make sure reference not EXTERNAL !!!
// read , constrain and map the value from throttle and brake
t = analogRead( THROTTLE_IN );
b = analogRead( BRAKE_IN );
#ifdef _DEBUG_ALL_
Serial.print( "TMin:" ); Serial.print( config.throttleMin );
Serial.print( " TMax:" ); Serial.print( config.throttleMax );
Serial.print( " BMin:" ); Serial.print( config.brakeMin );
Serial.print( " BMax:" ); Serial.print( config.brakeMax );
Serial.print( " TStart:" ); Serial.print( throttleStart );
Serial.print( " TEnd:" ); Serial.print( throttleEnd );
Serial.print( " BStart:" ); Serial.print( brakeStart );
Serial.print( " BEnd:" ); Serial.print( brakeEnd );
Serial.print( " TOld:" ); Serial.print( t );
Serial.print( " BOld:" ); Serial.print( b );
#endif
// Adjust throttle output, inverting if necessarry
t = constrain( t , config.throttleMin , config.throttleMax );
t = map( t , config.throttleMin , config.throttleMax , throttleStart , throttleEnd );
#ifdef USE_DAC
throttleDAC.setVoltage( t , false );
#endif
// Adjust brake output, inverting if necessarry
b = constrain( b , config.brakeMin , config.brakeMax );
b = map( b , config.brakeMin , config.brakeMax , brakeStart , brakeEnd );
#ifdef USE_DAC
brakeDAC.setVoltage( b , false );
#endif
#ifdef _DEBUG_ALL_
Serial.print( " TNew:" ); Serial.print( t );
Serial.print( " BNew:" ); Serial.println( b );
#endif
}
int checkCalibrationState()
{
// Blink LED's if required
blinkLED( GREEN_LED ); blinkLED( RED_LED );
// Check for Calibration first
boolean stateChanged = calibrateButton.update();
int state = calibrateButton.read();
// Detect the falling edge, with BOUNCE_INTERVAL detection
if ( stateChanged && state == LOW )
{
#ifdef _DEBUG_ALL_
Serial.print( "Calibration Change detected. Before:" );
Serial.print( calibrateState );
Serial.print( " After:" );
#endif
calibrateState = (calibrateState + 1)%5;
switch( calibrateState )
{
case 1: setLED( GREEN_LED , HIGH ); break;
case 2: config.throttleMin = analogRead( THROTTLE_IN ); setLED( GREEN_LED , BLINK ); break;
case 3: config.throttleMax = analogRead( THROTTLE_IN ); setLED( GREEN_LED , LOW ); setLED( RED_LED , HIGH ); break;
case 4: config.brakeMin = analogRead( BRAKE_IN ); setLED( RED_LED , BLINK ); break;
case 0: config.brakeMax = analogRead( BRAKE_IN ); writeEEPROM(); setLED( RED_LED , LOW ); break; // Be explicit and use 0, don't burn out eeprom if error
default: break;
}
#ifdef _DEBUG_ALL_
Serial.println( calibrateState );
#endif
}
return calibrateState;
}