Skip to main content

Arduino Yard Control Sketch

By May 13, 2024Electrical2 min read

May 13, 2024

“Speed” Muller came over and helped troubleshoot the Arduino sketch.  Note that the pushbuttons have been moved from the frog to the track on the diagram.  I have completed the two new panels and they are ready to install.  Here is the sketch which includes a startup test:

#define VERSION_STR “Gary Left Yard 0.02” /* * Author: Speed Muller */ #include “Heartbeat.h” #define NUMBUTTONS 8 #define NUMLEDS NUMBUTTONS #define NUMRELAYS 7 void setTrack( uint8_t track ); Heartbeat myHeart = Heartbeat( LED_BUILTIN, 100, 900 ); uint8_t u8Buttons[ NUMBUTTONS ] = { A1, A2, A3, A4, A5, A6, A7, A8 }; uint8_t u8Relays[ NUMRELAYS ] = { 31, 32, 33, 34, 35, 36, 37 }; uint8_t u8Leds[ NUMLEDS ] = { 41, 42, 43, 44, 45, 46, 47, 48 }; // The first entry in the matrix tells how many relays to set for that track. Then the relays follows with 0 for off and 1 for on. // All relays from relay 0 is set to off or on up to the last relay specified uint8_t u8Matrix[NUMBUTTONS][NUMRELAYS+1] = { { 1, 0 }, { 2, 1, 0 }, { 3, 1, 1, 0 }, { 4, 1, 1, 1, 0 }, { 5, 1, 1, 1, 1, 0 }, { 6, 1, 1, 1, 1, 1, 0 }, { 7, 1, 1, 1, 1, 1, 1, 0 }, { 7, 1, 1, 1, 1, 1, 1, 1 } }; void setup( ) { Serial.begin( 115200 ); Serial.println( ); myHeart.begin( ); Serial.println( VERSION_STR ); for( uint8_t i=0; i<NUMBUTTONS; ++i ) { // since NUMBUTTONS == NUMLEDS pinMode( u8Buttons[i], INPUT_PULLUP ); pinMode( u8Leds[i], OUTPUT ); digitalWrite( u8Leds[i], HIGH ); } // for for( uint8_t i=0; i<NUMRELAYS; ++i ) { pinMode( u8Relays[i], OUTPUT ); Serial.print( i ); Serial.print( “:r:” );Serial.println( u8Relays[i] ); digitalWrite( u8Relays[i], LOW ); delay( 100 ); digitalWrite( u8Relays[i], HIGH ); delay( 100 ); } // for // // sequence for show // for( uint8_t i=0; i<NUMLEDS; ++i ) { // delay( 50 ); // digitalWrite( LED_BUILTIN, LOW ); // Serial.println( “toggle ” ); // delay( 50 ); // digitalWrite( LED_BUILTIN, HIGH ); // } // for // sequence for show for( uint8_t i=0; i<NUMLEDS; ++i ) { delay( 100 ); digitalWrite( u8Leds[i], LOW ); delay( 100 ); digitalWrite( u8Leds[i], HIGH ); } // for setTrack( 0 ); } // void setup( ) /* * Set the relays in the matrix to get to the selected track * And then turn the LED on for that track */ void setTrack( uint8_t track ) { uint8_t u8RelaysToSet = u8Matrix[ track ][0]; Serial.print( track, DEC ); Serial.print( “->” ); Serial.print( u8RelaysToSet, DEC ); Serial.print( ” ” ); if( track < NUMBUTTONS ) { for( uint8_t i=0; i<u8RelaysToSet; ++i ) { Serial.print( “[” ); if( u8Matrix[ track ][i+1]==1 ) { Serial.print( u8Relays[i] ); Serial.print( “,” ); Serial.print( u8Matrix[ track ][i+1] ); digitalWrite( u8Relays[i], LOW ); delay( 200 ); } else { Serial.print( u8Relays[i] ); Serial.print( “,” ); Serial.print( u8Matrix[ track ][i+1] ); digitalWrite( u8Relays[i], HIGH ); delay( 200 ); } // if … (48 lines left

Leave a Reply