Pages: [1]   Go Down
Author Topic: Arduino/Serial/Processing issue  (Read 748 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to control 6x6 led matrix using arduino and I have written up such a code.

Code:
int a1 =  13;  
int a2 =  12;
int a3 =  11;
int a4 =  10;
int a5 =  9;
int a6 =  8;
int b1 =  2;
int b2 =  3;
int b3 =  4;
int b4 =  5;
int b5 =  6;
int b6 =  7;

int array[6][6] =
{
  {0,0,0,0,0,0},
  {0,0,0,0,0,0},
  {0,0,0,0,0,0},
  {0,0,0,0,0,0},
  {0,0,0,0,0,0},
  {0,0,0,0,0,0}
};

void setup()
{
  Serial.begin(9600);
  
  pinMode(a1, OUTPUT);
  pinMode(a2, OUTPUT);
  pinMode(a3, OUTPUT);
  pinMode(a4, OUTPUT);
  pinMode(a5, OUTPUT);
  pinMode(a6, OUTPUT);
  pinMode(b1, OUTPUT);
  pinMode(b2, OUTPUT);
  pinMode(b3, OUTPUT);
  pinMode(b4, OUTPUT);
  pinMode(b5, OUTPUT);
  pinMode(b6, OUTPUT);

  // Set them high so nothing would be on
  digitalWrite(a1, HIGH);
  digitalWrite(a2, HIGH);
  digitalWrite(a3, HIGH);
  digitalWrite(a4, HIGH);
  digitalWrite(a5, HIGH);
  digitalWrite(a6, HIGH);
  digitalWrite(b1, HIGH);
  digitalWrite(b2, HIGH);
  digitalWrite(b3, HIGH);
  digitalWrite(b4, HIGH);  
  digitalWrite(b5, HIGH);
  digitalWrite(b6, HIGH);

  
  Serial.flush();
}

void loop()
{
  //delay(100);
  if (Serial.available() > 35) {
    for (int j = 0; j<6; j++){
      for (int i = 0; i<6; i++){
        array[j][i]  = Serial.read()-65;
      }
    }
   Serial.flush();
  }
  draw(array,10);
 
}

void draw(int anim[6][6], int time) {
  for (int a=0; a<time; a++) {
    for (int i=0; i<6; i++) {
      
      digitalWrite(a1, anim[i][0]);
      digitalWrite(a2, anim[i][1]);
      digitalWrite(a3, anim[i][2]);
      digitalWrite(a4, anim[i][3]);
      digitalWrite(a5, anim[i][4]);
      digitalWrite(a6, anim[i][5]);
      digitalWrite(i+2, LOW);
      delay(1);                  // wait
      digitalWrite(i+2, HIGH);
      digitalWrite(a1, HIGH);
      digitalWrite(a2, HIGH);
      digitalWrite(a3, HIGH);
      digitalWrite(a4, HIGH);
      digitalWrite(a5, HIGH);
      digitalWrite(a6, HIGH);
      // set the LED off
    }
  }
}

User inputs 36  A or B and according led light up. It works fine if I input it through arduino's software's serial monitor and do it slowly. The problem arises if I try to use processing to write to serial.

Code:
import processing.serial.*;

Serial myPort;
int a = 200;

void setup(){  
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[1], 9600);
  //myPort.buffer(0);
  size(400, 400);
  background(51);
 // noLoop();
}

void draw(){
  myPort.write("BAAAABBAAAABBAAAABBAAAABBAAAABBAAAAB");
delay(1000);
  myPort.write("BAAAAAABAAAAAABAAAAAABAAAAAABAAAAAAB");
delay(1000);
  myPort.write("ABAAAAAABAAAAAABAAAAAABAAAAAABAAAAAA");
delay(1000);
}


Patterns seems to get randomly shifted. What could be the problem with this set up? How can I fix it?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You read data after 65 characters have arrived, and then flush the buffer. Some of the 2nd group make have arrived by then, but you dump it.

So, part of the second group and part of the 3rd group get collected as a group.

Why do you flush the buffer?

You should have some sort of marker for the start of a group, and a marker for the end of the group. Read and discard data (if available) until the start marker arrives, then read and store data until the end marker arrives. Then, process the stored data.

Another thing to consider is having the arduino send a byte to the processing sketch, to tell it that it is ready for more data, rather than hard-coding a wait time in the processing sketch.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You read data after 65 characters have arrived, and then flush the buffer. Some of the 2nd group make have arrived by then, but you dump it.

So, part of the second group and part of the 3rd group get collected as a group.

Why do you flush the buffer?

It was not behaving properly without flush so it was just a desperate try to fix things.

Quote
You should have some sort of marker for the start of a group, and a marker for the end of the group. Read and discard data (if available) until the start marker arrives, then read and store data until the end marker arrives. Then, process the stored data.

What should I use a a marker? Single symbol is fine? This would also require to change if (Serial.available() > 35) condition, right?

Quote
Another thing to consider is having the arduino send a byte to the processing sketch, to tell it that it is ready for more data, rather than hard-coding a wait time in the processing sketch.

My final project is to build Led matrix that reacts to music (processing controlled) - Follow the beat. Will arduino be able to handle such a rapid change of patterns?

Thanks for helping out!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What should I use a a marker? Single symbol is fine? This would also require to change if (Serial.available() > 35) condition, right?

A single marker is fine. Perhaps > for start and < for end.
The string sent would get longer by two bytes, so, yes, you need to change the 35 to 37.

Don't flush the buffer; data gets lost that way.

Quote
Will arduino be able to handle such a rapid change of patterns?

The limit on the ability of the Arduino to handle the rapid change in patterns depends on how rapidly it gets through the collecting and processing functions.

I'm not sure that I follow what you're doing in the setup and draw functions in the Arduino sketch.

In setup, you use digitalWrite to set the output pins HIGH. The comment says that turns them all off. Typically, setting the pin HIGH turns it on.

Then. in the draw function, you loop some number of times. In the loop, you turn some of the pins in one column HIGH and others LOW, wait one millisecond, and then turn them all HIGH, and go on to the next column.

Somehow HIGH and LOW seem to have reversed roles. The lights are on for a very short period of time.
Logged

Pages: [1]   Go Up
Jump to: