-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegment.ino
More file actions
138 lines (124 loc) · 3.01 KB
/
segment.ino
File metadata and controls
138 lines (124 loc) · 3.01 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
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
/*
Showing number 0-9 on a Common Anode 7-segment LED display
Displays the numbers 0-9 on the display, with one second inbetween.
A
---
F | | B
| G |
---
E | | C
| |
---
D
This example code is in the public domain.
*/
// Pin 2-8 is connected to the 7 segments of the display.
#include "pitches.h"
#include "digits.h"
#define outputA 15
#define outputB 16
#define BUTTON_PIN 17
#define SPEAKER_PIN 18
#define NOTE_TIME 100
int current_time;
int prev_time;
int button_prev_time;
int timer = 0;
int counter = 0;
int rotary_state;
int rotary_last_state;
int button_state = true;
int button_last_state = true;
int timer_started = false;
int notes[] = {NOTE_C4, NOTE_C4, NOTE_B5};
int current_note = -1;
int max_rings = 10;
int current_rings = 0;
int note_interval;
void setup() {
Serial.begin(9600);
setup_digits();
current_time = millis();
prev_time = millis();
button_prev_time = millis();
pinMode (outputA,INPUT);
pinMode (outputB,INPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(SPEAKER_PIN,OUTPUT);
rotary_last_state = digitalRead(outputA);
button_last_state = digitalRead(BUTTON_PIN);
note_interval = NOTE_TIME;
}
// the loop routine runs over and over again forever:
void loop() {
current_time = millis();
// Rotary code
if (current_note == -1) {
rotary_state = digitalRead(outputA);
if (rotary_state != rotary_last_state) {
if ((current_time - prev_time) >= 100) {
prev_time = current_time;
if (digitalRead(outputB) != rotary_state) {
timer++;
} else {
timer--;
}
}
rotary_last_state = rotary_state;
}
}
display_number(timer);
// Display code
if (timer_started) {
if ((timer >= 0) && ((current_time - prev_time) >= 1000)) {
if (timer == 0) {
current_note = 0;
timer_started = 0;
} else {
prev_time = current_time;
}
timer--;
}
display_number(timer);
}
// Button code
button_state = digitalRead(BUTTON_PIN);
if (button_state == LOW && button_last_state == HIGH && millis() - button_prev_time > 500) {
if (timer > 0) {
timer_started = !timer_started;
}
if (current_note >= 0) {
current_note = -1;
current_rings = 0;
}
button_prev_time = millis();
}
button_last_state = button_state;
// Speaker code
if (current_note >= 0) {
Serial.println("00");
if ((current_time - prev_time) >= note_interval) {
Serial.println("01");
if (current_rings >= max_rings) {
Serial.println("02");
current_note = -1;
current_rings = 0;
noTone(SPEAKER_PIN);
} else {
Serial.println("03");
tone(SPEAKER_PIN, notes[current_note], NOTE_TIME);
current_note++;
if (current_note >= 3) {
current_rings++;
Serial.println("04");
note_interval = 1000;
current_note = 0;
} else {
Serial.println("05");
note_interval = NOTE_TIME;
}
prev_time = current_time;
}
}
}
}