Go Down

Topic: Trying to control an 8x8 LED matrix according to Arduino.cc example (Read 1 time) previous topic - next topic

I'm trying to control an 8x8 LED matrix according to this example http://arduino.cc/en/Tutorial/RowColumnScanning but it doesn't seem to work, properly. Does this example have any bugs or something?
In the code, in the void readSensors() function I've changed these two lines of code
Code: [Select]

x = 7 - map(analogRead(A0), 0, 1023, 0, 7);
y = map(analogRead(A1), 0, 1023, 0, 7);

with this
Code: [Select]

while(Serial.available()){
  x = Serial.parseInt();
  y = Serial.parseInt();
}

Of course I've started the serial communication in the setup function. I've also tried
Code: [Select]

x = Serial.parseInt() - '0';
y = Serial.parseInt() - '0';

as I'm sending ASCII values from Pure Data, but none of these work, there are quite some LEDs constantly lit, and some others flicker once or twice whenever a new value is received. Any ideas?

PaulRB

Can you print the received numbers back to the pc with Serial.print to check they are being received and interpreted correctly?

Paul

Quote
Can you print the received numbers back to the pc with Serial.print to check they are being received and interpreted correctly?

I did that, both with Serial.print and Serial.write, and then printed them back to the Pure Data console. Indeed I was receiving correct values. I also change the code a bit to output values when arduino receives a line feed, but still...

PaulRB

What happens if you put the code back exactly like the example? If you dont have any pots, just try different pairs of resistors.

1ChicagoDave

Code: [Select]


while(Serial.available()){
  x = Serial.parseInt();
  y = Serial.parseInt();
}



While loops will loop continuously, and infinitely, until the expression inside the parenthesis, () becomes false. Something must change the tested variable, or the while loop will never exit.


Quote
While loops will loop continuously, and infinitely, until the expression inside the parenthesis, () becomes false. Something must change the tested variable, or the while loop will never exit.

I'm sending values to Arduino from Pure Data using the [cursor] object which detects the position of the cursor (obviously). When I don't move the mouse there's nothing being sent to Arduino so the while loop ends. I even deleted the loop and only used parseInt(), but the matrix shows the same behavior. Maybe the matrix I have doesn't have the same pin configurations as the one in the Arduino.cc example, could that be? It's actually this one http://grobotronics.com/led-matrix-8x8-red-3mm.html?sl=en#.Uk_HKSRPr0E but there are no specs about the pins there...

PaulRB

The Serial.parseInt seems to have a 1 second timeout. So the Serial.available may be true because some "whitespace" or other non-numeric characters are in the buffer, but then parseInt times out and returns zero because no numeric characters are in the buffer.

You matrix does seem to have the same pinout as the tutorial. Here's the data sheet:

http://toplight-international.com/product/pdf/A-1088BS.pdf


So how should I send two values to Arduino? For now I'm sending a list that looks like this: 52 32 48 10, which should tell Arduino to use column 4 and row 0, separated by a space and ended with a line feed. If there's a timeout problem with Serial.parseInt(), is there some alternative to sending a list to Arduino and then having Arduino route the different elements of the list to different variables?

PaulRB

Ok, I think I see the problem now. Dont use parseInt, that expects an number encoded as ascii characters. You are sending the value in a single byte so just use read (). Also, to avoid getting out of sync, check available () >= 4.

Tried to send one value only and get it with Serial.read(); but nothing happens, still. Anyway, I want to send two values, how is this possible with Serial.read();? This receives only one byte, right? I am sending ASCII values anyway, do you think this is a problem?

PaulRB

Sorry, this is very annoying. This forum used to warn you if someone replied while you were composing a reply, but it doesnt do that anymore.

Check my last response again. Does that help?

You will need to use read () to get the space and linefeed characters from the buffer, in addition to your values.

PaulRB

Wait...

Sorry, you are sending the numbers as ascii encoded. I just realised 48 = "0" and 52 = "4".

So parseInt was right. But I would still recommend checking available () >= 4.

It notified me earlier that you posted before I submitted my answer...
Anyway, I think that there's either something wrong with my matrix, or with the rest of the example code, cause I really think I am sending correct numbers. In the Arduino.cc example two potentiometers are used, which have their values mapped from 0-1023 to 0-7. These are the values I'm sending to Arduino from Pd, but really almost nothing happens. Columns 0, 1, 3, 4, 5 and 7 are constantly lit, and that's all there is to it. Sometimes some other columns or rows flicker dimly...Maybe it's the rows that are lit and not the columns, I'm a bit confused with the specs...
Anyway, if anyone's willing to check here's the code I'm using
Code: [Select]

// 2-dimensional array of row pin numbers:
const int row[8] = {2,7,19,5,13,18,12,16};

// 2-dimensional array of column pin numbers:
const int col[8] = {6,11,10,3,17,4,8,9};

// 2-dimensional array of pixels:
int pixels[8][8];

// cursor position:
int x = 5;
int y = 5;

void setup() {
  // initialize the I/O pins as outputs
  // iterate over the pins:
  for (int thisPin = 0; thisPin < 8; thisPin++) {
    // initialize the output pins:
    pinMode(col[thisPin], OUTPUT);
    pinMode(row[thisPin], OUTPUT); 
    // take the col pins (i.e. the cathodes) high to ensure that
    // the LEDS are off:
    digitalWrite(col[thisPin], HIGH);   
  }

  // initialize the pixel matrix:
  for (int x = 0; x < 8; x++) {
    for (int y = 0; y < 8; y++) {
      pixels[x][y] = HIGH;
    }
  }
  Serial.begin(9600);
}

void loop() {
  // read input:
  readSensors();

  // draw the screen:
  refreshScreen();
}

void readSensors() {
  // turn off the last position:
  pixels[x][y] = HIGH;
  // read the sensors for X and Y values:
  while(Serial.available() >= 4){
    x = Serial.parseInt();
    y = Serial.parseInt();
  }
  if(Serial.read() == '\n'){
    pixels[x][y] = LOW; 
  }
}

void refreshScreen() {
  // iterate over the rows (anodes):
  for (int thisRow = 0; thisRow < 8; thisRow++) {
    // take the row pin (anode) high:
    digitalWrite(row[thisRow], HIGH);
    // iterate over the cols (cathodes):
    for (int thisCol = 0; thisCol < 8; thisCol++) {
      // get the state of the current pixel;
      int thisPixel = pixels[thisRow][thisCol];
      // when the row is HIGH and the col is LOW,
      // the LED where they meet turns on:
      digitalWrite(col[thisCol], thisPixel);
      // turn the pixel off:
      if (thisPixel == LOW) {
        digitalWrite(col[thisCol], HIGH);
      }
    }
    // take the row pin low to turn off the whole row:
    digitalWrite(row[thisRow], LOW);
  }
}


and I've attached the Pd patch..
Thanks

PaulRB

Did you try my suggestion of running the tutorial sketch unaltered?

What's a pd patch?

Quote
Did you try my suggestion of running the tutorial sketch unaltered?

Actually not, but I guess I'll have to do that.

Quote
What's a pd patch?

It's a program in Pure Data, equivalent to an Arduino sketch. It's called a patch cause it's a visual programming language, where little boxes are connected by 'wires', called patch cords...
Thanks.

Go Up