Johnny Five, receiving input from the device

This post is part of the Johnny Five series. See the first post here.

In this post I want to get information from an electronic device using Johnny Five.

In particular, I want to use a water level sensor. This will tell me if I got enough …


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

This post is part of the Johnny Five series. See the first post here.

In this post I want to get information from an electronic device using Johnny Five.

In particular, I want to use a water level sensor. This will tell me if I got enough coffee or if I’m running out of it, and I need to re-fill the cup in order to be a working programmer.

This is the sensor:

We’re going to wire a little circuit to get this data, and we’re going to use Johnny Five to get this data into our Node.js app.

The sensor has 3 pins. One is GND (0V), one is VCC (5V) and the other is the analog data output.

Add the pin marked as - to GND, + to 5V, and connect S to the analog pin A0.

Here’s the circuit:

Now let’s create a sensor.js file with this content:

const { Board, Sensor } = require("johnny-five")
const board = new Board()

board.on("ready", () => {
  const sensor = new Sensor("A0")

  sensor.on("change", function () {
    console.log(this.value)
  })
})

Whenever the data coming in through the sensor changes, we’ll see it being printed to the console:

I used the on() method on the sensor object to watch all changes.

All the methods are detailed here, but I’m in particular interested in the within() method, that lets me define a callback that’s fired when the value is in a particular range:

const { Board, Sensor } = require("johnny-five")
const board = new Board()

board.on("ready", () => {
  const sensor = new Sensor("A0")

  sensor.within([0, 70], function () {
    console.log("Refill your coffee!!")
  })
})

If I start running out of coffee, the program will print “Refill your coffee!!” a lot of times, because the value keeps changing while the sensor gets drier.

So, let’s create an outOfCoffee variable we can use to debounce the data gathering.

We also declare that under 70 we are out of coffee, and above 150 we have enough:

const { Board, Sensor } = require("johnny-five")
const board = new Board()

board.on("ready", () => {
  const sensor = new Sensor("A0")
  let outOfCoffee = false

  sensor.within([0, 70], () => {
    if (!outOfCoffee) {
      outOfCoffee = true
      console.log("Refill your coffee!!")
    }
  })

  sensor.within([150, 500], () => {
    if (outOfCoffee) {
      outOfCoffee = false
      console.log("Ok, you can go on programming!!")
    }
  })
})

That’s it, now if I try moving the sensor in/out the cup of coffee, I get some more useful warnings:


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate
APA
flaviocopes.com | Sciencx (2022-10-02T14:26:41+00:00) » Johnny Five, receiving input from the device. Retrieved from https://www.scien.cx/2021/04/30/johnny-five-receiving-input-from-the-device/.
MLA
" » Johnny Five, receiving input from the device." flaviocopes.com | Sciencx - Friday April 30, 2021, https://www.scien.cx/2021/04/30/johnny-five-receiving-input-from-the-device/
HARVARD
flaviocopes.com | Sciencx Friday April 30, 2021 » Johnny Five, receiving input from the device., viewed 2022-10-02T14:26:41+00:00,<https://www.scien.cx/2021/04/30/johnny-five-receiving-input-from-the-device/>
VANCOUVER
flaviocopes.com | Sciencx - » Johnny Five, receiving input from the device. [Internet]. [Accessed 2022-10-02T14:26:41+00:00]. Available from: https://www.scien.cx/2021/04/30/johnny-five-receiving-input-from-the-device/
CHICAGO
" » Johnny Five, receiving input from the device." flaviocopes.com | Sciencx - Accessed 2022-10-02T14:26:41+00:00. https://www.scien.cx/2021/04/30/johnny-five-receiving-input-from-the-device/
IEEE
" » Johnny Five, receiving input from the device." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/04/30/johnny-five-receiving-input-from-the-device/. [Accessed: 2022-10-02T14:26:41+00:00]
rf:citation
» Johnny Five, receiving input from the device | flaviocopes.com | Sciencx | https://www.scien.cx/2021/04/30/johnny-five-receiving-input-from-the-device/ | 2022-10-02T14:26:41+00:00
https://github.com/addpipe/simple-recorderjs-demo