Wii Nunchuk via RFM69HW to Duplo 9203 Remote Control Car Kit using ARM mbed on a FRDM-KL25Z

Dependencies:   CRC FastPWM RFM69 USBDevice WakeUp WiiChuk_compat mbed-rtos mbed tlc59108

Fork of wiiNunchuk_compat by Greg Brush

Revision:
23:e08b10694786
Parent:
22:d4d991c7a40b
Child:
24:46777890b7d9
Child:
25:74edb9fc46c6
--- a/main.cpp	Tue Jun 28 15:39:15 2016 +0000
+++ b/main.cpp	Tue Jun 28 19:14:58 2016 +0000
@@ -258,21 +258,23 @@
     PwmOut b(LED_BLUE);
     //float b = 1.0f;
     DigitalOut nun_vdd(PTE31);
-    WiiChuck *nun = 0;
+    WiiChuck *nun;
+    uint8_t nun_settled = 0;
     bool nun_init(nunchuk *next) {
-        for (int i = 0; i < 10; i++) {
-            nun_vdd = 1;
-            Thread::wait(100);
+        for (int i = 0; i < 3; i++) {
             if (nun)
                 delete nun;
+            nun_vdd = 1;
+            Thread::wait(50);
             nun = new WiiChuck(PTE0, PTE1, pc);
             if (nun->Read(&next->X, &next->Y, &next->aX, &next->aY, &next->aZ, &next->C, &next->Z)) {
+                nun_settled = 0;
                 Thread::wait(10);
                 return true;
             }
             pc.printf("nun_init error\r\n");
             nun_vdd = 0;
-            Thread::wait(100);
+            Thread::wait(50);
         }
         return false;
     }
@@ -305,6 +307,7 @@
     radio.sleep();
     WakeUp::set(rx_last_contact.read() < 60? 1 : 5);
     deepsleep();
+    stops_sent = 0;
     rx_snoozing = false;
     rx_to_snooze = true;
     pc.printf("unsnooze rx\r\n");
@@ -395,7 +398,7 @@
     bool read = false;
     while(1) {
         if (sender) {
-            if (central_time.read() > 20) {
+            if (/*nun_settled++ > 2 && */central_time.read() > 20) {
                 #if DEBUG
                 pc.printf("snooze tx %f\r\n", central_time.read());
                 g = 0; Thread::wait(10); g = 1; Thread::wait(10);
@@ -421,7 +424,7 @@
             rx_last_contact.reset();
             rx_snooze.stop();
             rx_to_snooze = true;
-            pc.printf("rssi %d\r\n", radio.RSSI);
+            //pc.printf("rssi %d\r\n", radio.RSSI);
             read = (radio.DATALEN == sizeof(struct nunchuk));
             if (read) {
                 memcpy((void*)next, (const void*)radio.DATA, radio.DATALEN);
@@ -480,11 +483,16 @@
 #if DEBUG > 1
                 pc.printf("twitch %f\r\n", twitch);
 #endif
-                if (n->C || n->Z || twitch > 100)
-                    central_time.reset();
                 r = 1.0f;
                 g = 1.0f;
                 b = 1.0f;
+                if (n->C || n->Z || /*nun_settled > 2 &&*/ twitch > 500) {
+                    Thread::wait(10);
+#if DEBUG
+                    pc.printf("c %d z %d twitch %f\r\n", n->C, n->Z, twitch);
+#endif
+                    central_time.reset();
+                }
             } else {
                 central_time.reset();
                 if (central) {