1313 * See the GIT changelog for more details.
1414 */
1515
16+ #include < avr/wdt.h>
17+
1618// Pin configurations and other global variables.
1719#include " config.h"
1820
@@ -37,10 +39,25 @@ char trial_state_[5] = "PRE_";
3739int incoming_byte_ = 0 ;
3840bool reboot_ = false ;
3941
42+ ISR (WDT_vect)
43+ {
44+ // Handle interuppts here.
45+ // Nothing to handle.
46+ }
47+
48+ /* --------------------------------------------------------------------------*/
49+ /* *
50+ * @Synopsis When ctrl+c is pressed, reboot_ is set true and we restart the
51+ * arduino else ctrl+c will not stop arduino from giving stimulus.
52+ */
53+ /* ----------------------------------------------------------------------------*/
4054void reset_watchdog ( )
4155{
56+ if ( not reboot_ )
57+ wdt_reset ( );
4258}
4359
60+
4461/* *
4562 * @brief Read a command from command line. Consume when character is matched.
4663 *
@@ -65,14 +82,25 @@ bool is_command_read( char command, bool consume = true )
6582 return false ;
6683}
6784
85+ void check_for_reset ( void )
86+ {
87+ if ( is_command_read (' r' , true ) )
88+ {
89+ Serial.println ( " >>> Received r. Reboot in 2 seconds" );
90+ reboot_ = true ;
91+ }
92+ }
93+
94+
6895/* *
6996 * @brief Write data line to Serial port in csv format.
7097 * @param data
7198 * @param timestamp
7299 */
73100void write_data_line ( )
74101{
75- reset_watchdog ( );
102+ check_for_reset ();
103+ reset_watchdog ();
76104
77105 // Just read the registers where pin data is saved.
78106 int puff = digitalRead ( PUFF_PIN );
@@ -100,15 +128,6 @@ void write_data_line( )
100128 delay ( 3 );
101129}
102130
103- void check_for_reset ( void )
104- {
105- if ( is_command_read (' r' , true ) )
106- {
107- Serial.println ( " >>>Received r. Reboot in 2 seconds" );
108- reboot_ = true ;
109- }
110- }
111-
112131/* *
113132 * @brief Play tone for given period and duty cycle.
114133 *
@@ -120,7 +139,7 @@ void check_for_reset( void )
120139 */
121140void play_tone ( unsigned long period, double duty_cycle = 0.5 )
122141{
123- // reset_watchdog( );
142+ reset_watchdog ( );
124143 check_for_reset ( );
125144 unsigned long toneStart = millis ();
126145 while ( (millis () - toneStart) <= period )
@@ -228,6 +247,11 @@ void wait_for_start( )
228247 Serial.println ( " >>>Received 1. Start capturing frame" );
229248 digitalWrite ( CAMERA_TTL_PIN, HIGH );
230249 }
250+ else if ( is_command_read ( ' r' , true ) )
251+ {
252+ Serial.println ( " >>>Received r. Rebooting " );
253+ reboot_ = true ;
254+ }
231255 else if ( is_command_read ( ' 2' , true ) )
232256 {
233257 Serial.println ( " >>>Received 2. Stop capturing frames" );
@@ -263,6 +287,9 @@ void setup()
263287 // chars per ms i.e. baud rate should be higher than 100,000.
264288 Serial.begin ( 115200 );
265289
290+ // esetup watchdog. If not reset in 2 seconds, it reboots the system.
291+ wdt_enable ( WDTO_2S );
292+ wdt_reset ();
266293 // Random seed.
267294 randomSeed ( analogRead (A5) );
268295
@@ -363,7 +390,7 @@ void monitor_for_shock(void)
363390 */
364391void do_trial ( size_t trial_num, bool isprobe = false )
365392{
366- // reset_watchdog( );
393+ reset_watchdog ( );
367394 check_for_reset ( );
368395
369396 print_trial_info ( );
@@ -526,7 +553,7 @@ void do_trial( size_t trial_num, bool isprobe = false )
526553 sprintf (trial_state_, " ITI_" );
527554 while ((millis () - stamp_) <= rduration)
528555 {
529- reset_watchdog ( );
556+ reset_watchdog ();
530557 write_data_line ();
531558 delay (200 );
532559 }
@@ -542,9 +569,10 @@ void loop()
542569
543570 for (size_t i = 1 ; i <= PROTO_NumTrialsInABlock; i++)
544571 {
545-
546- reset_watchdog ( );
572+ check_for_reset ();
573+ reset_watchdog ();
547574 trial_count_ = i;
575+
548576 if (String (" TONE/LIGHT" ) == String (PROTO_CSValue))
549577 {
550578 // FOR Shomu. Mixed trials.
0 commit comments