-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUseless_box.ino
155 lines (121 loc) · 4.61 KB
/
Useless_box.ino
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Useless Box v1.0
//
// Kai Laborenz 2017
//
// original by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.
// include library to operate servos
#include <Servo.h>
// const and variables definitions
const int buttonPin = 2;
int buttonState = 0;
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos; // variable to store the servo position
long timeDelay; // variable to store random delay times for servo operation
int counter = 0; // counter for counting the switch uses
int counterSwitch = 0;
void setup()
{
pinMode(buttonPin, INPUT);
myservo.attach(9); // attaches servo on pin 9 to the servo object
Serial.begin(9600); // initialize serial communication at 9600 bits per second:
}
void loop()
{
timeDelay = 0;
buttonState = digitalRead(buttonPin);
switch (counter) {
case 4:
if (buttonState == HIGH) { // not switched
if (counterSwitch == 0) {
counter = counter + 1; // advance counter
counterSwitch = 1;
Serial.println(counter);
}
for(pos = myservo.read(); pos >=20; pos -= 1) {
myservo.write(pos);
}
}
else { // switched
// if (counter % 2 == 0) { timeDelay = 50; } // every second round goes slower
for(pos = myservo.read(); pos <=90; pos += 1) {
myservo.write(pos);
delay(50);
}
counterSwitch = 0;
}
break;
case 6:
if (buttonState == HIGH) { // not switched
if (counterSwitch == 0) {
counter = counter + 1; // advance counter
counterSwitch = 1;
Serial.println(counter);
}
for(pos = myservo.read(); pos >=20; pos -= 1) {
myservo.write(pos);
}
}
else { // switched
for(pos = myservo.read(); pos <=65; pos += 1) {
myservo.write(pos);
delay(50);
}
delay(1000);
myservo.write(90);
counterSwitch = 0;
}
break;
case 1:
if (buttonState == HIGH) { // not switched
if (counterSwitch == 0) {
counter = counter + 1; // advance counter
counterSwitch = 1;
Serial.println(counter);
}
for(pos = myservo.read(); pos >=20; pos -= 1) {
myservo.write(pos);
}
}
else { // switched
delay(1000);
for(pos = myservo.read(); pos <=88; pos += 1) {
myservo.write(pos);
delay(2);
}
delay(500);
for(pos = myservo.read(); pos >=50; pos -= 1) {
myservo.write(pos);
}
delay(1500);
for(pos = myservo.read(); pos <=85; pos += 1) {
myservo.write(pos);
delay(50);
}
delay(1500);
counterSwitch = 0;
}
break;
default:
if (buttonState == HIGH) { // not switched
if (counterSwitch == 0) {
counter = counter + 1; // advance counter
counterSwitch = 1;
Serial.println(counter);
}
for(pos = myservo.read(); pos >=20; pos -= 1) {
myservo.write(pos);
}
}
else { // switched
// if (counter % 2 == 0) { timeDelay = 50; } // every second round goes slower
for(pos = myservo.read(); pos <=90; pos += 1) {
myservo.write(pos);
delay(timeDelay);
}
counterSwitch = 0;
}
break; // end switch loop
}
} // end main loop