1

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);
        }
    }
}
B L Λ C K
  • 600
  • 1
  • 8
  • 24

1 Answers1

0

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.

João Mosmann
  • 2,884
  • 19
  • 25
  • Thanks for the reply but it didn't do anything. I added window.eyeclicked but it didn't do anything :( Here is the screenshot of the code - https://ibb.co/jgKugF. It shows the two cavas created usinng P5JS and if you notice one is faded and other is not. The faded one is non-active canvas and other is active canvas. Both canvas shows randomly moving ellipse when active and non-active cnavas show no activity. It should switch whenever user blink his eyes. – B L Λ C K Mar 29 '17 at 12:57
  • Are you sure that the `ws.onmessage` event handler is being called? Try debugging see if it's going all through the code. put a `debugger;` in the first line of the function, open your browser's webdevtool and reload the page. – João Mosmann Mar 29 '17 at 13:10
  • Yes, it does get called. I check it. Also, eyeClicked get pass into the code when I comment out one of the canvas code completely. It works for both canvas code when one is inactive but i doesn't work when both code are active. – B L Λ C K Mar 29 '17 at 13:42
  • It's hard to help on this, It's a logic problem I guess, nothing related to the technologies used on it. Try to make and opacity change after you define the eyeclicked=true instead of in both draw functions of the canvas. basically move the `if (eyeclicked) {`, `if (stateOne) {` and `if (stateTwo) {` to after the `eyeclicked = true;` in the **NodeCode to connect with sensor connect.js** – João Mosmann Mar 29 '17 at 14:05
  • Because it's calling first the first canvas draw function, so the state will be always `stateOne = false; stateTwo = true;` then it sets `eyeclicked=false` so it will never go through the second canvas draw state change. – João Mosmann Mar 29 '17 at 14:07
  • Mosmann I wish I can agree with you but the logic is correct in my understanding [please excuse my tone]. It works perfectly when I use mouseIsPressed instead of eyeClicked so I believe it has something to do with the multiple canvas where only one canvas can take sensor input actively for your further clarification I would add the mousePressed version of code. Thanks for the reply :) – B L Λ C K Mar 30 '17 at 02:34
  • 1
    Hehe, no worries, but still. It's a logic problem. See, in your codepen, you check for `p.mouseIsPressed`. But this variable is contextual, each canvas have one, and it will be true if the mouse is clicked inside that same canvas. In the other hand, with the `eyeclicked`, it's a global variable, that both canvas use, but the thing is, the first canvas draw function is always called before the second canvas draw function, and the draw function set `eyeclicked` as false, resulting in that the `eyeclicked` variable will never be true inside the second canvas draw function. – João Mosmann Mar 30 '17 at 15:25
  • @ João Mosmann Thank you so much :) :) I fixed the problem and yes it was a logical error :) thank you again – B L Λ C K Apr 04 '17 at 03:43