Project tutorial
Web Sketch (HTML5 Canvas)

Web Sketch (HTML5 Canvas) © GPL3+

Data from 2 rotary switches are sent to a webpage. It draws lines using HTML5 canvas with PHPoC Shield for Arduino like the Etch A Sketch.

  • 2,785 views
  • 1 comment
  • 25 respects

Components and supplies

About this project

Project

Do you know a game called "Etch A Sketch"?

You can draw lines with each knob on the bottom corner: the left knob for horizontal lines and the right knob for vertical lines.

I made this toy by using PHPoC Arduino Shield (P4S-347/P4S-348).

How it Works

Data from 2 rotary switches is sent to web page, and it draws lines using HTML5 canvas like the Etch A Sketch.

ADC reads voltage from rotary switches and converts it to a number between 0 and 1023 (5V). You can only try easy drawings, because the values are only available from 0 to 1023.

When you use a mobile device, you can erase drawings by shaking the device which is shown in the video.

Code

Arduino Source codeArduino
2. Upload Arduino source code
- Add Arduino library
Please add the PHPoC Library from [Sketch] > [Include Library] > [Manager Libraries...]

- Upload Arduino sketch
#include "SPI.h"
#include "Phpoc.h"

PhpocServer server(80);

void setup() {
  Serial.begin(9600);
  while(!Serial)
    ;

  Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
  //Phpoc.begin();

  server.beginWebSocket("rotary_canvas");

  Serial.print("WebSocket server address : ");
  Serial.println(Phpoc.localIP());  
}

void loop() {
      
  int x = 0, y = 0;
  
  for (int i = 0; i < 10; i++) 
  {
    x = x + analogRead(A0);
    y = y + analogRead(A1);
  }
  
  String rotary = String(x/10) + "/" + String(y/10);
  
  char rotaryBuf[10];
  rotary.toCharArray(rotaryBuf, 10);
  
  server.write(rotaryBuf);
  Serial.println(rotaryBuf);
  
  delay(50);
}
PHPoC Shield source codeHTML
1. Upload PHPoC Shield source code
Please refer to the manual of PHPoC Debugger below, and upload the source code(rotary_canvas.php).
http://www.phpoc.com/support/manual/phpoc_debugger_manual/contents.php?id=major_upload

- rotary_canvas.php
<!DOCTYPE html>
<html>
<head>
<title>PHPoC Shield - Sketch</title>
<meta name="viewport" content="width=device-width, initial-scale=0.55, maximum-scale=0.55">
<style>
body { font-family: verdana, Helvetica, Arial, sans-serif, gulim; font-weight: bold; text-align: center; }
h1 { font-size: 25pt; }
h2 { font-size: 15pt; }
#remote { background-color: lightgrey; border: 50px solid #E00000; border-radius: 5%;}
</style>
<script type="text/javascript">

var ws;
var canvas;
var ctx;
var WIDTH = 500;
var HEIGHT = 350;
var startPointX;
var startPointY;

function init()
{    
    if(ws == null)
    {
        ws = new WebSocket("ws://<?echo _SERVER("HTTP_HOST")?>/rotary_canvas", "text.phpoc");
        document.getElementById("ws_state").innerHTML = "CONNECTING";
        
        ws.onopen = ws_onopen;
        ws.onclose = ws_onclose;
        ws.onmessage = ws_onmessage; 
    }
    else
        ws.close();
}
function connect_onclick()
{
    if(ws == null)
    {
        var ws_host_addr = "<?echo _SERVER("HTTP_HOST")?>";
        //var debug = document.getElementById("debug");

        if((navigator.platform.indexOf("Win") != -1) && (ws_host_addr.charAt(0) == "["))
        {
            // network resource identifier to UNC path name conversion
            ws_host_addr = ws_host_addr.replace(/[\[\]]/g, '');
            ws_host_addr = ws_host_addr.replace(/:/g, "-");
            ws_host_addr += ".ipv6-literal.net";
        }

        //debug.innerHTML = "<br>" + navigator.platform + " " + ws_host_addr;
        ws = new WebSocket("ws://" + ws_host_addr + "/rotary_canvas", "text.phpoc");

        document.getElementById("ws_state").innerHTML = "CONNECTING";

        ws.onopen = ws_onopen;
        ws.onclose = ws_onclose;
        ws.onmessage = ws_onmessage;
    }
    else
        ws.close();
}
function ws_onopen()
{
    document.getElementById("ws_state").innerHTML = "<font color='blue'>CONNECTED</font>";
    document.getElementById("bt_connect").innerHTML = "Disconnect";
}
function ws_onclose()
{
    document.getElementById("ws_state").innerHTML = "<font color='gray'>CLOSED</font>";
    document.getElementById("bt_connect").innerHTML = "Connect";

    ws.onopen = null;
    ws.onclose = null;
    ws.onmessage = null;
    ws = null;
}
function ws_onmessage(e_msg)
{
    e_msg = e_msg || window.event; // MessageEvent

    var debug = document.getElementById("debug");
    //debug.innerHTML = e_msg.data;    
    
    canvas  = document.getElementById("remote");
    ctx = canvas.getContext("2d");
    
    var position = e_msg.data.split("/");;
    var x = Math.round(position[0] * WIDTH / 1023);
    var y = Math.round(position[1] * HEIGHT / 1023);
        
    draw(x, y);
}

function draw(pointX, pointY)
{    
    ctx.lineWidth = 2;  
    ctx.strokeStyle = "#404040";
    ctx.lineJoin = "round";
    ctx.lineCap = "round";
    ctx.beginPath(); 
    
    ctx.moveTo(startPointX, HEIGHT - startPointY);
    ctx.lineTo(pointX, HEIGHT - pointY);
    
    ctx.stroke();
    
    startPointX = pointX;
    startPointY = pointY;
    
}

function canvas_clear()
{    
    ctx.clearRect(0, 0, WIDTH, HEIGHT);
}

if (window.DeviceMotionEvent) 
{
    window.addEventListener('devicemotion', detectShake, true);
}
 
var detect_count = 0; 
function detectShake(event)
{    
    var accl = event.acceleration;
    if (accl.x > 1.5 || accl.y > 1.5 || accl.z > 1.5)
        detect_count++;
    
    if (detect_count > 50)
    {
        canvas_clear();
        detect_count = 0;
    }
}

window.onload = init;
</script>
</head>

<body>
<h1>PHPoC Sketch</h1>
<br />
<h2>WebSocket <font id="ws_state" color="gray">CLOSED</font></h2>
<button id="bt_connect" type="button" onclick="connect_onclick();">Connect</button> <button id="bt_clear" type="button" onclick="canvas_clear();">Clear</button>
<span id="debug"></span>
<br /><br /><br />
<canvas id="remote" width="500" height="350">
<br /><br /><br />
</body>
</html>

Schematics

Connection
rotary 1)
VCC ---- 5V
GND ---- GND
SIG ---- A0

rotary 2)
VCC ---- 5V
GND ---- GND
SIG ---- A1
Connection cjzecgexjs

Comments

Similar projects you might like

8X8 Matrix LED Snake Game (HTML5 Web Socket)

Project showcase by hmkim

  • 5,397 views
  • 1 comment
  • 14 respects

Reset Arduino via Web

Project tutorial by IoT_lover

  • 3,777 views
  • 2 comments
  • 29 respects

Arduino Dynamic Web Control

Project tutorial by phpoc_man

  • 34,771 views
  • 15 comments
  • 117 respects

Arduino - Web Oscilloscope (Support Trigger)

Project tutorial by phpoc_man

  • 5,766 views
  • 13 comments
  • 50 respects

Controlling Arduino State Machine via Web

Project tutorial by phpoc_man

  • 1,951 views
  • 4 comments
  • 22 respects

Arduino - Control Arm Robot via Web

Project tutorial by phpoc_man

  • 10,564 views
  • 17 comments
  • 118 respects
Add projectSign up / Login