Chicago IL USA
Offline
Jr. Member
Karma: 0
Posts: 54
int myMind = 0;
|
 |
« on: February 06, 2007, 11:53:40 pm » |
I'm trying to figure out how to update an array via serial. It seems easy enough to update parts of an array predefined in my program, but I want to be able to load all of my data into my array via a serial connection to save on the program size. for instance, mine is an array of 8 separate bytes. I need to be able to read 8 new bytes from the serial connection and load them into the appropriate place in the array, I just can't seem to figure out where to start... can anyone point me in the right direction?
|
|
|
|
|
Logged
|
there are 10 types of people in this world, those who can read binary and those who can't.
|
|
|
|
|
|
Chicago IL USA
Offline
Jr. Member
Karma: 0
Posts: 54
int myMind = 0;
|
 |
« Reply #2 on: February 07, 2007, 01:41:02 pm » |
thank you, but it's not that I don't understand arrays. I am already using them and accessing them in my program. What I'm stuck on is the serial bit. If I send 8 bytes to arduino, how can the software tell what byte is first. If I can sort that out, I'm set, because then I can just update the array.
|
|
|
|
|
Logged
|
there are 10 types of people in this world, those who can read binary and those who can't.
|
|
|
|
0
Offline
Faraday Member
Karma: 6
Posts: 2504
|
 |
« Reply #3 on: February 08, 2007, 07:23:03 am » |
This code is for the arduino; the host computer part is left as an excercise for the reader.  byte data[8], i;
while (i<8) { if (Serial.available()) { data[i++] = Serial.read(); } }
-j
|
|
|
|
|
Logged
|
|
|
|
|
Chicago IL USA
Offline
Jr. Member
Karma: 0
Posts: 54
int myMind = 0;
|
 |
« Reply #4 on: February 08, 2007, 02:51:23 pm » |
ah... this does work quite well... one question though... I'll need to reset "i" when I want to update the array yes?
|
|
|
|
|
Logged
|
there are 10 types of people in this world, those who can read binary and those who can't.
|
|
|
|
0
Offline
Faraday Member
Karma: 6
Posts: 2504
|
 |
« Reply #5 on: February 08, 2007, 03:15:41 pm » |
if by "update the array" you mean get a new set of values, yes. This toy example assumes a one-time initialization from the serial port.
-j
|
|
|
|
|
Logged
|
|
|
|
|
Florida, USA
Offline
Full Member
Karma: 0
Posts: 146
meow!
|
 |
« Reply #6 on: February 08, 2007, 04:06:01 pm » |
Actually, I think you need to set i=0, not just reset it afterward. In other words, you need "i=0;" before "while (i<  ". Otherwise, the value of "i" is not initialized to any particular value.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Faraday Member
Karma: 6
Posts: 2504
|
 |
« Reply #7 on: February 08, 2007, 05:07:32 pm » |
Doh! Good catch. You definitely need to initialize i.
-j
|
|
|
|
|
Logged
|
|
|
|
|
Chicago IL USA
Offline
Jr. Member
Karma: 0
Posts: 54
int myMind = 0;
|
 |
« Reply #8 on: February 08, 2007, 08:24:43 pm » |
so this is working for the most part for me, but I cant seem to find a good spot to reset the arrays index. byte dataPin = 0; // 74HC595 pin 14 byte latchPin = 1; // 74HC595 pin 12 byte clockPin = 2; // 74HC595 pin 11
byte data; // storage for the current data byte byte dataArray[8]; // define the data array
byte row; // storrage for the current row byte byte rowArray[8]; // define the row array
byte index = 0;
void setup() { pinMode(dataPin, OUTPUT); //pinmode setup pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT);
// row registers... DO NOT CHANGE THESE! rowArray[0] = B01111111; rowArray[1] = B10111111; rowArray[2] = B11011111; rowArray[3] = B11101111; rowArray[4] = B11110111; rowArray[5] = B11111011; rowArray[6] = B11111101; rowArray[7] = B11111110;
Serial.begin(9600); }
void loop() { serialListen(); drawSprite(); }
void drawSprite() { for(int j = 0; j < 8; j++){ // load the byte... data = dataArray[j]; // from the data array... row = rowArray[j]; // and the row array digitalWrite(latchPin, LOW); // ground the latch pin shiftOut(dataPin, clockPin, MSBFIRST, data); // shift out the data shiftOut(dataPin, clockPin, MSBFIRST, row); // shift out the row byte digitalWrite(latchPin, HIGH); // it's finished so bring the latch pin high } }
void serialListen() { while (index < 8) { if (Serial.available()) { dataArray[index++] = Serial.read(); } } }
my code draws to a LED matrix driven by 595s. when I insert an if statement in the code to check the index and reset it at it's peak either the display does nothing at all, or the display draws at the speed of the incoming serial data. I'm looking to load an array via serial and periodically update that same array with new bytes... this did get me a bit farther, but I'm really stuck this time.
|
|
|
|
|
Logged
|
there are 10 types of people in this world, those who can read binary and those who can't.
|
|
|
|
0
Offline
Faraday Member
Karma: 6
Posts: 2504
|
 |
« Reply #9 on: February 08, 2007, 08:43:17 pm » |
You're blocking on the reading of data, reading 8 bytes, displaying it, then blocking on the next data. in loop(), you have serialListen(); which blocks - i.e. it stops execution until the read is complete. I think what you want in loop() is if (serial.available()) { serialListen(); } This will only call serialListen() if data is available. Or, you could rewrite serialListen so that it is non-blocking, and while you're at it copy the incoming data into a temporary buffer so that you can keep displaying the old data until you get all of the new data (but that's a little bit trickier). -j
|
|
|
|
|
Logged
|
|
|
|
|
Chicago IL USA
Offline
Jr. Member
Karma: 0
Posts: 54
int myMind = 0;
|
 |
« Reply #10 on: February 08, 2007, 10:47:42 pm » |
I'm not sure this is the issue... within my serialListen() method, if(Serial.available()){ is first, so technically if serial is not available, it should just continue down the line. for example, I tried it from another angle void serialListen() { if(Serial.available()){ dataArray[index] = Serial.read(); index++; } if(index >= 8){ index = 0; } }
this allows me to send groups of 8 bytes at a time to my array and update the array without the while loop. however it seems to fall out of sync (can't really think of a better way to describe it) and the sprite drawn on the matrix can come out of center vertically... so though I can now update the display (play small animations etc) I still havn't been able to sort this out.
|
|
|
|
|
Logged
|
there are 10 types of people in this world, those who can read binary and those who can't.
|
|
|
|
0
Offline
Faraday Member
Karma: 6
Posts: 2504
|
 |
« Reply #11 on: February 09, 2007, 07:18:58 am » |
looks OK at a glance, are you sure you're receiving the data properly, no extra carriage returns or line feeds or anything?
-j
|
|
|
|
|
Logged
|
|
|
|
|
Chicago IL USA
Offline
Jr. Member
Karma: 0
Posts: 54
int myMind = 0;
|
 |
« Reply #12 on: February 09, 2007, 03:15:46 pm » |
yeah... just sending an array of bytes one by one. It seems ok now for some reason though. not sure what the issue was, but today it seems to like me better  .
|
|
|
|
|
Logged
|
there are 10 types of people in this world, those who can read binary and those who can't.
|
|
|
|
|