Lab_interaccio/2010/age_of_fiction/age_of_fiction.pde

323 lines
6.4 KiB
Plaintext
Raw Normal View History

2025-02-25 21:29:42 +01:00
#define BAUD 115200
//Pin connected to ST_CP of 74HC595
int latchPin = 13;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;
byte min1=0;
byte min0=0;
byte hora1=0;
byte hora0=0;
byte dia1=0;
byte dia0=0;
byte mes1=0;
byte mes0=0;
byte year_a0=0;
byte year_a1=0;
byte year_b1=0;
byte year_b0=0;
byte minuto=0;
byte hora=0;
byte dia=1;
byte mes=1;
byte year_a=0;
byte year_b=0;
int year=2012;
byte time=60;
byte res=1;
byte ready = true;
byte display0[10]= {119,65,59,107,77,110,126,67,127,111};
ISR(TIMER2_OVF_vect) {
sei(); //Reenable global interrupts, otherwise serial commands will get dropped
if( Serial.available() > 7 ) {
if (Serial.read()==0xF0){
year_a=Serial.read();
year_b=Serial.read();
year=year_a*100+year_b;
mes=Serial.read();
dia=Serial.read();
hora=Serial.read();
minuto=Serial.read();
min1=minuto/10;
min0=minuto%10;
time=Serial.read();
res=Serial.read();
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, display0[dia1]);
shiftOut(dataPin, clockPin, display0[dia0]);
shiftOut(dataPin, clockPin, display0[mes1]);
shiftOut(dataPin, clockPin, display0[mes0]);
shiftOut(dataPin, clockPin, display0[year_a1]);
shiftOut(dataPin, clockPin, display0[year_a0]);
shiftOut(dataPin, clockPin, display0[year_b1]);
shiftOut(dataPin, clockPin, display0[year_b0]);
shiftOut(dataPin, clockPin, display0[hora1]);
shiftOut(dataPin, clockPin, display0[hora0]);
shiftOut(dataPin, clockPin, display0[min1]);
shiftOut(dataPin, clockPin, display0[min0]);
digitalWrite(latchPin, 1);
//Serial.print("Minutos: ");
//minuto=(min1*10)+(min0);
//Serial.print(minuto);
//Serial.print(" Tiempo ficcion: ");
//Serial.print(time);
}
}
}
void setup() {
//Start Serial for debuging purposes
Serial.begin(BAUD);
//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
TCCR2A = 0;
TCCR2B = 0<<CS22 | 1<<CS21 | 1<<CS20;
//Timer2 Overflow Interrupt Enable
TIMSK2 = 1<<TOIE2;
year_a=year/100;
year_b=year%100;
}
void loop() {
//count up routine
for (min0 = 0; min0 < 10; min0++) {
//ground latchPin and hold low for as long as you are transmitting
hora1=hora/10;
hora0=hora%10;
dia1=dia/10;
dia0=dia%10;
mes1=mes/10;
mes0=mes%10;
year_a1=year_a/10;
year_a0=year_a%10;
year_b1=year_b/10;
year_b0=year_b%10;
digitalWrite(latchPin, 0);
//count up on GREEN LEDs
shiftOut(dataPin, clockPin, display0[dia1]);
shiftOut(dataPin, clockPin, display0[dia0]);
shiftOut(dataPin, clockPin, display0[mes1]);
shiftOut(dataPin, clockPin, display0[mes0]);
shiftOut(dataPin, clockPin, display0[year_a1]);
shiftOut(dataPin, clockPin, display0[year_a0]);
shiftOut(dataPin, clockPin, display0[year_b1]);
shiftOut(dataPin, clockPin, display0[year_b0]);
shiftOut(dataPin, clockPin, display0[hora1]);
shiftOut(dataPin, clockPin, display0[hora0]);
shiftOut(dataPin, clockPin, display0[min1]);
shiftOut(dataPin, clockPin, display0[min0]);
//count down on RED LEDs
//shiftOut(dataPin, clockPin, 255-j);
//return the latch pin high to signal chip that it
//no longer needs to listen for information
digitalWrite(latchPin, 1);
//Serial.print("Minutos: ");
Serial.print(0xF0,BYTE);
Serial.print(year_a);
Serial.print(year_b);
Serial.print(mes);
Serial.print(dia);
Serial.print(hora);
minuto=(min1*10)+(min0);
Serial.print(minuto);
//Serial.print(" Tiempo ficcion: ");
Serial.print(time);
for (int i= 1; i <= time; i++) delay(res*10);
}
min1++;
if (min1==6)
{
min1=0;
hora=hora+1;
}
if (hora==24)
{
hora=0;
dia=dia+1;
}
switch (mes)
{
case 1:
if (dia==32)
{
dia=1;
mes=mes+1;
}
break;
case 2:
if (dia==29)
{
dia=1;
mes=mes+1;
}
break;
case 3:
if (dia==32)
{
dia=1;
mes=mes+1;
}
break;
case 4:
if (dia==31)
{
dia=1;
mes=mes+1;
}
break;
case 5:
if (dia==32)
{
dia=1;
mes=mes+1;
}
break;
case 6:
if (dia==31)
{
dia=1;
mes=mes+1;
}
break;
case 7:
if (dia==32)
{
dia=1;
mes=mes+1;
}
break;
case 8:
if (dia==32)
{
dia=1;
mes=mes+1;
}
break;
case 9:
if (dia==31)
{
dia=1;
mes=mes+1;
}
break;
case 10:
if (dia==32)
{
dia=1;
mes=mes+1;
}
break;
case 11:
if (dia==31)
{
dia=1;
mes=mes+1;
}
break;
case 12:
if (dia==32)
{
dia=1;
mes=mes+1;
}
break;
}
if (mes>12)
{
mes=1;
year=year+1;
year_a=year/100;
year_b=year%100;
}
}
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);
}