-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathexperiment15.js
101 lines (90 loc) · 2.34 KB
/
experiment15.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// how detailed our picture will be
var WIDTH = 48;
var HEIGHT = 48;
var PIXELS = WIDTH*HEIGHT;
// The position in our scan
var px=0, py=0;
// Our pixel data
var data = new Float32Array(PIXELS);
function readPixel() {
var light = analogRead(A5);
// work out where in the array it should go
var idx = px + (py*WIDTH);
// save the data away
data[idx] = light;
}
function updateServos() {
readPixel();
/* Bring px and py into the
right range for the servo motors */
var x = ((px/WIDTH) - 0.5) / 3;
var y = ((py/HEIGHT) - 0.5) / 3;
// And move the servos
digitalPulse(B3, 1, E.clip(1.5+x, 1, 2));
digitalPulse(B3, 1, 0);
digitalPulse(B4, 1, E.clip(1.5+y, 1, 2));
/* Move to the next position. Go right */
px++;
// or if we're at the end of the line,
// go back to the start
if (px>=WIDTH) {
px=0;
py++;
}
if (py>=HEIGHT) {
/* If we got to the end, don't do anything
else. stop calling updateServos */
clearInterval(scanInterval);
}
}
var scanInterval = setInterval(updateServos, 20);
// Draw our pixels out to the screen
function draw() {
/* We have to use characters to represent
each shade of color, so we're putting some
characters in a string that get progressively
more 'dense' */
var shades = " .:;*@#";
/* Work out the maximum and minimum
values, so we can scale the image
brightness properly */
var min = data[0];
var max = data[0];
data.forEach(function(pixel) {
if (pixel < min) min = pixel;
if (pixel > max) max = pixel;
});
// Now we can print the data out, line by line
var n = 0;
for (var y=0;y<HEIGHT;y++) {
var str = "";
for (var x=0;x<WIDTH;x++) {
var light = (data[n]-min)/(max-min);
var shade = Math.floor(light*shades.length);
str += shades[shade];
n++;
}
console.log(str);
}
}
function getData() {
var min = data[0];
var max = data[0];
data.forEach(function(pixel) {
if (pixel < min) min = pixel;
if (pixel > max) max = pixel;
});
// Print the data out, line by line
var n = 0;
for (var y=0;y<HEIGHT;y++) {
var str = "";
for (var x=0;x<WIDTH;x++) {
var light = Math.round((data[n]-min)*255/(max-min));
str += light+",";
n++;
}
console.log("["+str+"],");
}
}
// You can now call draw() to draw the picture as ascii
// or getData() to output the data as an array