-
Notifications
You must be signed in to change notification settings - Fork 0
/
bazen.js
103 lines (81 loc) · 2.48 KB
/
bazen.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
102
// wiringpi start
var wiringpi = require('wiringpi-node');
function setUpPWMOut() {
// # use 'GPIO naming'
wiringpi.setup('gpio');
// # set #18 to be a PWM output
wiringpi.pinMode(18, wiringpi.PWM_OUTPUT);
// # set the PWM mode to milliseconds stype
wiringpi.pwmSetMode(wiringpi.PWM_MODE_MS);
// # divide down clock
wiringpi.pwmSetClock(192);
wiringpi.pwmSetRange(2000);
}
// change pin mode to input - turn off servo
function turnOffPWM() {
wiringpi.pinMode(18, wiringpi.INPUT);
}
var MIN_ANGLE = 80;
// var MAX_ANGLE = 260; // not currently used, anyway this is max angle value
var ANGLE_TO_PUSH = 150;
var DELAY_PERIOD = 20;
const WAIT_BEFORE_START = 5000;
const PUSH_LENGTH = 2000;
const SHUTDOWN_TIMER = 60000;
var currentAngle = MIN_ANGLE;
function writeNumber(angle) {
wiringpi.pwmWrite(18, angle);
currentAngle = angle;
console.log(angle);
}
function delayedWrite(currentAngle, desiredAngle, positive) {
// break if desiredAngle has been reached
if (positive && (currentAngle >= desiredAngle)) return;
if (!positive && (currentAngle <= desiredAngle)) return;
setTimeout(function () {
if (positive)
currentAngle++;
else
currentAngle--;
// console.log(currentAngle);
writeNumber(currentAngle);
// call next() recursively
delayedWrite(currentAngle, desiredAngle, positive);
}, DELAY_PERIOD);
}
function writeNumberSlow(angle) {
setUpPWMOut();
if (angle > currentAngle) {
delayedWrite(currentAngle, angle, true, wiringpi);
} else {
delayedWrite(currentAngle, angle, false, wiringpi);
}
setTimeout(turnOffPWM, DELAY_PERIOD * 200 + 1000);
}
// wiringpi end
// Require child_process
const exec = require('child_process').exec;
// Create shutdown function
function shutdown() {
console.log(`Shutting down`)
exec('shutdown +1')
}
function pushButton(pushToON) {
if (pushToON) {
writeNumberSlow(ANGLE_TO_PUSH);
console.log();
console.log('pushing button to ON, angle: \t' + ANGLE_TO_PUSH);
} else {
writeNumberSlow(MIN_ANGLE);
console.log();
console.log('pushing button to OFF, angle: \t' + MIN_ANGLE);
}
}
function main() {
setUpPWMOut();
writeNumber(MIN_ANGLE);
setTimeout(() => pushButton(true), WAIT_BEFORE_START);
setTimeout(() => pushButton(false), WAIT_BEFORE_START + PUSH_LENGTH);
setTimeout(() => shutdown(), SHUTDOWN_TIMER);
}
main();