setWatch & analogRead on BBC micro:bit

Posted on
  • Hi Gordon,

    I can use analogRead on pin D0 of the microbit ok, but setWatch does not seem work using the following code (this code works fine on classic Espruino to detect low to high voltage changes):-

    function OnMotion()
    {
    console.log("Movement detected");
    }
    setWatch( OnMotion, D0, {repeat:true, edge:"rising"} );

    Strangely when I call analogRead after setting the watch it seems to trigger the watch callback into action for one shot.

    Regards
    David

  • You don't mention any pinMode();. Espruino has a useful auto-behavior for setting pin mode, which may cause this strange behavior in combination with the different hardware: see Note in Reference.

    Therefore, give it a shot and define pin's mode first. I used both analog and digital input in conjunction with Resistive Touchscreen directly (no touch controller). You may get some ideas from there.

    Basically, I set the pin up for digital single shot watch with pull_up/down, in the callback I change the setup and make analog reads with no-pulls to get the x/y coordinates of the resistive touch screen, and return to initial setup with single shot watch.

    For you, this means:

    1. declare a function process() { ... } that:
      i) sets pin up for analog read - input with no pull up or down - to get untainted analog value
      ii) read the analog value and process it
      iii) invoke watch(), - see next

    2. declare a function watch() { ... } that:
      i) sets pin up for watch - input with pull-up or down and single trigger and desired rising/falling/both trigger - to get the trigger
      ii) use process as the watch's callback to handle the triggering value

    3. Start the 'closed' cycle by invoking function setup();.

  • So you're saying that even without analogRead, you can't use D0?

    For analogRead, I think this would be an issue on any board - internally, the pin can only be connected to one peripheral at a time - eg, SPI, I2C, Serial, GPIO, or Analog.

    So you've got it set to digital input for setWatch, but then when you read the analog value with analogRead, the pin is disconnected from the input and turned into an analog input - which is probably what causes the state change for the watch.

    As @allObjects says you might be able to force it with pinMode, but it's quite possible that analogRead won't work on the pin after that

  • Thanks, I was just using the default pin mode and assuming it was set to digital like the classic Espruino seems to be. I'll try setting it explicitly and see what happens. (I was not trying to use analog mode at the same time by the way, I just had to resort to that later).

  • Well, if you're not using analogRead, you shouldn't need the pin mode...

    But are you properly pulling the pin high and low? If not, you might need to use the internal pullup/down resistors (eg. pinMode with input_pullup)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

setWatch & analogRead on BBC micro:bit

Posted by Avatar for user63222 @user63222

Actions