OK, I made some more changes and got "better" results. I guess while copying and pasting from file to file I misplaced part of the card that dealt with the actual calculation of how to display the digits. I put it in and did a couple of other things and now I get '0000' initially and the 5 minute increment button works but the others don't. In response to the schematic for the timer, I don't have anyway of putting one up as of right now. I can't take a picture and I don't know of a program I could use to make one.
Also, I am currently using a voltage divider setup for the buttons (i.e. a resistor from the Vs to one side of the button and a resistor on the other side going to ground with the connection back to analog pin A0 from that side. the resistor to ground is 10k)
Here is my new code.
// Count down timer starting at a user inputted time designated by the pushing
// of 5 or 15 minute incrementing buttons. There is also a start/stop button
// and a reset button.
// List of digit select lines, least significant digit first
const unsigned char SegmentPins[] = {8,7,6,5,4,3,2}; // {A,B,C,D,E,F,G} Cathodes (HIGH for on, LOW for off)
const unsigned char DigitPins[] = {9, 10, 11, 12}; // {1,2,3,4} Anodes (LOW for on, HIGH for off)
// Button setup
int analogpin = A0; // select analog input pin to detect button push
int ButtonPushed = 0; // variable to store button push value
int startbutton = 0; // variable to store start button push to begin countdown
int stopbutton = 0; // I have not implemented the stop sequence as I am unsure
// as to how to do it.
// Bit maps for the seven segment display
const unsigned char Segments[] =
{ //dot,G,F,E,D,C,B,A
0b11000000, // 0
0b11001111, // 1
0b10100100, // 2
0b10000110, // 3
0b10001011, // 4
0b10010010, // 5
0b10010000, // 6
0b11000111, // 7
0b10000000, // 8
0b10000011, // 9
};
void setup()
{
for (int i=0; i<4; i++)
{
pinMode(DigitPins[i],OUTPUT);
digitalWrite(DigitPins[i],HIGH);
}
for (int i=0; i<8; i++)
{
pinMode(SegmentPins[i],OUTPUT);
digitalWrite(SegmentPins[i],LOW);
}
}
void loop()
{
// variables used for time initialization
int initTime = 0;
do
{
ButtonPushed = analogRead(analogpin);
// Increment Time 5 minutes
if (ButtonPushed > 500 && ButtonPushed < 600)
{
initTime += 5;
delay (200);
}
// Increment Time 15 minutes
else if (ButtonPushed > 250 && ButtonPushed < 400)
{
initTime += 15;
delay (200);
}
// Start/Stop
else if (ButtonPushed > 125 && ButtonPushed < 250)
{
startbutton = 1;
delay (200);
}
// Reset
else if (ButtonPushed > 50 && ButtonPushed < 125)
{
delay (200);
return;
}
// calculate time according to user input
unsigned long hundredths = (initTime*60000) / 10;
unsigned long seconds = hundredths / 100;
unsigned long minutes = seconds / 60;
int hours = minutes / 60;
// Display initTime while user is inputting.
WriteDigit(hundredths, seconds, minutes, hours);
}while (startbutton == 0);
// Start counting down when Start/Stop button is pushed
int currentmillis = millis(); // used to initialize the time for countdown
// calculate time according to user input
unsigned long hundredths = ((initTime*60000+currentmillis)-millis()) / 10;
unsigned long seconds = hundredths / 100;
unsigned long minutes = seconds / 60;
int hours = minutes / 60;
// Display countdown on 7-Segment
WriteDigit(hundredths, seconds, minutes, hours);
}
void WriteDigit(unsigned long hund, unsigned long sec, unsigned long minu, int hrs)
{
int clock;
// Display minutes:seconds up to 100 minutes, then hours/minutes
if (sec < 100)
clock = (sec % 100) * 100 + (hund % 100);
else if (minu < 100)
clock = (minu % 100) * 100 + (sec % 60);
else
clock = (hrs % 100) * 100 + (minu % 60);
// Clear all segments before enabling a digit
for (int s=0; s<8; s++)
{
digitalWrite(SegmentPins[s],HIGH);
}
// Display each digit, right to left
for (int i=0; i<4; i++)
{
// Peel a digit off the low end of the number
int digit = clock % 10;
clock /= 10;
// Display the digit on the seven segments
unsigned char segments = Segments[digit];
for (int s=0; s<8; s++)
{
digitalWrite(SegmentPins[s], segments & 1);
segments >>= 1;
}
// Turn on the digit briefly
digitalWrite(DigitPins[i], HIGH); // Select one digit
delay(3);
digitalWrite(DigitPins[i], LOW);
}
}