Home>

I want to perform the operation of blinking the LED of arduino with Johnny-five

I made it possible to operate Arduino from the Web, and I can turn the LED on and off, but I can't blink.

Corresponding source code
const express = require ("express");
const five = require ("johnny-five");
const board = new five.Board ({port: "COM5"});
const app = express ();
app.use (express.static ("C: \ Users \ ***** \ AppData \ Roaming \ npm \ node_modules \ johnny-five \ public"));
const bodyParser = require ("body-parser");
app.use (bodyParser.urlencoded ({extended: false}));
let led;
board.on ('ready', function () {
    led = new five.Led (12);
});
const message = "current state:";
app.get ("/", (req, res) =>{
    res.render ("index.ejs", {
        content: message + "off",
    });
});
app.post ("/", (req, res) =>{
    if (req.body.operate == "on") {
        led.on ();
    }
    if (req.body.operate == "off") {
        led.off ();
    }]
// A program that blinks below this
    if (req.body.operate == "stop") {
        led.on ();
        sleep (2000);
        led.off ();
    }
//communication
    res.render ("index.ejs", {
        content: message + req.body.operate,
    });
});const server = app.listen (3000, () =>console.log ("System"));
<! DOCTYPE html><html><head>    <meta http-equiv = "content-type" content = "text/html" charset = "utf-8">    <title>NodeWebOperate</title>    <link type = "text/css" href = "C: \ Users \ ***** \ AppData \ Roaming \ npm \ node_modules \ johnny-five \ style.css" rel = "stylesheet"><style>  .parent {
     width: 100%;
     display: flex;
     display: flex;
      justify-content: center;
      align-items: center;
    }
    .child {
     width: 100px;
    }
    iframe {
     overflow: hidden;
    }
    .buttonstyle {
        position: fixed;/* Fix the position of the element * /
        bottom: 0;/* Specify the reference position at the bottom of the screen * /
        right: 0;/* Specify the reference position on the far right of the screen * /
        width: 300px;/* Specify the width * /
    }

        .html {
            height: 150px;
            overflow-y: scroll;
            -ms-overflow-style: none;/* IE, Edge compatible * /
            scrollbar-width: none;/* Firefox compatible * /
        }
        .html ::-webkit-scrollbar {/ * Chrome, Safari compatible * /
            display: none;
        }
  </style></head><body>    <main>        <form action = "/" method = "post"><button type = "submit" value = "on" name = "operate"><svg width = "10em" height = "10em" viewBox = "0 0 16 16" fill = "currentColor" xmlns = "http: // www.w3.org/2000/svg ">                <path fill-rule = "evenodd" d = "M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zm-11.5.5a.5.5 0 0 1 0-1h5.793L8.146 5.354a.5.5 0 1 1. 708-.708l3 3a.5.5 0 0 1 0 .708l-3 3a.5.5 0 0 1-.708-.708L10.293 8.5H4.5z "/>              </svg></button>                              </form>              <form action = "/" method = "post">                                    <button type = "submit" value = "off" name = "operate">              <svg width = "10em" height = "10em" viewBox = "0 0 16 16" fill = "currentColor" xmlns = "http://www.w3.org/2000/svg">                <path fill-rule = "evenodd" d = "M4.146 3.646a.5.5 0 0 0 0 .708L7.793 8l-3.647 3.646a.5.5 0 0 0 .708.708l4-4a.5.5 0 0 0 0-. 708l-4-4a.5.5 0 0 0-.708 0zM11.5 1a.5.5 0 0 1 .5.5v13a.5.5 0 0 1-1 0v-13a.5.5 0 0 1 .5-.5z "/>              </svg></button>             </form>
              </form>            <form action = "/" method = "post">                                <button type = "submit" value = "on" name = "operate">              <svg width = "10em" height = "10em" viewBox = "0 0 16 16" fill = "currentColor" xmlns = "http://www.w3.org/2000/svg">                <path fill-rule = "evenodd" d = "M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zm-7.5 3.5a.5.5 0 0 1-1 0V5.707L5.354 7.854a.5.5 0 1 1- .708-.708l3-3a.5.5 0 0 1 .708 0l3 3a.5.5 0 0 1-.708.708L8.5 5.707V11.5z "/>              </svg></button>               </form>              <form action = "/" method = "post">                                    <button type = "submit" value = "off" name = "operate">              <svg width = "10em" height = "10em" viewBox = "0 0 16 16" fill = "currentColor" xmlns = "http://www.w3.org/2000/svg">                <path fill-rule = "evenodd" d = "M3.646 11.854a.5.5 0 0 0 .708 0L8 8.207l3.646 3.647a.5.5 0 0 0 .708-.708l-4-4a.5.5 0 0 0 -.708 0l-4 4a.5.5 0 0 0 0 .708zM2.4 5.2c0 .22.18.4.4.4h10.4a.4.4 0 0 0 0-.8H2.8a.4.4 0 0 0-.4.4z "/>              </svg></button>             </form>            <form action = "/" method = "post">                                    <button type = "submit" value = "stop" name = "operate">            <svg width = "10em" height = "10em" viewBox = "0 0 16 16" fill = "currentColor" xmlns = "http://www.w3.org/2000/svg">                <path fill-rule = "evenodd" d = "M3.646 4.146a.5.5 0 0 1 .708 0L8 7.793l3.646-3.647a.5.5 0 0 1 .708.708l-4 4a.5.5 0 0 1-. 708 0l-4-4a.5.5 0 0 1 0-.708zM1 11.5a.5.5 0 0 1 .5-.5h13a.5.5 0 0 1 0 1h-13a.5.5 0 0 1-.5-.5z "/>              </svg></button>             </form>
      </main></body></html>
Supplementary information (FW/tool version, etc.)

Arduino Uno
Reference: https://qiita.com/masayasviel/items/e6bf949b81d42da27d9a

  • Answer # 1

    led.on ();
    Promise.resolve ()
      .then (wait (10)) // Wait 10 seconds here (passing "function that returns Promise object" to then)
      .then (function () {
    led.off ();
      })
      .catch (function (err) {
        console.error (err);
        self.result_message = error;
      });