@@ -19,6 +19,7 @@ void handleBroadcastMessage(BroadcastData& bd, uintptr_t data);
1919void handleAntError (uint8_t code, uintptr_t data);
2020void handleStartUpMessage (StartUpMessage& sum, uintptr_t data);
2121void handleChannelEventResponse (ChannelEventResponse& cer, uintptr_t data);
22+ bool itsAlive (StartUpMessage& sm, uintptr_t data);
2223
2324void setup ()
2425{
@@ -29,6 +30,7 @@ void setup()
2930 ChannelPeriod cp;
3031 ChannelRfFrequency crf;
3132 OpenChannel oc;
33+ StartUpMessage sm;
3234
3335 Serial1.begin (BAUD_RATE);
3436 ant.onBroadcastData (handleBroadcastMessage);
@@ -37,24 +39,39 @@ void setup()
3739 ant.onChannelEventResponse (handleChannelEventResponse);
3840 ant.onOtherResponse (handleMessageWithNoCallback);
3941 ant.begin (Serial1);
40- ant.send (rs);
41- // Delay after resetting the radio to give the user time to connect on serial
42- delay (10000 );
42+
4343 Serial.begin (9600 );
44+ delay (10000 );
45+ // Delay to give the user time to connect on serial
46+
4447 Serial.println (" Running" );
4548
49+ ant.send (rs);
50+ ant.waitFor (sm, 2000 , itsAlive); // wait 2s for device to start
51+
4652 snk = SetNetworkKey ();
4753 snk.setNetwork (0 );
4854 snk.setKey ((uint8_t *)NETWORK_KEY);
4955 ant.send (snk);
50- ant.loop ();
56+ if (ant.waitForStatus (snk.getMsgId (), 1000 )) {
57+ Serial.println (" No Response for setting network key" );
58+ } else {
59+ ChannelEventResponse cer = ChannelEventResponse ();
60+ ant.getResponse ().getChannelEventResponseMsg (cer);
61+ handleChannelEventResponse (cer, 0 );
62+ }
5163
5264 ac = AssignChannel ();
5365 ac.setChannel (0 );
5466 ac.setChannelType (CHANNEL_TYPE_BIDIRECTIONAL_RECEIVE); // can't wildcard this
5567 ac.setChannelNetwork (0 );
56- ant.send (ac);
57- ant.loop ();
68+ if (ant.sendAndWait (ac, 2000 )) {
69+ Serial.println (" No Response for assigning channel" );
70+ } else {
71+ ChannelEventResponse cer = ChannelEventResponse ();
72+ ant.getResponse ().getChannelEventResponseMsg (cer);
73+ handleChannelEventResponse (cer, 0 );
74+ }
5875
5976 ci = ChannelId ();
6077 ci.setChannel (0 );
@@ -145,3 +162,8 @@ void handleChannelEventResponse(ChannelEventResponse& cer, uintptr_t data) {
145162 break ;
146163 }
147164}
165+
166+ bool itsAlive (StartUpMessage& sm, uintptr_t data) {
167+ Serial.println (" Radio Reset!" );
168+ return true ;
169+ }
0 commit comments