javascripthtmlcanvasmindwave

multiple HTML5 canvas with Neurosky sensor input is not working


I have created multiple html5 canvas using instantiation mode in P5JS. I am using Neurosky mindwave EEG sensor to activate and deactivate canvas one by one. Neurosky mindwave EEG sensor can detect user's eye blink which I am using as input. When user blinks, it should activate one canvas and deactivate another canvas and vice-versa.I am using Neurosky mindwave EEG sensor to activate and deactivate canvas one by one. Neurosky mindwave EEG sensor can detect user's eye blink which I am using as input. When user blinks, it should activate one canvas and deactivate another canvas and vice-versa.

Just to check if my code logic works, I used mouse pressed input to switch between the canvas and it worked perfectly. But, when I used it with the sensor it didn't work.

What I did - I have created multiple HTML5 canvas using instantiation mode in P5JS. I have used node-neurosky node module to capture the eyeblink data from the sensor. Node Module

What worked - When I launch the app it takes the eye blink as input for the first time and activate the another canvas but when I blink again it doesn't deactivate the current canvas and activate another canvas. I have tried printing flags to check the code and it is doing fine. Eyeblink gets detected every time when I blink but it doesn't switch the canvas.

What didn't work - When I tried to use eye blink strength directly into the sketch.js it didn't work then I created another boolean variable eyeclick which also didn't work.

sketch.js

var stateTwo, stateOne = true;
// sketch one -----------------------------------

var first = new p5(firstSketch, "canvasOne");

function firstSketch(p) {

    p.setup = function() {
        p.createCanvas(400, 250);
    }
    p.draw = function() {
        p.background(255, 10, 100);
        p.fill(255);
        p.ellipse(p.width / 2, p.height / 2, 50, 50);
        if (eyeclicked) {
            stateOne = false;
            stateTwo = true;
            console.log(" canvas <-- one");
            // k = 0;
            eyeclicked = false;
        }
        if (stateOne) {
            $('#canvasOne').css('opacity', '1');
            $('#canvasTwo').css('opacity', '0.5');
            // console.log("canvas One");
            p.fill(255, 0, 0);
            p.ellipse(p.random(p.width), p.random(p.height), 50, 50);
        }
    }
}

// sketch two -----------------------------------

var second = new p5(secondSketch, "canvasTwo");

function secondSketch(p) {

    p.setup = function() {
        p.createCanvas(400, 250);
    }
    p.draw = function() {
        p.background(60, 250, 100);
        p.fill(0);
        p.ellipse(p.width / 2, p.height / 2, 50, 50);

        if (eyeclicked) {
            stateOne = true;
            stateTwo = false;
            console.log(" canvas <-- two");
            //  k = 0;
            eyeclicked = false;
        }

        if (stateTwo) {
            $('#canvasOne').css('opacity', '0.5');
            $('#canvasTwo').css('opacity', '1');
            // console.log("canvas Two");
            p.fill(0, 0, 255);
            p.ellipse(p.random(p.width), p.random(p.height), 50, 50);
        }
    }
}

NodeCode to connect with sensor connect.js

 var attention = 0;
 var meditation = 0;
 var blink;
 var poorSignalLevel = 0;
 var eyeclicked = false;

 if ("WebSocket" in window) {
     console.log("WebSocket is supported by your Browser. Proceed.");
     // $('#connect-controls').show();
 }

 var ws = new WebSocket("ws://127.0.0.1:8080");
 ws.onopen = function() {
     console.log('opened connection');
     ws.send("Hello from websocket client!");
 };

 // whenever websocket server transmit a message, do this stuff
 ws.onmessage = function(evt) {
     // parse the data (sent as string) into a standard JSON object (much easier to use)
     var data = JSON.parse(evt.data);
     // handle "eSense" data
     if (data.eSense) {
         $('#brain').css({
             opacity: 1
         });
         attention = data.eSense.attention;
         meditation = data.eSense.meditation;
         // brainProgress('#focusProgress', attention);
         // brainProgress('#medProgress', meditation);
         $("#focus").text(attention);
         $("#meditation").text(meditation);
     }

     // handle "blinkStrength" data
     if (data.blinkStrength) {
         blink = data.blinkStrength;
         var blinkcol = "white";
         var eyeVal = map_range(blink, 0, 255, 0, 100);
         $('#eyeBlinkStrength').text(parseInt(eyeVal));
         if (blink > 40) {
             //blinkcol = "rgba(102,211,43,1.0)";
             eyeclicked = true;
             //  k++;   
             console.log(blink + " " + eyeclicked);
         } else blinkcol = "white";
         $('#eyeBlink').css({
             width: eyeVal,
             height: eyeVal,
             background: blinkcol
         });
     } else {
         blink = 0;
         eyeclicked = false;
     }

     // handle "poorSignal" data
     if (data.poorSignalLevel != null) {
         poorSignalLevel = parseInt(data.poorSignalLevel);
     }
 };

 // when websocket closes connection, do this stuff
 ws.onclose = function() {
     // websocket is closed.
     console.log("Connection is closed...");
 };


 function map_range(value, low1, high1, low2, high2) {
     return low2 + (high2 - low2) * (value - low1) / (high1 - low1);
 }

EDIT CODE PEN DEMO

Mouse Input Based Code which demonstrate the logic of switching between multiple canvas. It works perfectly. Try to click into the center circle

var stateTwo, stateOne = true;
var eyeIsBlinked;
// sketch one -----------------------------------

var first = new p5(firstSketch, "canvasOne");

function firstSketch(p) {

    p.setup = function() {
        p.createCanvas(400, 250);
    }
    p.draw = function() {
        p.background(255, 10, 100);
        p.fill(255);
        p.ellipse(p.width / 2, p.height / 2, 50, 50);
        if (p.mouseIsPressed && p.dist(p.mouseX, p.mouseY, p.width / 2, p.height / 2) < 50) {
            stateOne = false;
            stateTwo = true;
            console.log(" <-- one");
            // k = 0;
            // window.eyeIsBlinked = false;
            // blink = 0;
        }
        if (stateOne) {
            $('#canvasOne').css('opacity', '1');
            $('#canvasTwo').css('opacity', '0.5');
            // console.log("canvas One");
            p.fill(255, 0, 0);
            p.ellipse(p.random(p.width), p.random(p.height), 50, 50);
        }
    }
}

// sketch two -----------------------------------

var second = new p5(secondSketch, "canvasTwo");

function secondSketch(p) {

    p.setup = function() {
        p.createCanvas(400, 250);
    }
    p.draw = function() {
        p.background(60, 250, 100);
        p.fill(0);
        p.ellipse(p.width / 2, p.height / 2, 50, 50);

        if (p.mouseIsPressed && p.dist(p.mouseX, p.mouseY, p.width / 2, p.height / 2) < 50) {
            stateOne = true;
            stateTwo = false;
            console.log(" <-- two");
            //  k = 0;
            //  window.eyeIsBlinked = false;
            //blink = 0;
        }

        if (stateTwo) {
            $('#canvasOne').css('opacity', '0.5');
            $('#canvasTwo').css('opacity', '1');
            // console.log("canvas Two");
            p.fill(0, 0, 255);
            p.ellipse(p.random(p.width), p.random(p.height), 50, 50);
        }
    }
}

Solution

  • I don't know how your project works. But I guess the problem might be a scope problem. Both files are using the eyeclicked variable, but they might be using two different variables. Try to make sure that they're using the same variable by using it inside the window global variable.

    So instead of eyeclicked use window.eyeclicked.