I'm attempting to drive a 4 digit 7-segment LED using a MAX7219 shift register with the code below and schematic from 7.12 of the Arduino Cookbook. It calls for the Max7221 but I thought I'd try it with a Max7219 (I'm new to this but I hope it doesn't make a difference with this simple sketch).
/*
Max7221_digits
*/
#include <SPI.h> // Arduino SPI library introduced in Arduino version 0019
const int slaveSelect = 10; //pin used to enable the active slave
const int numberOfDigits = 2; // change these to match the number of digits wired up
const int maxCount = 99;
int number = 0;
void setup()
{
Serial.begin(9600);
SPI.begin(); // initialize SPI
pinMode(slaveSelect, OUTPUT);
digitalWrite(slaveSelect,LOW); //select slave
// prepare the 7221 to display 7-segment data - see data sheet
sendCommand(12,1); // normal mode (default is shutdown mode);
sendCommand(15,0); // Display test off
sendCommand(10,8); // set medium intensity (range is 0-15)
sendCommand(11,numberOfDigits); // 7221 digit scan limit command
sendCommand(9,255); // decode command, use standard 7-segment digits
digitalWrite(slaveSelect,HIGH); //deselect slave
}
void loop()
{
// display a number from the serial port terminated by the end of line character
if(Serial.available())
{
char ch = Serial.read();
if( ch == '\n')
{
displayNumber(number);
number = 0;
}
else
number = (number * 10) + ch - '0'; // see Chapter 4 for details
}
}
// function to display up to four digits on a 7-segment display
void displayNumber( int number)
{
for(int i = 0; i < numberOfDigits; i++)
{
byte character = number % 10; // get the value of the rightmost decade
if(number == 0 && i > 0)
character = 0xf; // the 7221 will blank the segments when receiving value
// send digit number as command, first digit is command 1
sendCommand(numberOfDigits-i, character);
number = number / 10;
}
}
void sendCommand( int command, int value)
{
digitalWrite(slaveSelect,LOW); //chip select is active low
//2-byte data transfer to the 7221
SPI.transfer(command);
SPI.transfer(value);
digitalWrite(slaveSelect,HIGH); //release chip, signal end transfer
}
I believe I have set up the hardware correctly on my breadboard. My problem is that nothing happens but more significantly is that the Arduino's on board LEDs all go dim. When I disconnect the pins into ground or 5v the Arduino comes back to life. I'm using 30 ohms for the resistor for the Max7219 per the table on this page: Arduino Playground - MAX72XXHardware
I'm using this 4 digit 7 Segment LED: http://www.mouser.com/Search/ProductDetail.aspx?R=HDSP-B03Evirtualkey63050000virtualkey630-HDSP-B03E . If someone can advise me where I went wrong I'd really appreciate it.
Edit: Rookie mistake. I had positive and negative jumpers reversed.