03.12.2015 Views

laravel4cookbook

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Embedded Systems 302<br />

25 var parts = data.split(",");<br />

26<br />

27 if (parts[0] == "analogRead") {<br />

28 sensor.val(parseInt(parts[2], 10));<br />

29 }<br />

30<br />

31 });<br />

32<br />

33 window.socket = socket;<br />

34<br />

35 led.on("change", function() {<br />

36 socket.send("analogWrite,6," + led.val() + ".");<br />

37 });<br />

38<br />

39 x.on("change", function() {<br />

40 socket.send("servoWrite,5," + x.val() + ".");<br />

41 });<br />

42<br />

43 y.on("change", function() {<br />

44 socket.send("analogWrite,3," + y.val() + ".");<br />

45 });<br />

46<br />

47 setInterval(function() {<br />

48 socket.send("analogRead,0,void.");<br />

49 }, 1000);<br />

50<br />

51 }<br />

52 } catch (e) {<br />

53 // console.log("exception: " + e);<br />

54 }<br />

.<br />

This was extracted from public/js/shared.js.<br />

This script does a number of cool things. When it loads, we set the pinMode of the LED pin (6) and<br />

the two servo pins (3, 5) to the correct modes. We also attach a message event listener to receive and<br />

parse messages from the Arduino.<br />

We then attach some event listeners for the input elements in the interface. These will respond to<br />

changes in value by sending signals to the Arduino to adjust its various pin values.<br />

Finally, we set a timer to ping the Arduino with analogRead requests on the sensor pin. Thanks to

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!