-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathArduinoPlaysTimberman.ino
More file actions
78 lines (64 loc) · 1.65 KB
/
ArduinoPlaysTimberman.ino
File metadata and controls
78 lines (64 loc) · 1.65 KB
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
/*
Arduino Plays Timberman
A simple programm for Arduino that plays Timberman.
Circuit:
* JZC-11F RELAYS attached to TIP120 attached to pins 4 and 8
* OP580DA PHOTODARLINGTON NPN attached to A0
* BUTTON attached to pin 7
created July 23th 1014
by Valentin Heun
*/
int left = 4; int right = 8;
int button = 7; int timing = 23;
int Buffer[] = {0, 0, 0, 0, 0};
int startstate = 0;
void setup() {
pinMode(left, OUTPUT); pinMode(right, OUTPUT);
pinMode(button, INPUT); digitalWrite(button, HIGH);
}
void loop() {
//realise the axe
digitalWrite(left, LOW); digitalWrite(right, LOW);
// update the buffer
if (startstate > 0) {
for (int i = 3; i > 0; i--) {
Buffer[i] = Buffer[i - 1];
}
// read a couple of frames and determan if a branch had crossed the sensor
Buffer[0] = 0;
for (int i = 0; i <= timing; i++) {
delay(4);
//delayMicroseconds(200);
int analog = analogRead(A0);
if ((analog > startstate + 5) || (analog < startstate - 5)) {
Buffer[0] = 1;
}
}
}
// set start value for the sensor
if (digitalRead(button) == HIGH) {
if (startstate == 0) {
startstate = analogRead(A0);
}
// timberman cutts around the right side of the branches.
int theswitch = Buffer[1];
if (Buffer[2] == 1 && Buffer[1] == 0) {
theswitch = 1;
}
// make the actuall cut
if (theswitch == 0) {
digitalWrite(left, HIGH);
}
else {
digitalWrite(right, HIGH);
}
delay(38);
}
else {
// if switched off, set everything back to zero
startstate = 0;
for (int i = 0; i <= 4; i++) {
Buffer[i] = 0;
}
}
}