352 lines
8.8 KiB
C++
352 lines
8.8 KiB
C++
|
|
/*
|
|
- RECEIVE COMMAND: 'c' 'l' 'o' 'c' 'k' minute second
|
|
- AFTER VALID COMMAND THE SYSTEM UPDATE THE NEW TIME AND ACTIVATE THE COUNTER
|
|
- AFTER COUNT WAIT FOR THE NEXT VALID COMMAND
|
|
*/
|
|
|
|
//Pin connected to ST_CP of 74HC595
|
|
#include "WProgram.h"
|
|
void setup();
|
|
void loop();
|
|
void start_counter();
|
|
void clockOutput();
|
|
void shiftOut(int myDataPin, int myClockPin, byte myDataOut);
|
|
void blinkAll_4Bytes(int n, int d);
|
|
void black_out();
|
|
void blink_black_out(int n, int d);
|
|
int latchPin = 8;
|
|
//Pin connected to SH_CP of 74HC595
|
|
int clockPin = 12;
|
|
////Pin connected to DS of 74HC595
|
|
int dataPin = 11;
|
|
|
|
int second=59, minute=14;
|
|
int stop_counter = 0;
|
|
int bytesread = 0;
|
|
|
|
int clock_pin = 6;
|
|
int clock_gnd = 7;
|
|
|
|
//holders for infromation you're going to pass to shifting function
|
|
byte data;
|
|
byte dataArrayseg1[10];
|
|
byte dataArrayseg2[10];
|
|
byte dataArrayseg3[10];
|
|
byte dataArrayseg4[10];
|
|
byte puntos[10];
|
|
|
|
|
|
void setup() {
|
|
//set pins to output because they are addressed in the main loop
|
|
pinMode(latchPin, OUTPUT);
|
|
pinMode(clock_pin, INPUT);
|
|
pinMode(clock_gnd, OUTPUT);
|
|
Serial.begin(57600);
|
|
|
|
digitalWrite(clock_gnd, LOW);
|
|
|
|
//Arduino doesn't seem to have a way to write binary straight into the code
|
|
//so these values are in HEX. Decimal would have been fine, too.
|
|
dataArrayseg1[0] = B01101111;
|
|
dataArrayseg1[1] = B00000011;
|
|
dataArrayseg1[2] = B01011101;
|
|
dataArrayseg1[3] = B01010111;
|
|
dataArrayseg1[4] = B00110011;
|
|
dataArrayseg1[5] = B01110110;
|
|
dataArrayseg1[6] = B01111110;
|
|
dataArrayseg1[7] = B01000011;
|
|
dataArrayseg1[8] = B01111111;
|
|
dataArrayseg1[9] = B01110111;
|
|
|
|
//Arduino doesn't seem to have a way to write binary straight into the code
|
|
//so these values are in HEX. Decimal would have been fine, too.
|
|
|
|
puntos[0] = B00000000;
|
|
puntos[1] = B00000001;
|
|
puntos[2] = B00000000;
|
|
puntos[3] = B00000001;
|
|
puntos[4] = B00000000;
|
|
puntos[5] = B00000001;
|
|
puntos[6] = B00000000;
|
|
puntos[7] = B00000001;
|
|
puntos[8] = B00000000;
|
|
puntos[9] = B00000001;
|
|
|
|
|
|
dataArrayseg2[0] = B11011110;
|
|
dataArrayseg2[1] = B00000110;
|
|
dataArrayseg2[2] = B10111010;
|
|
dataArrayseg2[3] = B10101110;
|
|
dataArrayseg2[4] = B01100110;
|
|
dataArrayseg2[5] = B11101100;
|
|
dataArrayseg2[6] = B11111100;
|
|
dataArrayseg2[7] = B10000110;
|
|
dataArrayseg2[8] = B11111110;
|
|
dataArrayseg2[9] = B11101110;
|
|
|
|
dataArrayseg3[0] = B01111011;
|
|
dataArrayseg3[1] = B01100000;
|
|
dataArrayseg3[2] = B01011101;
|
|
dataArrayseg3[3] = B01110101;
|
|
dataArrayseg3[4] = B01100110;
|
|
dataArrayseg3[5] = B00110111;
|
|
dataArrayseg3[6] = B00111111;
|
|
dataArrayseg3[7] = B01100001;
|
|
dataArrayseg3[8] = B01111111;
|
|
dataArrayseg3[9] = B01110111;
|
|
|
|
dataArrayseg4[0] = B01111011;
|
|
dataArrayseg4[1] = B01100000;
|
|
dataArrayseg4[2] = B01011101;
|
|
dataArrayseg4[3] = B01110101;
|
|
dataArrayseg4[4] = B01100110;
|
|
dataArrayseg4[5] = B00110111;
|
|
dataArrayseg4[6] = B00111111;
|
|
dataArrayseg4[7] = B01100001;
|
|
dataArrayseg4[8] = B01111111;
|
|
dataArrayseg4[9] = B01110111;
|
|
|
|
blinkAll_4Bytes(2,500);
|
|
|
|
}
|
|
|
|
void loop() {
|
|
|
|
/*
|
|
// Serial input
|
|
if(Serial.available() > 0) {
|
|
if(Serial.read() == 'c') { // check for header
|
|
bytesread = 0;
|
|
Serial.println("detectada c");
|
|
while (bytesread < 6) { // read c lock minute second
|
|
if( Serial.available() > 0) {
|
|
int incomingByte = Serial.read();
|
|
if( (incomingByte == 'l') && (bytesread == 0) )
|
|
{
|
|
bytesread = 1;
|
|
Serial.println("detectada l");
|
|
}
|
|
else if( (incomingByte == 'o') && (bytesread == 1) )
|
|
{
|
|
bytesread = 2;
|
|
Serial.println("detectada o");
|
|
}
|
|
else if( (incomingByte == 'c') && (bytesread == 2) )
|
|
{
|
|
bytesread = 3;
|
|
Serial.println("detectada c");
|
|
}
|
|
else if( (incomingByte == 'k') && (bytesread == 3) )
|
|
{
|
|
bytesread = 4;
|
|
Serial.println("detectada k");
|
|
}
|
|
else if( bytesread == 4 )
|
|
{
|
|
minute = incomingByte;
|
|
bytesread = 5;
|
|
Serial.print("detectado minute ");
|
|
Serial.println(minute);
|
|
}
|
|
else if( bytesread == 5 )
|
|
{
|
|
second = incomingByte;
|
|
bytesread = 6;
|
|
Serial.print("detectado second ");
|
|
Serial.println(second);
|
|
stop_counter = 0;
|
|
}
|
|
else
|
|
bytesread = 6;
|
|
}
|
|
}
|
|
}
|
|
Serial.flush();
|
|
}
|
|
*/
|
|
|
|
if (digitalRead(clock_pin))
|
|
{
|
|
stop_counter = 0;
|
|
second = 44;
|
|
minute = 7;
|
|
}
|
|
|
|
static unsigned long lastTick = 0; // set up a local variable to hold the last time we moved forward one second
|
|
// move forward one second every 1000 milliseconds
|
|
if ( (millis() - lastTick >= 1000) && stop_counter == 0 ) {
|
|
lastTick = millis();
|
|
clockOutput();
|
|
//Serial.print("second ");
|
|
//Serial.println(second);
|
|
second--;
|
|
}
|
|
|
|
// move forward one minute every 60 seconds
|
|
if (second < 0) {
|
|
minute--;
|
|
second = 59; // reset seconds to zero
|
|
//Serial.print("minut ");
|
|
//Serial.println(minute);
|
|
}
|
|
|
|
// move forward one hour every 60 minutes
|
|
if (minute < 0) {
|
|
stop_counter = 1;
|
|
//Serial.println("FINAL DE CUENTA");
|
|
minute = 0; // reset minutes to zero
|
|
if(stop_counter) {
|
|
blink_black_out(5,1000);
|
|
}
|
|
}
|
|
}
|
|
|
|
void start_counter()
|
|
{
|
|
int second=59, minute=14;
|
|
Serial.flush();
|
|
}
|
|
|
|
|
|
void clockOutput()
|
|
{
|
|
int sec_unit, sec_dec;
|
|
int min_unit, min_dec;
|
|
sec_unit = second%10;
|
|
sec_dec = second/10;
|
|
min_unit = minute%10;
|
|
min_dec = minute/10;
|
|
|
|
digitalWrite(latchPin, 0);
|
|
shiftOut(dataPin, clockPin, dataArrayseg1[min_dec]);
|
|
data=dataArrayseg2[min_unit]|puntos[second%2];
|
|
shiftOut(dataPin, clockPin, data);
|
|
shiftOut(dataPin, clockPin, dataArrayseg3[sec_dec]);
|
|
shiftOut(dataPin, clockPin, dataArrayseg4[sec_unit]);
|
|
digitalWrite(latchPin, 1);
|
|
|
|
}
|
|
|
|
// the heart of the program
|
|
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
|
|
// This shifts 8 bits out MSB first,
|
|
//on the rising edge of the clock,
|
|
//clock idles low
|
|
|
|
//internal function setup
|
|
int i=0;
|
|
int pinState;
|
|
pinMode(myClockPin, OUTPUT);
|
|
pinMode(myDataPin, OUTPUT);
|
|
|
|
//clear everything out just in case to
|
|
//prepare shift register for bit shifting
|
|
digitalWrite(myDataPin, 0);
|
|
digitalWrite(myClockPin, 0);
|
|
|
|
//for each bit in the byte myDataOut?
|
|
//NOTICE THAT WE ARE COUNTING DOWN in our for loop
|
|
//This means that %00000001 or "1" will go through such
|
|
//that it will be pin Q0 that lights.
|
|
for (i=7; i>=0; i--) {
|
|
digitalWrite(myClockPin, 0);
|
|
|
|
//if the value passed to myDataOut and a bitmask result
|
|
// true then... so if we are at i=6 and our value is
|
|
// %11010100 it would the code compares it to %01000000
|
|
// and proceeds to set pinState to 1.
|
|
if ( myDataOut & (1<<i) ) {
|
|
pinState= 1;
|
|
}
|
|
else {
|
|
pinState= 0;
|
|
}
|
|
|
|
//Sets the pin to HIGH or LOW depending on pinState
|
|
digitalWrite(myDataPin, pinState);
|
|
//register shifts bits on upstroke of clock pin
|
|
digitalWrite(myClockPin, 1);
|
|
//zero the data pin after shift to prevent bleed through
|
|
digitalWrite(myDataPin, 0);
|
|
}
|
|
|
|
//stop shifting
|
|
digitalWrite(myClockPin, 0);
|
|
}
|
|
|
|
|
|
//blinks the whole register based on the number of times you want to
|
|
//blink "n" and the pause between them "d"
|
|
//starts with a moment of darkness to make sure the first blink
|
|
//has its full visual effect.
|
|
void blinkAll_4Bytes(int n, int d) {
|
|
for (int x = 0; x < n; x++) {
|
|
digitalWrite(latchPin, 0);
|
|
shiftOut(dataPin, clockPin, 255);
|
|
shiftOut(dataPin, clockPin, 255);
|
|
shiftOut(dataPin, clockPin, 255);
|
|
shiftOut(dataPin, clockPin, 255);
|
|
digitalWrite(latchPin, 1);
|
|
delay(d);
|
|
digitalWrite(latchPin, 0);
|
|
shiftOut(dataPin, clockPin, 0);
|
|
shiftOut(dataPin, clockPin, 0);
|
|
shiftOut(dataPin, clockPin, 0);
|
|
shiftOut(dataPin, clockPin, 0);
|
|
digitalWrite(latchPin, 1);
|
|
delay(d);
|
|
}
|
|
}
|
|
|
|
void black_out() {
|
|
|
|
digitalWrite(latchPin, 0);
|
|
shiftOut(dataPin, clockPin, 0);
|
|
shiftOut(dataPin, clockPin, 0);
|
|
shiftOut(dataPin, clockPin, 0);
|
|
shiftOut(dataPin, clockPin, 0);
|
|
digitalWrite(latchPin, 1);
|
|
|
|
}
|
|
|
|
void blink_black_out(int n, int d) {
|
|
for (int x = 0; x < n; x++) {
|
|
digitalWrite(latchPin, 0);
|
|
shiftOut(dataPin, clockPin, dataArrayseg1[0]);
|
|
shiftOut(dataPin, clockPin, dataArrayseg2[0]);
|
|
shiftOut(dataPin, clockPin, dataArrayseg3[0]);
|
|
shiftOut(dataPin, clockPin, dataArrayseg4[0]);
|
|
digitalWrite(latchPin, 1);
|
|
delay(d);
|
|
digitalWrite(latchPin, 0);
|
|
shiftOut(dataPin, clockPin, 0);
|
|
shiftOut(dataPin, clockPin, 0);
|
|
shiftOut(dataPin, clockPin, 0);
|
|
shiftOut(dataPin, clockPin, 0);
|
|
digitalWrite(latchPin, 1);
|
|
delay(d);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(void)
|
|
{
|
|
init();
|
|
|
|
setup();
|
|
|
|
for (;;)
|
|
loop();
|
|
|
|
return 0;
|
|
}
|
|
|