-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample10.html
80 lines (66 loc) · 2.4 KB
/
example10.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<!DOCTYPE html>
<meta charset = utf8>
<html>
<head>
<title>Example with DC Motor and potentiometer</title>
</head>
<body>
<button id="buttonLeft" onClick="left()">Left</button>
<button id="buttonRight" onClick="right()">Right</button>
<button id="buttonControl" onClick="control()">Start</button>
<div id="print1"></div>
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
<script type="text/javascript">
var socket = io.connect("192.168.1.134:8080");
function sendPWM (speed) {
//valuePWM = document.getElementById("pwm").value;
socket.emit("sendPWM", speed/4); //max value of potentionmeter is 1024. Max value of the speed is 255.
}
function left () {
socket.emit("left", 1);
}
function right() {
socket.emit("right", 0);
}
function control() {
if(!isStoped){
isStoped = true;
socket.emit("stop", 0);
var button = document.getElementById("buttonControl");
button.innerHTML = "Start";
} else {
var button = document.getElementById("buttonControl");
isStoped = false;
button.innerHTML = "Stop";
}
}
var potValue1 = 0; // value of the first potentiometer
var divElement = document.getElementById("print1"); // variable for div object where the values will be printed (logged)
var numberOfLinesBeforeScroll = 20;
var linesPrintCounter = 0;
var isStoped = true;
function log(msg) {
var node=document.createElement("tr"); // we create the variable node as the a table row (tr)
var textnode=document.createTextNode(linesPrintCounter + " | " + msg); // we create element with the text adding the counter
node.appendChild(textnode); // adding text to "node", i.e. table row
divElement.insertBefore(node, divElement.childNodes[0]); // inserting into variable node
if (linesPrintCounter > numberOfLinesBeforeScroll-1) { // if the lines are more than limit -> start with scroll
divElement.removeChild(divElement.childNodes[numberOfLinesBeforeScroll]); // we remove the oldest printout
}
linesPrintCounter++; // increasing the number of printouts
}
socket.on("messageToClient", function (msg){
log(msg); // add msg
});
socket.on("clientReadValues", function(value) {
if(!isStoped){
sendPWM(value.desiredValue);
log(value.desiredValue);
}
});
socket.on("disconnect", function(){
log("Disconnected from the server"); // we print status of disconn. to div
});
</script>
</body>
</html>