質問編集履歴
1
自動整形を適応
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,162 +3,148 @@
|
|
3
3
|
このプログラムをGRSAKURAで動かしたいと考えています
|
4
4
|
|
5
5
|
|
6
|
-
/* Arduino Based - 7Seg Display Clock (4 Digits HH : MM)
|
6
|
+
"/* Arduino Based - 7Seg Display Clock (4 Digits HH : MM)
|
7
|
-
|
7
|
+
Can be activated via sound through the microphone
|
8
|
-
|
8
|
+
Dev: Michalis Vasilakis // Date: 30/12/2015 // License: BY-NC-SA
|
9
|
-
|
9
|
+
More info soon at www.ardumotive.com
|
10
|
-
//Library
|
10
|
+
//Library
|
11
|
-
#include "SevenSeg.h"
|
12
|
-
#include <DS1302.h>
|
13
|
-
|
14
|
-
//Seven Seg and DS1302 init
|
15
|
-
SevenSeg disp (11,12,8,7,6,10,9); //Defines the segments A-G: SevenSeg(A, B, C, D, E, F, G);
|
16
|
-
const int numOfDigits =4; //Number of 7 segments
|
17
|
-
int digitPins [numOfDigits]={3,4,5,13}; //CC(or CA) pins of segment
|
18
|
-
DS1302 rtc(0,1,2); // Init the DS1302
|
19
|
-
Time t; // Init a Time-data structure
|
20
|
-
|
21
|
-
//Constants
|
22
|
-
const int selectBT = A0; //Select - Set button at pin A0
|
23
|
-
const int changeBT = A1; //Change button at pin A1
|
24
|
-
const int showBT = A2; //Show time button at pin A2
|
25
|
-
const int mic = A3; //Microphone at pin A3
|
26
|
-
const int leds= A4; //Leds to seperate time from min HH : MM
|
27
|
-
|
28
|
-
//Variables
|
29
|
-
int hour;
|
30
|
-
int min;
|
31
|
-
String clock;
|
32
|
-
int number;
|
33
|
-
int count=0;
|
34
|
-
unsigned long cc=0;
|
35
|
-
int sound;
|
36
|
-
int flag=0;
|
37
|
-
int flag1=0;
|
38
|
-
|
39
|
-
|
40
|
-
void setup() {
|
41
|
-
|
42
|
-
//Define Pins as inputs or outputs:
|
43
|
-
pinMode(selectBT, INPUT_PULLUP);
|
44
|
-
pinMode(changeBT, INPUT_PULLUP);
|
45
|
-
pinMode(showBT, INPUT_PULLUP);
|
46
|
-
pinMode(min, INPUT);
|
47
|
-
pinMode(leds, OUTPUT);
|
48
|
-
|
49
|
-
// Set the clock to run-mode, and disable the write protection
|
50
|
-
rtc.halt(false);
|
51
|
-
rtc.writeProtect(false);
|
52
|
-
|
53
11
|
|
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
|
-
hour++;
|
106
|
-
}
|
107
|
-
}
|
108
|
-
if (count == 2){
|
109
|
-
if (min>58){
|
110
|
-
min=0;
|
111
|
-
}
|
112
|
-
else{
|
113
|
-
min++;
|
114
|
-
}
|
115
|
-
}
|
116
|
-
|
117
|
-
|
118
|
-
}
|
119
|
-
|
12
|
+
include "SevenSeg.h"
|
13
|
+
|
14
|
+
include <DS1302.h>
|
15
|
+
|
16
|
+
//Seven Seg and DS1302 init
|
17
|
+
SevenSeg disp (11,12,8,7,6,10,9); //Defines the segments A-G: SevenSeg(A, B, C, D, E, F, G);
|
18
|
+
const int numOfDigits =4; //Number of 7 segments
|
19
|
+
int digitPins [numOfDigits]={3,4,5,13}; //CC(or CA) pins of segment
|
20
|
+
DS1302 rtc(0,1,2); // Init the DS1302
|
21
|
+
Time t; // Init a Time-data structure
|
22
|
+
|
23
|
+
//Constants
|
24
|
+
const int selectBT = A0; //Select - Set button at pin A0
|
25
|
+
const int changeBT = A1; //Change button at pin A1
|
26
|
+
const int showBT = A2; //Show time button at pin A2
|
27
|
+
const int mic = A3; //Microphone at pin A3
|
28
|
+
const int leds= A4; //Leds to seperate time from min HH : MM
|
29
|
+
|
30
|
+
//Variables
|
31
|
+
int hour;
|
32
|
+
int min;
|
33
|
+
String clock;
|
34
|
+
int number;
|
35
|
+
int count=0;
|
36
|
+
unsigned long cc=0;
|
37
|
+
int sound;
|
38
|
+
int flag=0;
|
39
|
+
int flag1=0;
|
40
|
+
|
41
|
+
void setup() {
|
42
|
+
|
43
|
+
//Define Pins as inputs or outputs:
|
44
|
+
pinMode(selectBT, INPUT_PULLUP);
|
45
|
+
pinMode(changeBT, INPUT_PULLUP);
|
46
|
+
pinMode(showBT, INPUT_PULLUP);
|
47
|
+
pinMode(min, INPUT);
|
48
|
+
pinMode(leds, OUTPUT);
|
49
|
+
|
50
|
+
// Set the clock to run-mode, and disable the write protection
|
51
|
+
rtc.halt(false);
|
52
|
+
rtc.writeProtect(false);
|
53
|
+
|
54
|
+
//Defines the number of digits to be "numOfDigits" and the digit pins to be the elements of the array "digitPins"
|
55
|
+
disp.setDigitPins ( numOfDigits , digitPins );
|
56
|
+
//Only for common cathode 7segments
|
57
|
+
disp.setCommonCathode();
|
58
|
+
//Control brightness (values 0-100);
|
59
|
+
disp.setDutyCycle(70);
|
60
|
+
disp.setTimer (2);
|
61
|
+
disp.startTimer ();
|
62
|
+
|
120
63
|
}
|
64
|
+
|
65
|
+
void loop()
|
66
|
+
{
|
67
|
+
sound = analogRead(mic);
|
68
|
+
|
69
|
+
if ((hour<=9) && (min>9)){
|
70
|
+
clock = '0' + String(hour) + String(min);
|
71
|
+
|
72
|
+
}
|
73
|
+
else if ((min<=9) && (hour>9)){
|
74
|
+
clock = String(hour) + '0' + String(min);
|
75
|
+
}
|
76
|
+
else if ((hour<=9) && (min<=9)){
|
77
|
+
clock = '0' + String(hour) + '0' + String(min);
|
78
|
+
}
|
79
|
+
else{
|
80
|
+
clock = String(hour) + String(min);
|
81
|
+
}
|
82
|
+
|
83
|
+
number = clock.toInt();
|
84
|
+
|
85
|
+
if (digitalRead(selectBT) == LOW){
|
86
|
+
|
87
|
+
delay(300);
|
88
|
+
count++;
|
89
|
+
if (count>2){
|
90
|
+
count=0;
|
91
|
+
rtc.setTime(hour, min, 0); // Set the hour to 12:00:00 (24hr format)
|
92
|
+
}
|
93
|
+
}
|
94
|
+
|
95
|
+
if (count>0){
|
96
|
+
if (digitalRead(changeBT) == LOW){
|
97
|
+
delay(300);
|
98
|
+
if (count == 1){
|
99
|
+
if (hour>22){
|
100
|
+
hour=0;
|
101
|
+
}
|
102
|
+
else{
|
103
|
+
hour++;
|
104
|
+
}
|
105
|
+
}
|
106
|
+
if (count == 2){
|
107
|
+
if (min>58){
|
108
|
+
min=0;
|
109
|
+
}
|
110
|
+
else{
|
111
|
+
min++;
|
112
|
+
}
|
113
|
+
}
|
114
|
+
}
|
115
|
+
}
|
116
|
+
else{
|
117
|
+
// Get data from the DS1302
|
118
|
+
t = rtc.getTime();
|
119
|
+
hour = t.hour;
|
120
|
+
min = t.min;
|
121
|
+
}
|
122
|
+
if ((digitalRead(showBT) == LOW) || (sound<445 || sound>485)){
|
123
|
+
delay(500);
|
124
|
+
//...ones, turn led on!
|
125
|
+
if ( flag == 0){
|
126
|
+
digitalWrite(leds, HIGH);
|
127
|
+
flag=1; //change flag variable
|
128
|
+
}
|
129
|
+
//...twice, turn led off!
|
130
|
+
else if ( flag == 1){
|
131
|
+
flag=0; //change flag variable again
|
132
|
+
digitalWrite(leds, LOW);
|
133
|
+
hideTime();
|
134
|
+
}
|
135
|
+
}
|
136
|
+
if (flag == 1){
|
137
|
+
showTime();
|
138
|
+
}
|
121
139
|
else{
|
122
|
-
// Get data from the DS1302
|
123
|
-
t = rtc.getTime();
|
124
|
-
hour = t.hour;
|
125
|
-
min = t.min;
|
126
140
|
}
|
127
|
-
|
128
|
-
|
129
|
-
if ((digitalRead(showBT) == LOW) || (sound<445 || sound>485)){
|
130
|
-
delay(500);
|
131
|
-
//...ones, turn led on!
|
132
|
-
if ( flag == 0){
|
133
|
-
digitalWrite(leds, HIGH);
|
134
|
-
|
135
|
-
flag=1; //change flag variable
|
136
|
-
|
141
|
+
}
|
137
|
-
//...twice, turn led off!
|
138
|
-
else if ( flag == 1){
|
139
|
-
flag=0; //change flag variable again
|
140
|
-
digitalWrite(leds, LOW);
|
141
|
-
|
142
|
-
|
142
|
+
void showTime(){
|
143
|
+
disp.write(clock);
|
143
|
-
|
144
|
+
}
|
145
|
+
void hideTime(){
|
146
|
+
disp.write(" ");
|
144
|
-
|
147
|
+
}
|
145
|
-
if (flag == 1){
|
146
|
-
showTime();
|
147
|
-
}
|
148
|
-
else{
|
149
|
-
|
150
|
-
}
|
151
|
-
|
152
|
-
}
|
153
|
-
|
154
|
-
void showTime(){
|
155
|
-
disp.write(clock);
|
156
|
-
}
|
157
|
-
|
158
|
-
void hideTime(){
|
159
|
-
disp.write(" ");
|
160
|
-
}
|
161
|
-
|
162
|
-
ISR( TIMER2_COMPA_vect ){
|
148
|
+
ISR( TIMER2_COMPA_vect ){
|
163
|
-
disp.interruptAction ();
|
149
|
+
disp.interruptAction ();
|
164
|
-
}
|
150
|
+
}"
|