• This project is a wireless controlled 2 wheeled robot.

    Master hardware:
    -Espruino 1v54

    • Wii nunchuck (B10, B11)
    • NRF24L01P wireless chip

      Master software code:

      var btn_state=false;
      var read_wii,wii_data,wii_data_new;
      SPI1.setup({sck:A5, miso:A6, mosi:A7});
      //I2C1.setup({scl:B6,sda:B7}); // slave
      I2C2.setup({scl:B10,sda:B11}); //master
      var nrf = require("NRF24L01P").connect( SPI1, B0, B1 );
      var wii = require("wii_nunchuck").connect(I2C2);
      function onInit() {
      nrf.init([0,0,0,0,2], [0,0,0,0,1]);
      wii_data_new=wii_data;
      }
      onInit();
      setInterval(function() {
      nrf.masterHandler();
      }, 50);
      function start_wii_read(){
      read_wii = setInterval(function(){
      //console.log(JSON.Stringify(wii.read()));
      wii_data_new=wii.read();
      //console.log(wii_data.joy);
      if(wii_data.btn.z!==wii_data_new.btn.z){
        if(wii_data_new.btn.z){ wii_btn_z_press(); } else { wii_btn_z_release(); }
      }
      if(wii_data.btn.c!==wii_data_new.btn.c){
        if(wii_data_new.btn.c){ wii_btn_c_press(); } else { wii_btn_c_release(); }
      }
      wii_data=wii_data_new;
      },20);
      }
      function stop_wii_read(){
      	clearInterval(start_read);
      }
      
      function wii_btn_z_press(){
      //console.log("Z");
      LED2.set();
      nrf.sendCommand("MOTOR(0,1,0,1);",function(r){});
      //nrf.sendCommand("LED2.set();",function(r){});
      }
      function wii_btn_c_press(){
      LED1.set();
      nrf.sendCommand("MOTOR(1,0,1,0);",function(r){});
      //console.log("C");
      }
      function wii_btn_z_release(){
      LED2.reset();
      nrf.sendCommand("MOTOR(0,0,0,0);",function(r){});
      //nrf.sendCommand("LED2.reset();",function(r){});
      //console.log("stopZ");
      }
      function wii_btn_c_release(){
      //console.log("stopC");
      LED1.reset();
      nrf.sendCommand("MOTOR(0,0,0,0);",function(r){});
      //nrf.sendCommand("LED1.reset();",function(r){});
      //console.log("stopC");
      }
      
      function led_on() {
      	LED3.set();
      }
      
      function led_off() {
      	LED3.reset();
      }
      
      wii_data=wii.read();
      
      btn_watch_press = setWatch(function(){
      	if(!btn_state){
      		led_on();
          start_wii_read();
      } else {
      		led_off();
          stop_wii_read();
      	}
      	btn_state=!btn_state;
      },BTN,{repeat:true,edge:'rising'});
      btn_watch_release = setWatch(function(){
      	console.log(btn_state);
      },BTN,{repeat:true,edge:'falling'});
      

    Slave hardware:
    -Espruino 1v53
    -L298N motor controller board with 2 hacked micro servo motor used as simple DC motor
    -NRF24L01P wireless chip
    -6xAA wired in series rechargeable 2000mAh battery

    Slave software code:

    /* NRF24L01P - wireless */
    SPI1.setup({sck:A5, miso:A6, mosi:A7});
    var nrf = require("NRF24L01P").connect( SPI1, B0, B1 );
    function onInit() {
      nrf.init([0,0,0,0,1], [0,0,0,0,2]);
    }
    onInit();
    setInterval(function() {
      nrf.slaveHandler();
    }, 50);
    
    /* motor controller */
    var MOTOR_PINS=[C9,C8,C7,C6];
    function MOTOR(val1,val2,val3,val4){
      digitalWrite(MOTOR_PINS[0],parseInt(val1));
      digitalWrite(MOTOR_PINS[1],parseInt(val2));
      digitalWrite(MOTOR_PINS[2],parseInt(val3));
      digitalWrite(MOTOR_PINS[3],parseInt(val4));
    }
    

    Sometimes NRF24L01P on master trow an error: TX not received 30

    Wii nunchuk I2C port BUSY. Got error message when try to save() to master espruino.
    Error message:

    INTERNAL ERROR: Timeout on I2C Read BUSY
    INTERNAL ERROR: Timeout on I2C Read SB
    INTERNAL ERROR: Timeout on I2C Read Receive Mode
    INTERNAL ERROR: Timeout on I2C Read Receive
    INTERNAL ERROR: Timeout on I2C Read Receive
    INTERNAL ERROR: Timeout on I2C Read Receive
    INTERNAL ERROR: Timeout on I2C Read Receive
    INTERNAL ERROR: Timeout on I2C Read Receive
    INTERNAL ERROR: Timeout on I2C Read Receive
    INTERNAL ERROR: Timeout on I2C Read STOP
    at line 1 col 23
    {var a=b.readFrom(82,6);b.writeTo(82,0);return{joy:{x:(a[0]-127)/128,y:(a[1]-127)/128},acc:{x:(a[2]-127)/54,y:(a[3]-127)/54,z:(a[4]-127)/54},btn:{z:!(a[5]&1),c:!(a[5]&2)}}}

                       ^
    

    in function "read" called from line 3 col 27
    in function called from system
    Execution Interrupted during event processing - clearing all timers and watches.

    Video:
    https://vimeo.com/87922368

About

Avatar for Benceee @Benceee started