@@ -58,7 +58,8 @@ void FED3::run() {
5858 DateTime now = rtc.now ();
5959 currentHour = now.hour (); // useful for timed feeding sessions
6060 currentMinute = now.minute (); // useful for timed feeding sessions
61- unixtime = now.unixtime ();
61+ currentSecond = now.second (); // useful for timed feeding sessions
62+ unixtime = now.unixtime ();
6263 ReadBatteryLevel ();
6364 UpdateDisplay ();
6465 goToSleep ();
@@ -147,11 +148,13 @@ void FED3::Feed() {
147148 pelletDispensed = RotateDisk (-300 );
148149 }
149150
150- pixelsOff ();
151-
151+ if (EnableSleep==true ){
152+ pixelsOff ();
153+ }
154+
152155 // If pellet is detected during or after this motion
153156 if (pelletDispensed == true ) {
154- digitalWrite (MOTOR_ENABLE, LOW); // Disable motor driver and neopixel
157+ ReleaseMotor ();
155158 pelletTime = millis ();
156159
157160 display.fillCircle (25 , 99 , 5 , BLACK);
@@ -213,7 +216,7 @@ void FED3::Feed() {
213216 }
214217 }
215218
216- digitalWrite (MOTOR_ENABLE, LOW); // Disable motor driver and neopixel
219+ ReleaseMotor ();
217220 PelletCount++;
218221 Left = false ;
219222 Right = false ;
@@ -356,18 +359,49 @@ bool FED3::dispenseTimer_ms(int ms) {
356359
357360// Timeout function
358361void FED3::Timeout (int seconds) {
359- for (int k = 0 ; k <= seconds; k++) {
360- delay (1000 );
361- display.fillRect (5 , 20 , 200 , 25 ,WHITE); // erase the data on screen without clearing the entire screen by pasting a white box over it
362+ DateTime now = rtc.now ();
363+ unixtime = now.unixtime ();
364+ unsigned long TimeoutStart = now.unixtime ();
365+ while (unixtime - TimeoutStart < seconds) {
366+ // Log pokes while pellet is present
367+ if (digitalRead (LEFT_POKE) == LOW) { // If left poke is triggered
368+ leftPokeTime = millis ();
369+ LeftCount ++;
370+ leftInterval = 0.0 ;
371+ while (digitalRead (LEFT_POKE) == LOW) {} // Hang here until poke is clear
372+ leftInterval = (millis () - leftPokeTime);
373+ Event = " LeftinTimeout" ;
374+ logdata ();
375+ }
376+
377+ if (digitalRead (RIGHT_POKE) == LOW) { // If right poke is triggered
378+ rightPokeTime = millis ();
379+ RightCount ++;
380+ rightInterval = 0.0 ;
381+ while (digitalRead (RIGHT_POKE) == LOW) {} // Hang here until poke is clear
382+ rightInterval = (millis () - rightPokeTime);
383+ Event = " RightinTimeout" ;
384+ logdata ();
385+ }
386+
387+ DateTime now = rtc.now ();
388+ unixtime = now.unixtime ();
389+
390+ delay (10 );
391+
392+ if (unixtime - displayupdate >= 1 ){
393+ UpdateDisplay ();
394+ display.fillRect (5 , 20 , 200 , 25 , WHITE); // erase the data on screen without clearing the entire screen by pasting a white box over it
362395 display.setCursor (6 , 36 );
363396 display.print (" Timeout: " );
364- display.print (seconds - k );
397+ display.print (int ( floor ( seconds - (unixtime - TimeoutStart))) );
365398 display.refresh ();
399+ displayupdate = now.unixtime ();
366400 }
367- display.fillRect (5 , 20 , 100 , 25 , WHITE); // erase the data on screen without clearing the entire screen by pasting a white box over it
368- UpdateDisplay ();
401+
369402 Left = false ;
370403 Right = false ;
404+ }
371405}
372406
373407/* *************************************************************************************************************************************************
@@ -914,7 +948,9 @@ void FED3::writeConfigFile() {
914948
915949// Write to SD card
916950void FED3::logdata () {
917- digitalWrite (MOTOR_ENABLE, LOW); // Disable motor driver and neopixel
951+ if (EnableSleep==true ){
952+ digitalWrite (MOTOR_ENABLE, LOW); // Disable motor driver and neopixel
953+ }
918954 SD.begin (cardSelect, SD_SCK_MHZ (4 ));
919955
920956 // fix filename (the .CSV extension can become corrupted) and open file
@@ -1085,11 +1121,11 @@ void FED3::logdata() {
10851121 logfile.println (sqrt (-1 )); // print NaN
10861122 }
10871123
1088- else if ((Event == " Left" ) or (Event == " LeftShort" ) or (Event == " LeftWithPellet" )) { //
1124+ else if ((Event == " Left" ) or (Event == " LeftShort" ) or (Event == " LeftWithPellet" ) or (Event == " LeftinTimeout " ) ) { //
10891125 logfile.println (leftInterval/1000.000 ); // print left poke timing
10901126 }
10911127
1092- else if ((Event == " Right" ) or (Event == " RightShort" ) or (Event == " RightWithPellet" )) { //
1128+ else if ((Event == " Right" ) or (Event == " RightShort" ) or (Event == " RightWithPellet" ) or (Event == " RightinTimeout " ) ) { //
10931129 logfile.println (rightInterval/1000.000 ); // print left poke timing
10941130 }
10951131
@@ -1324,7 +1360,9 @@ void FED3::ReleaseMotor () {
13241360 digitalWrite (A3, LOW);
13251361 digitalWrite (A4, LOW);
13261362 digitalWrite (A5, LOW);
1327- digitalWrite (MOTOR_ENABLE, LOW); // disable motor driver and neopixels
1363+ if (EnableSleep==true ){
1364+ digitalWrite (MOTOR_ENABLE, LOW); // disable motor driver and neopixels
1365+ }
13281366}
13291367
13301368/* *************************************************************************************************************************************************
0 commit comments