Hey everyone,
A big thanks for all of the info given.
Rintin, that was going to be my next question, but I was able to figure out how to pad with zeros before I came back to the forum
Although, your code does look cleaner. Here's what I did thanks to David's examples:
int latchPin = 8;
int clockPin = 12;
int dataPin = 11;
char OutTo[4];
void setup() {
pinMode(latchPin, OUTPUT);
}
void loop() {
for (int j = 0; j < 1500; j++) {
sendOut(j);
delay(100);
}
}
void sendOut(int a) {
char buf[5] = {0};
itoa(a, buf, 10);
if(a < 10) {OutTo[0] = buf[0]; OutTo[1] = '0'; OutTo[2] = '0'; OutTo[3] = '0';}
if(a >= 10 && a < 100) {OutTo[0] = buf[1]; OutTo[1] = buf[0]; OutTo[2] = '0'; OutTo[3] = '0';}
if(a >= 100 && a < 1000) {OutTo[0] = buf[2]; OutTo[1] = buf[1]; OutTo[2] = buf[0]; OutTo[3] = '0';}
if(a > 1000) {OutTo[0] = buf[3]; OutTo[1] = buf[2]; OutTo[2] = buf[1]; OutTo[3] = buf[0];}
digitalWrite(latchPin, 0);
for (int i = 4; i > 0; i -= 2) {shiftOut(dataPin, clockPin, ascii2nixie(OutTo[i-1], OutTo[i-2]));}
digitalWrite(latchPin, 1);
}
uint8_t ascii2nixie(char hi, char lo) {
return ((hi - '0') << 4) | (lo - '0');
}
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
int i=0;
int pinState;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);
digitalWrite(myDataPin, 0);
digitalWrite(myClockPin, 0);
for (i=7; i>=0; i--) {
digitalWrite(myClockPin, 0);
if ( myDataOut & (1<<i) ) {
pinState= 1;
}
else {
pinState= 0;
}
digitalWrite(myDataPin, pinState);
digitalWrite(myClockPin, 1);
digitalWrite(myDataPin, 0);
}
digitalWrite(myClockPin, 0);
}
It works as it should!
I did forget to change writeNumber() to an int, instead of a byte, earlier.
I'll try the code that Rintin revised as well, just to see another way of going about this task. I was wondering why David was using uint32_t earlier, but knowing now that his logic can display up to eight characters make my project far easier and use less pins/ hardware.
6v6gt, my plan is to replace the stock instrument cluster in my 2000 Mazda Miata with nixie tubes. I wanted to use four tubes and two 595's for RPM and then three more tubes for MPH and one for fuel level(would display numerator value in tenth's of a tank). The MPH and fuel level would use another two 595's and three more pins to go along with it.
Given that I can convert an integer to individual chars in an array, what I may do is this:
- get fuel level(represented by a single digit) multiply this by 1000, put in temp location
- get MPH(other hardware/calculations done to get this number)
- Add MPH to fuel level temp location to get a four digit number
- itoa the temp number that has MPH and fuel level
- put into array with RPM and then shift all data out to 8 tubes(with 4 595's daisy chained)
This way I'm only using three pins to display all of my numerical data.
David, how would I go about using SPI, and what would be the advantages? I only used the pins I did because that was what the Arduino example used.
I do need to work on a dimming circuit, which will probably be as 6v6gt described, unless there is a better way.
Again, thanks for all of the help!