Hey all. First post, and hopefully one thats not too complicated.
I've decided to try and write to code to get my single 8x8 matrix scrolling, and to display whatever I feed it via the serial monitor.
I'm progressing pretty well but I'm after getting stuck.
The Arduino is my first experience in programming in C, so my code is probably very inefficient and messy (I've only played with the arduino for 15-20 hours total since I got it). I'm just hoping by writing the code and not just copying a full program from someone else it'll help me get better at it.
My problem is that if I feed in 10 or so characters through serial, the output on the screen is missing one of the letters every so often. If I only feed in upto 4 letters it seems to get them all.
e.g. If I type"ACACACACACAC". The output will lose a random letter from that ending up with something like "ACACCACACAC".
I've only defined the letters A, C, H at the moment, I'll add the rest when I get it working.
At a guess, I think that the screenUpdate function that run's every 1/100th of a second to light the correct row of LED's, could be what's causing the problem. Maybe the program is skipping a letter if screenUpdate runs while its working?
If it is, I can't figure out how to get around it.
The alphabet font in there at the moment is someone elses, I'll make my own once the rest is done. The bitReverse function at the bottom is just to flip the bytes in the alphabet because it displayed upside down in my program.
The hardware is just 2 74HC595 shift registers and an 8x8 LED matrix.
Here's my code so far:
#include <TimerOne.h>
//initial serial buffer
char buffer[1];
//initial numchar
int numChar=0;
// Latch pin of shift registers
int latchPin = 8;
//Clock pin of shift registers
int clockPin = 12;
//Data pin of shift registers
int dataPin = 11;
//for bitReverse function
uint8_t mybitRev;
//--Setup counter for number of letters in buffer for passArray function (and any other function
//that needs it
uint8_t bufferCount=0;
//alphabet (revered using bitReverse function)
uint8_t alphabets[][5] = {
{0,0,0,0,0},
{31, 36, 68, 36, 31},
{127, 73, 73, 73, 54},
{62, 65, 65, 65, 34},
{127, 65, 65, 34, 28},
{127, 73, 73, 65, 65},
{127, 72, 72, 72, 64},
{62, 65, 65, 69, 38},
{127, 8, 8, 8, 127},
{0, 65, 127, 65, 0},
{2, 1, 1, 1, 126},
{127, 8, 20, 34, 65},
{127, 1, 1, 1, 1},
{127, 32, 16, 32, 127},
{127, 32, 16, 8, 127},
{62, 65, 65, 65, 62},
{127, 72, 72, 72, 48},
{62, 65, 69, 66, 61},
{127, 72, 76, 74, 49},
{50, 73, 73, 73, 38},
{64, 64, 127, 64, 64},
{126, 1, 1, 1, 126},
{124, 2, 1, 2, 124},
{126, 1, 6, 1, 126},
{99, 20, 8, 20, 99},
{96, 16, 15, 16, 96},
{67, 69, 73, 81, 97},
};
//Row incrementer
int rowInc = 0;
uint8_t led[200];
long counter1 = 0;
long counter2 = 0;
void setup() {
for (int first=0; first<26; first++) {
for (int second=0; second<5; second++) {
bitReverse (alphabets[first][second]);
alphabets[first][second] = mybitRev;
}
}
Serial.begin(9600);
Serial.flush();
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
Timer1.initialize(10000);
Timer1.attachInterrupt(screenUpdate);
}
void loop() {
if (Serial.available() > 0) {
timeVal=10;
for (int x=0; x<bufferCount ; x++) {
buffer[x]='\0';
}
int index = 0;
delay(100); //buffer fill time
int numChar = Serial.available();
char buffer [numChar];
bufferCount = numChar;
uint8_t led[bufferCount * 8];
while (numChar--) {
buffer[index++] = Serial.read();
}
passArray(buffer);
timeVal=0;
}
delay(100);
rowInc++;
if (rowInc > bufferCount * 8) {
rowInc = 0;
}
}
void screenUpdate() {
uint8_t row = B00000001;
for (byte k = 0; k < 9; k++) {
//open up the latch ready to receive data
digitalWrite(latchPin, LOW);
shiftIt(~row );
shiftIt(led[k+rowInc] ); //LED Array
digitalWrite(latchPin, HIGH);
row = row << 1;
}
}
void shiftIt(byte dataOut) {
boolean pinState;
digitalWrite(dataPin, LOW);
for (int i=0; i<8; i++) {
digitalWrite(clockPin, LOW);
if (dataOut & (1<<i) ) {
pinState = HIGH;
}
else {
pinState = LOW;
}
digitalWrite(dataPin, pinState);
digitalWrite(clockPin, HIGH);
digitalWrite(dataPin, LOW);
}
digitalWrite(clockPin, LOW);
}
void passArray(char* data) {
for (int bufferFeed=0; bufferFeed <= bufferCount; bufferFeed++) {
if ((data[bufferFeed] == 'a') || (data[bufferFeed] == 'A')) {
led[0+(bufferFeed * 8)] = alphabets[1][0];
led[1+(bufferFeed * 8)] = alphabets[1][1];
led[2+(bufferFeed * 8)] = alphabets[1][2];
led[3+(bufferFeed * 8)] = alphabets[1][3];
led[4+(bufferFeed * 8)] = alphabets[1][4];
led[5+(bufferFeed * 8)] = 0;
led[6+(bufferFeed * 8)] = 0;
led[7+(bufferFeed * 8)] = 0;
}
if ((data[bufferFeed] == 'c') || (data[bufferFeed] == 'C')) {
led[0+(bufferFeed * 8)] = alphabets[3][0];
led[1+(bufferFeed * 8)] = alphabets[3][1];
led[2+(bufferFeed * 8)] = alphabets[3][2];
led[3+(bufferFeed * 8)] = alphabets[3][3];
led[4+(bufferFeed * 8)] = alphabets[3][4];
led[5+(bufferFeed * 8)] = 0;
led[6+(bufferFeed * 8)] = 0;
led[7+(bufferFeed * 8)] = 0;
}
if ((data[bufferFeed] == 'h') || (data[bufferFeed] == 'H')) {
led[0+bufferFeed * 8] = alphabets[8][0];
led[1+bufferFeed * 8] = alphabets[8][1];
led[2+bufferFeed * 8] = alphabets[8][2];
led[3+bufferFeed * 8] = alphabets[8][3];
led[4+bufferFeed * 8] = alphabets[8][4];
led[5+bufferFeed * 8] = 0;
led[6+bufferFeed * 8] = 0;
led[7+bufferFeed * 8] = 0;
}
}
}
void bitReverse(uint8_t num) {
uint8_t var = 0;
uint8_t i, x, y, p;
uint8_t s = 8;
for (i = 0; i < (s / 2); i++) {
p = s - i - 1;
x = num & (1 << p);
x = x >> p;
y = num & (1 << i);
y = y >> i;
var = var | (x << i); // apply x
var = var | (y << p); // apply y
}
mybitRev = var;
}
Help out a newbie?
Thanks.