Pages: [1]   Go Down
Author Topic: array help  (Read 1430 times)
0 Members and 1 Guest are viewing this topic.
Chicago IL USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
int myMind = 0;
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Netherlands
Offline Offline
Sr. Member
****
Karma: 0
Posts: 414
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Did you check this, it should get you started:
http://www.arduino.cc/en/Reference/Array
Logged

Chicago IL USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
int myMind = 0;
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Did you check this, it should get you started:
http://www.arduino.cc/en/Reference/Array

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 Offline
Faraday Member
**
Karma: 8
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This code is for the arduino; the host computer part is left as an excercise for the reader.  smiley

Code:
byte data[8], i;

while (i<8)
{
    if (Serial.available())
    {
        data[i++] = Serial.read();
     }
}

-j
Logged

Chicago IL USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
int myMind = 0;
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Faraday Member
**
Karma: 8
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Full Member
***
Karma: 0
Posts: 146
meow!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Actually, I think you need to set i=0, not just reset it afterward.  In other words, you need "i=0;" before "while (i<smiley-cool".  Otherwise, the value of "i" is not initialized to any particular value.
Logged

0
Offline Offline
Faraday Member
**
Karma: 8
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Doh!  Good catch.  You definitely need to initialize i.

-j
Logged

Chicago IL USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
int myMind = 0;
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

so this is working for the most part for me, but I cant seem to find a good spot to reset the arrays index.

Code:
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 Offline
Faraday Member
**
Karma: 8
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You're blocking on the reading of data, reading 8 bytes, displaying it, then blocking on the next data.

in loop(), you have
Code:
serialListen();
which blocks - i.e. it stops execution until the read is complete.

I think what you want in loop() is
Code:
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 Offline
Jr. Member
**
Karma: 0
Posts: 54
int myMind = 0;
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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 Offline
Faraday Member
**
Karma: 8
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Jr. Member
**
Karma: 0
Posts: 54
int myMind = 0;
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-wink.
Logged

there are 10 types of people in this world, those who can read binary and those who can't.

Pages: [1]   Go Up
Jump to: