-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8e1d9fe
Showing
2 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
const int signalPin = 0; | ||
const int sampleInterval = 10; // milliseconds | ||
const int ledPin = 13; | ||
|
||
int secondIndex = -1; | ||
byte bits[59]; // being lazy | ||
int lastValue; | ||
unsigned long lowStart = 0; | ||
unsigned long highStart = 0; | ||
int ledState = LOW; | ||
|
||
int readSignalPin() { | ||
return analogRead(signalPin) > 400 ? LOW : HIGH; | ||
} | ||
|
||
boolean sampleSignal() { | ||
boolean result = false; | ||
int value = readSignalPin(); | ||
|
||
if (lastValue == HIGH) { | ||
if (value == LOW) { // new second starts | ||
Serial.println("new second starts"); | ||
unsigned long time = millis(); // FIXME: need to handle overflow | ||
|
||
lowStart = time; | ||
|
||
if (highStart > 0) { | ||
unsigned long highDuration = time - highStart; | ||
|
||
if (highDuration > 1500) { // new minute starts | ||
Serial.println("new minute starts"); | ||
secondIndex = 0; | ||
} | ||
} | ||
|
||
if (ledState == LOW) { | ||
ledState = HIGH; | ||
} else { | ||
ledState = LOW; | ||
} | ||
|
||
digitalWrite(ledPin, ledState); | ||
} | ||
} | ||
else { // lastValue == LOW | ||
if (value == HIGH) { | ||
unsigned long time = millis(); // FIXME: need to handle overflow | ||
|
||
highStart = time; | ||
|
||
if (lowStart > 0 && secondIndex >= 0 && secondIndex <= 58) { | ||
unsigned long lowDuration = time - lowStart; | ||
|
||
if (lowDuration < 150) { // bit is 0 | ||
Serial.println("bit is 0"); | ||
bits[secondIndex] = 0; | ||
} | ||
else { // bit is 1 | ||
Serial.println("bit is 1"); | ||
bits[secondIndex] = 1; | ||
} | ||
|
||
++secondIndex; | ||
|
||
if (secondIndex == 59) { | ||
result = true; | ||
} | ||
} | ||
} | ||
} | ||
|
||
lastValue = value; | ||
|
||
return result; | ||
} | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
Serial.println("setup"); | ||
|
||
pinMode(ledPin, OUTPUT); | ||
|
||
lastValue = readSignalPin(); | ||
} | ||
|
||
void loop() { | ||
delay(sampleInterval); // FIXME: need to account for the actual time it takes to do the sampling | ||
|
||
if (sampleSignal()) { | ||
Serial.print("full: "); | ||
|
||
for (int i = 0; i < 59; ++i) { | ||
Serial.print(bits[i]); | ||
Serial.print(" "); | ||
} | ||
|
||
Serial.println(""); | ||
|
||
int day = bits[41] * 20 + bits[40] * 10 + bits[39] * 8 + bits[38] * 4 + bits[37] * 2 + bits[36] * 1; | ||
int month = bits[49] * 10 + bits[48] * 8 + bits[47] * 4 + bits[46] * 2 + bits[45] * 1; | ||
int year = bits[57] * 80 + bits[56] * 40 + bits[55] * 20 + bits[54] * 10 + bits[53] * 8 + bits[52] * 4 + bits[51] * 2 + bits[50] * 1; | ||
int hour = bits[34] * 20 + bits[33] * 10 + bits[32] * 8 + bits[31] * 4 + bits[30] * 2 + bits[29] * 1; | ||
int minute = bits[27] * 40 + bits[26] * 20 + bits[25] * 10 + bits[24] * 8 + bits[23] * 4 + bits[22] * 2 + bits[21] * 1; | ||
|
||
Serial.print(day); | ||
Serial.print("."); | ||
|
||
if (month < 10) { | ||
Serial.print(0); | ||
} | ||
|
||
Serial.print(month); | ||
Serial.print("."); | ||
|
||
Serial.print("20"); | ||
Serial.print(year); | ||
Serial.print(" "); | ||
|
||
Serial.print(hour); | ||
Serial.print(":"); | ||
|
||
if (minute < 10) { | ||
Serial.print(0); | ||
} | ||
|
||
Serial.print(minute); | ||
Serial.println(""); | ||
} | ||
} |