• Thanks for your answer!

    Here is the complete code:

    var actual_menu, gatt;
    var last_wheel_event_time = 0, actual_wheel_event_time;
    var last_cumulative_wheel_revolutions = 0, actual_cumulative_wheel_revolutions = 0, track_cumulative_revolutions = 0;
    var delta_revolutions, delta_event_time, timer = -1;
    var wheel_circumference = 2340;
    var delta_time_in_ms, connected = false;
    
    function connect() {
      print("Starting scan...");
      g.clear();
      var s = "Connecting...";
      g.drawString(s ,0 ,0); g.flip();
      try {
      NRF.requestDevice({ timeout:20000, filters: [{ namePrefix: 'Wahoo' }] }).then(function(device) {
      console.log(device);
      return device.gatt.connect();
      }).then(function(gatt_object) {
        gatt = gatt_object;
        s = "Looking for service...";
        g.clear(); g.drawString(s ,0 ,0); g.flip();
        return gatt.getPrimaryService("1816");
        }).then(function(service) {
          s = "Looking for char...";
          g.clear(); g.drawString(s ,0 ,0); g.flip();
          return service.getCharacteristic("0x2A5B");
        }).then(function(characteristic) {
          characteristic.on('characteristicvaluech­anged', OnNotify);
          return characteristic.startNotifications();
          }).then(function() {
            connected = true;
            setDisplayMode("livedata");
            console.log("Done!");
            // Then call gatt.disconnect(); if/when you want to disconnect
            });
      } catch(error) {
          print("Sensor not found", error);
          g.clear(); g.drawString("No sensor found..." ,0 ,0); g.flip();
        }
    }
    
    function disconnect() {
      if (gatt) {
          gatt.disconnect();
      }
      connected = false;
      print("Connected: ", connected);
      setDisplayMode("menu");
    }
    
    function OnNotify(event) {
        //console.log("-> "+(event.target.value.getUint8(0, true)>>> 0).toString(2)); //characteristics flags
        actual_cumulative_wheel_revolutions = event.target.value.getUint32(1, true);
        actual_wheel_event_time = event.target.value.getUint16(5, true);
        //console.log("Cumulative wheel Revolutions: " + actual_cumulative_wheel_revolutions);
        //console.log("Last Wheel Event time: "+ actual_wheel_event_time + " ms");
        delta_revolutions = actual_cumulative_wheel_revolutions - last_cumulative_wheel_revolutions;
        //console.log("-> "+event.target.value.getUint16(7, true)); //crank
        //console.log("-> "+event.target.value.getUint16(9, true)); //crank
      	if (actual_wheel_event_time >= last_wheel_event_time) {
    				delta_event_time = actual_wheel_event_time - last_wheel_event_time;
    		} else {
    				delta_event_time = 65536 - last_wheel_event_time + actual_wheel_event_time;
    			}
        delta_time_in_ms =(delta_event_time / 1.024);
        //console.log("delta_time_in_ms:" + delta_time_in_ms);
        last_wheel_event_time = actual_wheel_event_time;
        last_cumulative_wheel_revolutions = actual_cumulative_wheel_revolutions;
        track_cumulative_revolutions += delta_revolutions;
    }
    
    var mainmenu = {
      "" : {
        "title" : "-- Main Menu --",
        "fontHeight" : 10
      },
    
      "Connect" : function () { setDisplayMode("connecting"); },
      "Disconnect" : function () { disconnect(); },
      "Live Data" : function() {setDisplayMode("livedata"); },
      "New Track" : function () {},
      "Toggle Backlight" : function() { LED1.toggle(); },
      "-> Settings" : function () {Pixl.menu(SettingsMenu); },
      //"Exit" : function() { Pixl.menu(); },
    };
    
    var SettingsMenu = {
      "" : {
        "title" : "-- Settings --",
        "fontHeight" : 10
      },
      "Set Wheel Circumference" : undefined, // do nothing
      "Set Total Distance Counter" : undefined, // do nothing
      "< Back" : function() { Pixl.menu(mainmenu); },
    };
    
    function showLiveData() {
      //print("showlivedata");
      g.clear();
      var s = "Cum. Wheel Revolutions: ";
      g.drawString(s ,0 ,0); //95 - g.stringWidth(s) g.getWidth()
      g.drawString(actual_cumulative_wheel_rev­olutions, 0, 11);
      var speed = delta_revolutions * wheel_circumference / delta_time_in_ms * 60 * 60 / 1000;
      s = "Speed: " + String(speed.toFixed(2));
      g.drawString(s, 0, 22);
      s = "Track distance: " + (track_cumulative_revolutions * wheel_circumference / 1000 / 1000).toFixed(2);
      g.drawString(s, 0, 33);
      g.flip();
    }
    
    function onInit () {
      require("Font8x12").add(Graphics);
      setDisplayMode("menu");
      setWatch(function () { setDisplayMode("menu"); }, BTN2, {repeat:true, debounce:50});
    }
    
    function setDisplayMode(displaymode) {
     print("setDisplayMode: ", displaymode);
     switch (displaymode) {
             case "switchToConnected":
               Pixl.menu();
               break;
             case "connecting":
               if (timer!==-1) {clearInterval(timer); timer = -1;}
               Pixl.menu();
               setTimeout(function() {connect();},1);
               break;
             case "livedata":
               print("livedata");
               Pixl.menu();
               if (timer==-1) {
                 print("Set timer"); 
                 timer = setInterval( function () {showLiveData(); }, 2000); 
                 print("timer = ", timer);
               }
               break;
             case "menu":
               if (timer!==-1) {clearInterval(timer); timer = -1;}
               m=Pixl.menu(mainmenu);
               g.setFont8x12();
               m.draw();
               }
    }
    
    onInit();
    
    

    Though sadly your idea with

    setTimeout(connect, 1);

    did not work out; returning the same error. Thanks again!

About

Avatar for Joost @Joost started