-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.ino
110 lines (86 loc) · 1.68 KB
/
clock.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
#define AUTO A0
#define CLOCKLED 5
#define BUTTON_PIN 7
#define RESET_PIN 9
// Initialize the speed to OFF
int speed = 0;
int oldSpeed = 0;
int steps = 0;
void sleep(int sleep_time)
{
long start_time = millis();
while(millis() < start_time + sleep_time)
{
// do nothing
}
}
// Tick the clock
void toggle_clock()
{
digitalWrite(CLOCKLED, HIGH);
sleep(10);
digitalWrite(CLOCKLED, LOW);
sleep(10);
}
// Single increment of the clock
void single_step()
{
toggle_clock();
Serial.print("STEP ");
Serial.println(steps);
steps++;
sleep(500);
}
void setup() {
// set up the pins and the event listeners
pinMode(CLOCKLED, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
pinMode(AUTO, INPUT);
pinMode(RESET_PIN, OUTPUT);
// initialize serial communication
Serial.begin(115200);
// Output the status of the buttons/switches
Serial.print("POT: ");
Serial.println(analogRead(AUTO));
Serial.print("STEP: ");
Serial.println(digitalRead(BUTTON_PIN));
// Reset the computer
digitalWrite(RESET_PIN, LOW);
single_step();
single_step();
single_step();
single_step();
single_step();
digitalWrite(RESET_PIN, HIGH);
}
void loop()
{
// What is the pot set to?
speed = analogRead(AUTO);
if(abs(speed-oldSpeed)>30) {
oldSpeed = speed;
if(speed > 1000)
{
// Manual stepping listener
Serial.println("AUTO OFF");
}
else
{
// Set the speed to the trim pot
Serial.print("POT: ");
Serial.println(speed);
}
}
else
{
if(speed > 1000) {
if(digitalRead(BUTTON_PIN))
{
single_step();
}
} else {
sleep(speed);
toggle_clock();
}
}
}