/* * auto_rev_intrupt.txt * * 2015.5.22 suzu * */ #include #include #include "DSGatewayLib.h" #define ON 1 #define OFF 0 #define LOCO_ADDRESS 12 // ***Loco Address A #define STOP_TIME 2 // interrupt stop second #define MIN_SPEED 150 // 1-1024 #define MAX_SPEED 250 // 1-1024 DSGatewayLib Gateway; int speed; word LocAddrA = ADDR_DCC + LOCO_ADDRESS; unsigned char Forward_A = 1; // TRAIN_A ininitial go 1=forward, 2=backward volatile int mode = 0; // define functions void func( word address, unsigned char function, int times ) { Gateway.SetLocoFunction( address, function, ON ); delay( times * 1000 ); Gateway.SetLocoFunction( address, function, OFF ); } void light( word address, unsigned char on_off ) { Gateway.SetLocoFunction( address, 0, on_off ); } void interrupt_A() { mode = 2; } void interrupt_B() { mode = 3; } // ******************* setup *********************** void setup() { Serial.begin( 9600 ); pinMode(2, INPUT); // ***Digital interrupt0 pin# pinMode(3, INPUT); // ***Digital interrupt1 pin# Gateway.begin(); delay( 5000 ); Gateway.SetPower(true); delay( 1000 ); Serial.println( "Function initialize" ); Gateway.SetLocoSpeed( LocAddrA, 0 ); Gateway.SetLocoDirection( LocAddrA, Forward_A ); light( LocAddrA, OFF ); // Gateway.SetLocoFunction( LocAddrA, 8, ON ); // Mute } // ******************* loop *********************** void loop() { Serial.println( "" ); Serial.println( "loop" ); light( LocAddrA, ON ); func( LocAddrA, 2, 1 ); // Whistle // Speed up Serial.println( "Speed up" ); speed = MIN_SPEED; Serial.println( speed ); Gateway.SetLocoSpeed( LocAddrA, speed ); delay( 1000 ); speed = MAX_SPEED; Serial.println( speed ); Gateway.SetLocoSpeed( LocAddrA, speed ); func( LocAddrA, 3, 1 ); // Short Whistle attachInterrupt( 0, interrupt_A, RISING ); attachInterrupt( 1, interrupt_B, RISING ); mode = 0; Serial.print( "mode 1 = " ); Serial.println( mode ); // Continue while( mode == 0 ) { ; // do nothing // Serial.println( "Continue" ); } detachInterrupt( 0 ); detachInterrupt( 1 ); // stop Gateway.SetLocoSpeed( LocAddrA, 0 ); func( LocAddrA, 1, 1 ); // Bell Serial.print( "mode = " ); Serial.println( mode ); delay( STOP_TIME * 1000 ); // Reverse if ( Forward_A == 1 ) Forward_A = 2; else Forward_A = 1; Serial.print( "Forward_A = " ); Serial.println( Forward_A ); Gateway.SetLocoDirection( LocAddrA, Forward_A ); }