Skip to content

Commit 14b0ed4

Browse files
author
Daily
committed
Controling DC Motor with buttons on a web page
1 parent a4716bb commit 14b0ed4

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

example09.html

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE html>
2+
<meta charset = utf8>
3+
<html>
4+
<head>
5+
<title>Example with graph</title>
6+
</head>
7+
8+
<body>
9+
10+
PWM: <input id="pwm" value=100 />
11+
<button id="buttonSendPWM" onClick="sendPWM()">Set PWM</button>
12+
<button id="buttonLeft" onClick="left()">Left</button>
13+
<button id="buttonRight" onClick="right()">Right</button>
14+
<button id="buttonStop" onClick="stop()">Stop</button>
15+
16+
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
17+
18+
<script type="text/javascript">
19+
var socket = io.connect("192.168.1.134:8080");
20+
function sendPWM () {
21+
valuePWM = document.getElementById("pwm").value;
22+
socket.emit("sendPWM", valuePWM);
23+
}
24+
function left () {
25+
socket.emit("left", 1);
26+
}
27+
function right() {
28+
socket.emit("right", 0);
29+
}
30+
function stop() {
31+
socket.emit("stop", 0);
32+
}
33+
</script>
34+
35+
36+
</body>
37+
38+
</html>

example09.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
var http = require("http").createServer(handler);
2+
var firmata = require("firmata");
3+
var fs = require("fs");
4+
var io = require("socket.io").listen(http);
5+
6+
console.log("Connecting to Arduino");
7+
8+
var board = new firmata.Board("/dev/ttyACM0", function(){
9+
console.log("Activation of pin 2");
10+
board.pinMode(2, board.MODES.OUTPUT);
11+
console.log("Activation of pin 3");
12+
board.pinMode(3, board.MODES.PWM);
13+
});
14+
15+
function handler(req, res) {
16+
fs.readFile(__dirname + "/example09.html",
17+
function(err, data) {
18+
if (err) {
19+
res.writeHead(500, {"Content-Type": "text/plain"});
20+
return res.end("You didn't connect html file!");
21+
}
22+
res.writeHead(200);
23+
res.end(data);
24+
});
25+
}
26+
27+
http.listen(8080);
28+
29+
var desiredValue = 0;
30+
31+
console.log("Running the system");
32+
33+
board.on("ready", function(){
34+
console.log("Board is ready!");
35+
board.analogRead(0, function(value){
36+
desiredValue = value;
37+
});
38+
39+
io.sockets.on("connection", function(socket){
40+
41+
socket.on("sendPWM", function(pwm){
42+
board.analogWrite(3,pwm);
43+
console.log("PWM value:" + pwm);
44+
});
45+
46+
socket.on("left", function(value) {
47+
board.digitalWrite(2,value);
48+
});
49+
50+
socket.on("right", function(value) {
51+
board.digitalWrite(2,value);
52+
});
53+
54+
socket.on("stop", function(value) {
55+
board.analogWrite(3,value);
56+
});
57+
58+
});
59+
60+
});

0 commit comments

Comments
 (0)