testing Arduino and web comunication

I’m using Andy Sygler’s example for Node.js and Arduino (Uno or Yun), to test and understand the concept of sending serial data from browser, through the server to Arduino. LED is lighting with random color when the mouse is pressed; and when the mouse is released, browser should get the color information and change the color to be the same as LED a second ago (when the mouse was pressed).

.

.

There are only two events in the browser window, with only two values to be forwarded (0 or 1) to Arduino:

window.onmousedown = function(){
socket.send(1);
}

and

window.onmouseup = function(){
socket.send(0);
}

.

Key concept here is that the server is only making connection between the to and passing the values; Nothing more complicated than that.

Browser is listening for the 2 mouse events;

and Arduino is waiting for one of the two possible values, picks the random color, telling the server what did he pick, turns of the LED and waits again.

 

void loop() {
if(Serial.available()){

char inByte = Serial.read();

// mousedown event
if (inByte==’1′) {

// make a new color, and turn the LED on
r = random(255);
g = random(255);
b = random(255);

analogWrite(3,r);
analogWrite(5,g);
analogWrite(6,b);
}

// mouseup event
else if ( inByte == ‘0’) {

// send the color to the browser, then turn the LED off
Serial.print(r);
Serial.print(‘,’);
Serial.print(g);
Serial.print(‘,’);
Serial.println(b);

analogWrite(3,0);
analogWrite(5,0);
analogWrite(6,0);
}
}
}

Advertisements

Comments are closed.

%d bloggers like this: