I have created a clock with 74HC595 and four 7seg display and would like to power it digispark and make it available trough USB communication to set time and other tasks.
A first i have made a simple code to drive HC595's thats worked flawless after that included USB library to use and communicate with it, but it's just doesnt work maybe some timing issue or anything else? Using P0, P1, P2 for driving HC595's so P2 and P3 on digispark free for USB data communication.
Tryed switching on / off innterupts in writeRegisters() call but no sucess. So when first line commented out nothing works.
Any idea about this issue are welcome, thanks!
//#include <DigiUSB.h>
int SER_Pin = 0;
int RCLK_Pin = 2;
int SRCLK_Pin = 1;
#define number_of_74hc595s 4
#define numOfRegisterPins number_of_74hc595s * 8
boolean registers[numOfRegisterPins];
void setup()
{
pinMode(SER_Pin, OUTPUT);
pinMode(RCLK_Pin, OUTPUT);
pinMode(SRCLK_Pin, OUTPUT);
}
void clearRegisters()
{
for(int i = numOfRegisterPins - 1; i >= 0; i--) {
registers[i] = HIGH;
}
}
void writeRegisters()
{
digitalWrite(RCLK_Pin, LOW);
for(int i = numOfRegisterPins - 1; i >= 0; i--) {
digitalWrite(SRCLK_Pin, LOW);
int val = registers[i];
digitalWrite(SER_Pin, val);
digitalWrite(SRCLK_Pin, HIGH);
}
digitalWrite(RCLK_Pin, HIGH);
}
void setRegisterPin(int index, int value)
{
registers[index] = value;
}
void number(int seg, int num)
{
int segments[11][7] = {
//c,d,e,b,a,f,g
{1,1,1,1,1,1,0}, // 0
{1,0,0,1,0,0,0}, // 1
{0,1,1,1,1,0,1}, // 2
{1,1,0,1,1,0,1}, // 3
{1,0,0,1,0,1,1}, // 4
{1,1,0,0,1,1,1}, // 5
{1,1,1,0,1,1,1}, // 6
{1,0,0,1,1,0,0}, // 7
{1,1,1,1,1,1,1}, // 8
{1,0,0,1,1,1,1}, // 9
{1,1,1,1,1,1,1} // off
};
setRegisterPin(1 + seg, !segments[num][0]);
setRegisterPin(2 + seg, !segments[num][1]);
setRegisterPin(3 + seg, !segments[num][2]);
setRegisterPin(4 + seg, !segments[num][3]);
setRegisterPin(5 + seg, !segments[num][4]);
setRegisterPin(6 + seg, !segments[num][5]);
setRegisterPin(7 + seg, !segments[num][6]);
}
int c = 0;
void loop()
{
clearRegisters();
writeRegisters();
if (c > 9999) {
c = 0;
}
int num = c;
number(0, num % 10);
num /= 10;
number(8, num % 10);
num /= 10;
number(16, num % 10);
num /= 10;
number(24, num % 10);
writeRegisters();
delay(100);
}