//***********************************************************************// // Arduino Binary Clock Demonstation w/ Seconds // // // // Keith Cameron, 2010 // // // // An open-source binary clock for Arduino. // // Based on the code from Daniel Andrade (http://www.danielandrade.net) // // Based on the code from by Rob Faludi (http://www.faludi.com) // // Code under (cc) by Daniel Spillere Andrade, www.danielandrade.net // // http://creativecommons.org/license/cc-gpl // // // // Shift register setup from Carlyn Maw, Tom Igoe, 25 Oct, 2006 // // shiftOutCode, Predefined Array Style // // // //***********************************************************************// // ================SECONDS SHIFT REGISTER================= // Pin connected to ST_CP of 74HC595 int latchPin = 16; // Pin connected to SH_CP of 74HC595 int clockPin = 17; // Pin connected to DS of 74HC595 int dataPin = 15; // holders for infromation you're going to pass to shifting function byte data; byte dataArray[60]; // holds info on whether or not the clock has been set. By default it has not. boolean clockRecentlySet = 0; // secondCount used for initializing int secondCount = 0; int hexSeconds; // ================MINUTES/HOURS INIT===================== // Declare all the pin integers // Hour Hand Line 1: 3, 2 // Hour Hand Line 2: 7, 6, 5, 4 const int h_ledPin[6] = { 3,2,7,6,5,4 }; // Minute Hand Line 1: 10, 9, 8 // Minute Hand Line 2: 11, 12, 16, 7 const int m_ledPin[7] = { 10,9,8,11,12,13,14 }; // create start time at 00:00:00 int second=0, minute=0, hour=0; // create general counters int minute_unit, hour_unit, value_minute=0, value_hour=0, ledstats, i; // declare hour button, minute button int hourButton = 4; int minuteButton = 5; // =======================================================// // SETUP // // =======================================================// void setup() { // ================SECONDS SHIFT REGISTER ================ // set up the clockPin,dataPin and latchPin to OUTPUT MODE to indicate it will // be sending information. pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT); pinMode(latchPin, OUTPUT); // set up the hourButton and minuteButton to INPUT MODE to indicate it will // be waiting for information. pinMode(hourButton, INPUT); pinMode(minuteButton, INPUT); // initialize the serial connection to utilize the RX/TX features for pc diagnosis Serial.begin(9600); Serial.print("initializing"); // 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. // dataArray[0] = 0x3c; //bin 0011 1100, int 60. DELMITED due to dynamic array /* for (hexSeconds = 0x00; hexSeconds < 0x3c; hexSeconds++) { dataArray[secondCount] = hexSeconds; secondCount++; } */ dataArray[0] = 0x00; dataArray[1] = 0x01; dataArray[2] = 0x02; dataArray[3] = 0x03; dataArray[4] = 0x04; dataArray[5] = 0x05; dataArray[6] = 0x06; dataArray[7] = 0x07; dataArray[8] = 0x08; dataArray[9] = 0x09; dataArray[10] = 0x10; dataArray[11] = 0x11; dataArray[12] = 0x12; dataArray[13] = 0x13; dataArray[14] = 0x14; dataArray[15] = 0x15; dataArray[16] = 0x16; dataArray[17] = 0x17; dataArray[18] = 0x18; dataArray[19] = 0x19; dataArray[20] = 0x20; dataArray[21] = 0x21; dataArray[22] = 0x22; dataArray[23] = 0x23; dataArray[24] = 0x24; dataArray[25] = 0x25; dataArray[26] = 0x26; dataArray[27] = 0x27; dataArray[28] = 0x28; dataArray[29] = 0x29; dataArray[30] = 0x30; dataArray[31] = 0x31; dataArray[32] = 0x32; dataArray[33] = 0x33; dataArray[34] = 0x34; dataArray[35] = 0x35; dataArray[36] = 0x36; dataArray[37] = 0x37; dataArray[38] = 0x38; dataArray[39] = 0x39; dataArray[40] = 0x40; dataArray[41] = 0x41; dataArray[42] = 0x42; dataArray[43] = 0x43; dataArray[44] = 0x44; dataArray[45] = 0x45; dataArray[46] = 0x46; dataArray[47] = 0x47; dataArray[48] = 0x48; dataArray[49] = 0x49; dataArray[50] = 0x50; dataArray[51] = 0x51; dataArray[52] = 0x52; dataArray[53] = 0x53; dataArray[54] = 0x54; dataArray[55] = 0x55; dataArray[56] = 0x56; dataArray[57] = 0x57; dataArray[58] = 0x58; dataArray[59] = 0x59; //function that blinks all the LEDs //gets passed the number of blinks and the pause time blinkAll( 6, 500 ); // ================MINUTES/HOURS INIT===================== for (int thisPin = 0; thisPin < 7; thisPin++) { // initialize the output pins: pinMode(m_ledPin[thisPin], OUTPUT); // take the col pins (i.e. the cathodes) high to ensure that // the LEDs are off: digitalWrite(m_ledPin[thisPin], HIGH); } for (int thisPin = 0; thisPin < 6; thisPin++) { // initialize the output pins: pinMode(h_ledPin[thisPin], OUTPUT); // take the col pins (i.e. the cathodes) high to ensure that // the LEDs are off: digitalWrite(h_ledPin[thisPin], HIGH); } Serial.println(" |"); } // end setup(); // =======================================================// // MAIN LOOP // // =======================================================// void loop() { // set up a local variable to hold the last time we moved forward one second. // static variables are initialized once and keep their values between function calls. static unsigned long lastTick = 0; // move forward one second every 1000 milliseconds if (millis() - lastTick >= 1000) { lastTick = millis(); second++; Serial.print(hour); Serial.print(":"); Serial.print(minute); Serial.print(":"); Serial.println(second); } // move forward one minute every 60 seconds if (second >= 60) { minute++; second = 0; // reset seconds to zero } // move forward one hour every 60 minutes if (minute >= 60) { hour++; minute = 0; // reset minutes to zero } // reset hour back to 0 ever 24 hours if (hour >= 24) { hour=0; // reset hours to zero minute = 0; // reset minutes to zero.. This appears repetitive (deliminate?) } // sets the variable minute_unit and hour_unit for the unit digits minute_unit = minute%10; hour_unit = hour%10; //------------------ // Setting the clock //------------------ // add one minute/hour when pressed. Utilizing analog read to increase inputs. value_minute = analogRead(minuteButton); value_hour = analogRead(hourButton); // Due to use of analog read, you have to read input data as voltage. // So, if value_minute reads any kind of voltage decrease (800 is .8v), then perform the following statement. if(value_hour < 800) { hour++; delay(300); //reset to 0 while changing the hours second = 0; // create boolean emphasizing that the clock was recently set clockRecentlySet = 1; } if(value_minute < 800) { minute++; delay(300); //reset to 0 while changing the minutes second = 0; // create boolean emphasizing that the clock was recently set clockRecentlySet = 1; } if (clockRecentlySet == 1) { // reset the status of the Clock Set indicator clockRecentlySet = 0; //reset to 0 while changing the minutes second = 0; } //------------------- // Lighting the LED's //------------------- //-------------------------- // SECONDS //-------------------------- //load the light sequence you want from array setSeconds(second); //-------------------------- // MINUTES //-------------------------- // returning minute data to the LEDs (1's) if(minute_unit == 1 || minute_unit == 3 || minute_unit == 5 || minute_unit == 7 || minute_unit == 9) { digitalWrite(m_ledPin[3], HIGH); } else { digitalWrite(m_ledPin[3],LOW); } if(minute_unit == 2 || minute_unit == 3 || minute_unit == 6 || minute_unit == 7) { digitalWrite(m_ledPin[4], HIGH); } else { digitalWrite(m_ledPin[4],LOW); } if(minute_unit == 4 || minute_unit == 5 || minute_unit == 6 || minute_unit == 7) { digitalWrite(m_ledPin[5], HIGH); } else { digitalWrite(m_ledPin[5],LOW); } if(minute_unit == 8 || minute_unit == 9) { digitalWrite(m_ledPin[6], HIGH); } else { digitalWrite(m_ledPin[6],LOW); } //returning minute data to the LEDs (10's) if((minute >= 10 && minute < 20) || (minute >= 30 && minute < 40) || (minute >= 50 && minute < 60)) { digitalWrite(m_ledPin[0], HIGH); } else { digitalWrite(m_ledPin[0],LOW); } if(minute >= 20 && minute < 40) { digitalWrite(m_ledPin[1], HIGH); } else { digitalWrite(m_ledPin[1],LOW); } if(minute >= 40 && minute < 60) { digitalWrite(m_ledPin[2], HIGH); } else { digitalWrite(m_ledPin[2],LOW); } //-------------------------- // HOURS //-------------------------- // returning hour data to the LEDs (1's) if(hour_unit == 1 || hour_unit == 3 || hour_unit == 5 || hour_unit == 7 || hour_unit == 9) { digitalWrite(h_ledPin[2], HIGH); } else { digitalWrite(h_ledPin[2],LOW); } if(hour_unit == 2 || hour_unit == 3 || hour_unit == 6 || hour_unit == 7) { digitalWrite(h_ledPin[3], HIGH); } else { digitalWrite(h_ledPin[3],LOW); } if(hour_unit == 4 || hour_unit == 5 || hour_unit == 6 || hour_unit == 7) { digitalWrite(h_ledPin[4], HIGH); } else { digitalWrite(h_ledPin[4],LOW); } if(hour_unit == 8 || hour_unit == 9) { digitalWrite(h_ledPin[5], HIGH); } else { digitalWrite(h_ledPin[5],LOW); } //returning hour data to the LEDs (10's) if(hour >= 10 && hour < 20) { digitalWrite(h_ledPin[0], HIGH); } else { digitalWrite(h_ledPin[0],LOW); } if(hour >= 20 && hour < 24) { digitalWrite(h_ledPin[1], HIGH); } else { digitalWrite(h_ledPin[1],LOW); } } // end void loop(); // =======================================================// // SECONDS: SHIFT OUT // // // // The primary second counter. // // =======================================================// 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<