LCC_Extort

Beginning of an LCC Arduino Node to control a turnout that might be thrown with a Tortoise switch machine:


// We are going to start off with a system that will toggle a relay (or H-bridge) // from two separate buttons or inputs. Only because we need this circuit a little sooner, like tomorrow!
#define VERSION "TortoiseTurnoutWith2Buttons 0.01, pre CAN, 2016.08.12;23:44" #define BAUD 115200#define TURNOUT_PIN 3
// LED_PIN, until CAN takes MOSI and MISO #define LED_PIN 13 #define BUTTON_PIN_1 A0 #define BUTTON_PIN_2 A1
// one pin driving a relay to reverse the polarity on a Tortoise or likewise turnoutclass TortoiseTurnout { const bool TURNOUT_DEBUG = true; byte turnoutPin; byte turnoutState; // 0 off/low/closed, 1 on/high/thrown long _now;
void output( ) { if ( TURNOUT_DEBUG ) { Serial.print( "turnout state "); Serial.println( turnoutState ); } // if digitalWrite( turnoutPin, turnoutState ); digitalWrite( LED_PIN, turnoutState ); } // void output
public: TortoiseTurnout( int pin ) { turnoutPin = pin; turnoutState = LOW; // off/low/closed } // Constructor TortoiseTurnout( int )
void closeSwitch( ) { turnoutState = LOW; output(); } // void closeSwitch( ) void throwSwitch( ) { turnoutState = HIGH; output( ); } // void throwSwitch( )
void toggleSwitch( ) { if ( turnoutState == HIGH ) turnoutState = LOW; else turnoutState = HIGH; output( ); } // void toggleSwitch( )
void setupPin( ) { pinMode( turnoutPin, OUTPUT ); pinMode( LED_PIN, OUTPUT ); turnoutState = LOW; // off output( ); _now = millis( ); } // void setupPin( )
void update( ) { _now = millis( ); } // void update( )}; // class TortoiseTurnout

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// one pin driving a relay to reverse the polarity on a Tortoise or likewise turnoutclass Button { const bool BUTTON_DEBUG = true; byte buttonPin; byte buttonState; // 0 off/low, 1 on/high/pressed long _now;
public: Button( int pin ) { buttonPin = pin; buttonState = LOW; // off/low/closed } // Constructor Button( int )
bool input( ) { byte newState = digitalRead( buttonPin ); byte pressed = 0; if ( newState == LOW ) { buttonState = HIGH; pressed = 1;
if ( BUTTON_DEBUG ) { Serial.print( F( "button state [" ) ); Serial.print( buttonPin ); Serial.print( F( "] " ) ); Serial.println( buttonState ); } // if BUTTON_DEBUG } else { return pressed == 1; } // if newState == LOW
while ( newState == LOW ) { delay( 100 ); // blocking here, debounce for now newState = digitalRead( buttonPin ); } // while
buttonState = LOW; if ( BUTTON_DEBUG ) { Serial.print( F( "button state [" ) ); Serial.print( buttonPin ); Serial.print( F( "] " ) ); Serial.println( buttonState ); } // if
return pressed == 1; } // bool input( )
void setupPin( ) { pinMode( buttonPin, INPUT_PULLUP ); buttonState = HIGH; // off/not pressed _now = millis( ); } // setupPin
void update( ) { _now = millis( ); } // void update( )}; // class Button
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TortoiseTurnout turn1( TURNOUT1_PIN );TortoiseTurnout turn2( TURNOUT2_PIN );
Button butt1( BUTTON_PIN_1 );Button butt2( BUTTON_PIN_2 );
void setup() { Serial.begin( BAUD ); Serial.println( VERSION );
turn1.setupPin(); turn2.setupPin();
butt1.setupPin(); butt2.setupPin();
turn1.toggleSwitch(); delay( 1000 );
turn1.toggleSwitch(); delay( 1000 );
turn2.toggleSwitch(); delay( 1000 );
turn2.toggleSwitch(); delay( 1000 );} // setup
void loop() { if ( butt1.input() ) turn1.toggleSwitch();
if ( butt2.input() ) turn2.toggleSwitch(); } // loop
///////////////////////////////////// END of CODE ////////////////////////////////////