You are reading a single comment by @Rollo and its replies. Click here to read the full conversation.
  • @allObjects thanks to your suggestions I have played with different timers and debounce values again and I think I'm getting somewhere.

    @Gordon it's even less reliable this way, it's difficult to describe LED's behavior now so I have changed the code to use console.log and it looks interesting.

    First try:

    var door = new Pin(A8); //pull-up
    
    E.on("init", function () {
      door.mode("input");
    });
    
    function doorOpenedHandler() {
      console.log("door opened");
    }
    
    function doorClosedHandler() {
      console.log("door closed");
    }
    
    setWatch(function () {
      doorOpenedHandler();
    }, door, {repeat: true, edge: "falling", debounce: 500});
    
    setWatch(function () {
      doorClosedHandler();
    }, door, {repeat: true, edge: "rising", debounce: 500});
    
     1v81 Copyright 2015 G.Williams
    >echo(0);
    =undefined
    >save()
    =undefined
    Erasing Flash.....
    Writing.....
    Compressed 81600 bytes to 2942
    Checking...
    Done!
    
    // I'm starting with closed switch
    door closed
    >door.read()
    =true
    
    // I'm opening the switch
    door opened
    >door.read()
    =false
    
    // I'm closing the switch
    door opened
    >door.read()
    =true
    // wait, what? wrong handler got called but the door value is correct
    

    Now let's try with e.state:

    ...
    setWatch(function (e) {
      if (e.state) {doorOpenedHandler();} else {doorClosedHandler();}
    }, door, {repeat: true, edge: "both", debounce: 500});
    
    // I'm starting with closed switch again, looks like the logic has changed
    door opened
    >door.read()
    =true
    
    // I'm opening the switch
    door closed
    >door.read()
    =false
    
    // I'm closing the switch
    door opened
    >door.read()
    =true
    
    // I'm opening the switch
    door closed
    
    // I'm closing the switch
    door closed
    // wrong handler again i.e. after seeing "door closed" the last time I would expect "door opened" now
    

    Any ideas, please? Is my wiring ok?

About

Avatar for Rollo @Rollo started