LLC_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 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 turnout class 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 );        } // output    public:   TortoiseTurnout( int pin ) {     turnoutPin = pin;     turnoutState = LOW; // off/low/closed   } // Constructor TortoiseTurnout( int )   void closeSwitch() {     turnoutState = LOW;     output();   } // closeSwitch      void throwSwitch() {     turnoutState = HIGH;     output();   } // throwSwitch      void toggleSwitch() {     if ( turnoutState == HIGH )       turnoutState = LOW;     else       turnoutState = HIGH;     output();   } // toggleSwitch   void setupPin() {     pinMode( turnoutPin, OUTPUT );     pinMode( LED_PIN, OUTPUT );     turnoutState = LOW; // off     output();     _now = millis();   } // setupPin   void update() {     _now = millis();        } // update }; // class TortoiseTurnout ////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////// // one pin driving a relay to reverse the polarity on a Tortoise or likewise turnout class 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 TortoiseTurnout( int )   bool input() {     byte newState = digitalRead( buttonPin );     byte pressed = 0;     if ( newState == LOW ) {       buttonState = HIGH;       pressed = 1;             if ( BUTTON_DEBUG ) {         Serial.print( "button state [");         Serial.print( buttonPin );         Serial.print( "] ");         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( "button state [");       Serial.print( buttonPin );       Serial.print( "] ");       Serial.println( buttonState );     } // if         return pressed == 1;   } // input      void setupPin() {     pinMode( buttonPin, INPUT_PULLUP );     buttonState = HIGH; // off/not pressed     _now = millis();   } // setupPin   void update() {     _now = millis();   } // update }; // class Button ////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////// TortoiseTurnout turn1( TURNOUT_PIN ); Button butt1( BUTTON_PIN_1 ); Button butt2( BUTTON_PIN_2 ); void setup() {   Serial.begin( 115200 );   Serial.println( VERSION );   turn1.setupPin();   butt1.setupPin();   butt2.setupPin();   turn1.toggleSwitch();   delay( 1000 );   turn1.toggleSwitch();   delay( 1000 );   turn1.toggleSwitch();   delay( 1000 );   turn1.toggleSwitch();     delay( 1000 ); } // setup void loop() {   if ( butt1.input() )     turn1.toggleSwitch();   if ( butt2.input() )     turn1.toggleSwitch();  } // loop
//////////////////////////////////// END of CODE ///////////////////////////////////
Comments