/* * auto_rev_cds.txt * * 2015.5.21 suzu * */ #include #include #include "DSGatewayLib.h" #define ON 1 #define OFF 0 #define STOP_TIME 3 // station stop second #define TRAIN_A 12 // ***Loco Address A #define SPEED_1 200 // First speed 1-1024 #define SPEED_2 300 // Second speed 1-1024 #define CDS_A 0 // CDS analog port# #define CDS_B 1 // CDS analog port# #define LIGHT_LEVEL 200 // ***CDS Light Level DSGatewayLib Gateway; int speed; word LocAddrA = ADDR_DCC + TRAIN_A; unsigned char stop_CDS; // stop point = CDS_A or CDS_B unsigned char Forward_A = 1; // TRAIN_A ininitial go 1=forward, 2=backward // 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 ); } // ******************* setup *********************** void setup() { Serial.begin( 9600 ); 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 ); Serial.println( "Light On" ); light( LocAddrA, ON ); delay ( 1000 ); Serial.println( "Function 2" ); func( LocAddrA, 2, 2 ); // Whistle // First Speed up Serial.println( "First Speed up" ); speed = SPEED_1; Serial.println( speed ); Gateway.SetLocoSpeed( LocAddrA, speed ); delay( 1000 ); speed = SPEED_2; Serial.println( speed ); Gateway.SetLocoSpeed( LocAddrA, speed ); // Continue Serial.println( "Continue" ); func( LocAddrA, 3, 1 ); // Short Whistle while ( analogRead( CDS_A ) > LIGHT_LEVEL && analogRead( CDS_B ) > LIGHT_LEVEL ) { ; // do nothing } // Catch target CDS Serial.println( "Catch target CDS" ); if ( analogRead( CDS_A ) <= LIGHT_LEVEL ) stop_CDS = CDS_B; else stop_CDS = CDS_A; Serial.print( "next stop_CDS = " ); Serial.print( stop_CDS ); Serial.println( "" ); // Speed down Serial.println("First Speed down"); func( LocAddrA, 1, 2 ); // Bell // STOP Serial.println("First STOP"); Gateway.SetLocoSpeed( LocAddrA, 0 ); light( LocAddrA, OFF ); delay ( STOP_TIME * 1000 ); // Reverse Serial.println( "First Reverse" ); Serial.print( "Forward_A = " ); if ( Forward_A == 1 ) Forward_A = 2; else Forward_A = 1; Serial.println( Forward_A ); Gateway.SetLocoDirection( LocAddrA, Forward_A ); } // ******************* loop *********************** void loop() { Serial.println("loop"); light( LocAddrA, ON ); func( LocAddrA, 4, 1 ); // Steam Release func( LocAddrA, 2, 2 ); // Whistle // Speed up Serial.println("Speed up"); speed = SPEED_1; Serial.println( speed ); Gateway.SetLocoSpeed( LocAddrA, speed ); delay( 2000 ); speed = SPEED_2; Serial.println( speed ); Gateway.SetLocoSpeed( LocAddrA, speed ); // Continue Serial.println( "Continue" ); func( LocAddrA, 3, 1 ); // Short Whistle while ( analogRead( stop_CDS ) > LIGHT_LEVEL ) { ; // do nothing } // Speed down Serial.println( "Speed down" ); func( LocAddrA, 1, 1 ); // Bell // STOP Serial.println( "STOP" ); Gateway.SetLocoSpeed( LocAddrA, 0 ); func( LocAddrA, 1, 2 ); // Bell light( LocAddrA, OFF ); delay ( STOP_TIME * 1000 ); // Reverse Serial.println( "Reverse" ); Serial.print( "Forward_A = " ); if ( Forward_A == 1 ) Forward_A = 2; else Forward_A = 1; Serial.println( Forward_A ); Gateway.SetLocoDirection( LocAddrA, Forward_A ); // Set next CDS if ( stop_CDS == CDS_A ) stop_CDS = CDS_B; else stop_CDS = CDS_A; Serial.print( "Next CDS = " ); Serial.println( stop_CDS ); }