I'm working on a simple class of my own.. I haven't gotten to creating the library yet as I still wish to add multiplexing support.
I'm using it to display a simple time stamp right now. I know my code isn't as concise as a good programmer, or done correctly. I am a beginner trying to get into software driven electronics.
My issue is occasionally the screen displays some crazy things. Segments light up in a fashion that's not defined in the code. Since I wasn't thinking and started with pin0 I can't necessarily troubleshoot via serial.
Does anyone see any major discrepancies in my code?
int s, m, h;
int time[4];
void setup()
{
for(int i = 0; i < 7; i++)
{
pinMode(i, OUTPUT);
digitalWrite(i, HIGH);
}
pinMode(12, OUTPUT);
digitalWrite(12, HIGH);
pinMode(13, INPUT);
digitalWrite(13, LOW);
}
// 0 1 2 3 4 5 6 6 8 9
const byte numDef[10]= {64, 121, 36, 48, 25, 18, 2, 120, 0, 24};
void loop()
{
int buttonState = digitalRead(13);
if (buttonState == HIGH)
{displayTime();}
static unsigned long count = 0;
if (millis() - count >= 1000)
{count = millis(); s++;}
if (s > 59)
{m++; s = 0;}
if (m > 59)
{h++; m = 0;}
if (h > 23)
{h = 0;}
}
void displayTime()
{
time[0] = h/10;
time[1] = h - time[0]*10;
time[2] = m/10;
time[3] = m - time[0]*10;
for (int i = 0; i < 4; i++)
{
setSegments(time[i]);
}
}
void setSegments(int num)
{
byte segments = numDef[num];
for (int s = 0; s < 7; s++)
{
int bitVal = bitRead(segments, s);
digitalWrite(s, bitVal);
}
delay(250);
clearSegments();
delay(50);
}
void clearSegments()
{
for (int i = 0; i < 7; i++)
{
digitalWrite(i, HIGH);
}
}