Got a 8segment display ordered from ebay
const int slaveSelectPin = 10;
int num = 0;
void setup() {
Serial.begin(57600); //
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
//Serial.println("Goodnight moon!");
// set the slaveSelectPin as an output:
//pinMode (slaveSelectPin, OUTPUT);
// initialize SPI:
pinMode (slaveSelectPin, OUTPUT);
SPI.begin();
initMax();
}
void pulseCS(void) {
digitalWrite(slaveSelectPin, 1);
delay(1);
digitalWrite(slaveSelectPin, 0);
}
void initMax()
{
digitalWrite(slaveSelectPin, 0);
SPI.transfer(0x09);//Decode Mode
// SPI.transfer(0xFF);//BCD decode for all digits
SPI.transfer(0x00); // No decode
pulseCS();
SPI.transfer(0x0B);//Scan Limit
SPI.transfer(0x07);
pulseCS();
SPI.transfer(0x0A);//Intensity
SPI.transfer(0x01);
pulseCS();
for(int i=1; i<=8; i++) {
SPI.transfer(i);
SPI.transfer(0x00);
pulseCS();
}
SPI.transfer(0x0C);
SPI.transfer(0x01);
pulseCS();
//Serial.println("Display Init");
//numDigits(7);
}
int numDigits(int i)
{
int count = 1;
while(1)
{
i = i /10;
if (i != 0)
count ++;
else
break;
}
Serial.print("Num Digits :");
Serial.println(count);
return count;
}
void displayNum(int i)
{
for(int i=1; i<=8; i++) {
SPI.transfer(i);
SPI.transfer(0x00);
pulseCS();
}
//Find how many digits
int numDig = numDigits(i);
int pos = 1;
while (1)
{
int k = i % 10;
SPI.transfer(pos);
switch(k)
{
case 0:
SPI.transfer(B01111110);
pulseCS();
break;
case 1:
SPI.transfer(B00110000);
pulseCS();
break;
case 2:
SPI.transfer(B01101101);
pulseCS();
break;
case 3:
SPI.transfer(B01111001);
pulseCS();
break;
case 4:
SPI.transfer(B00110011);
pulseCS();
break;
case 5:
SPI.transfer(B01011011);
pulseCS();
break;
case 6:
SPI.transfer(B01011111);
pulseCS();
break;
case 7:
SPI.transfer(B01110000);
pulseCS();
break;
case 8:
SPI.transfer(B01111111);
pulseCS();
break;
case 9:
SPI.transfer(B01111011);
pulseCS();
break;
}
if (pos == numDig)
break;
pos ++;
i = i/10;
}
}
void loop() {
if (num <= 100)
{
displayNum(num);
delay(1000);
num++;
}
else
{
num = 0;
}
//SPI.transfer(1);
//SPI.transfer(0x09);
//pulseCS();
//delay(1000);
//SPI.transfer(2);
//SPI.transfer(0x08);
//pulseCS();
}