processing to arduino serial fail

i read about serial communication a lot but i can't solve my problem.

so i try to send data from processing(1.5.1) via serial (USB) to arduino uno (arduino 1.0).

i check the serial monitor but it is weird what i get there. i print the data with Serial.println(data) but the input data looks like this:

serial monitor

1
11

1
111
1
11
1
1

1
11
11
1

11

processing code:

void setup(){
  myPort = new Serial(this, Serial.list()[1], 9600);
  myPort.buffer(1);
  size(400,400);
}

void draw(){
 myPort.write(1);
}

arduino code:

byte input;

void setup() {
  Serial.begin(9600);
}

void loop(){
}

void serialEvent(){
 while(Serial.available()>0) { 
  input = Serial.read();
  Serial.println(input);
 }
}

so why aren't they look like this:

1
1
1
1
1
1
1
...

other problem when i try to send more bytes like this:

processing code:
 
void setup(){
  myPort = new Serial(this, Serial.list()[1], 9600);
  myPort.buffer(1);
  size(400,400);
}

void draw(){
  for(byte i=0;i<8;i++){
    myPort.write(i);
  }
}


arduino code:


byte input;

void setup() {
  Serial.begin(9600);
}

void loop(){
}

void serialEvent(){
 while(Serial.available()>0) { 
   input = Serial.read();
   Serial.println(input);
 }
}

serial monitor:

5
3
4
2
4
0
2
7
0
35
62
1
...

why get i this wrong sequence? if i try to add some data via serial monitor manually without processing is a fail too:
i add this 1 , 2 , 3 and i get this with Serial.println():

49
10
50
10
51
10

what is the "10" doing there after each input?

my idea is that processing send the data too fast, but how can i get "synchronous" them?
any idea/solution?

I can't run both processing and the serial monitor on the same port at the same time, so I can't duplicate your results.

When you send from the serial monitor, the arduino receives the character '1', which has the ascii value 49. Do you have the ''newline' option selected from the drop-down in serial monitor? That would explain the 10s you are seeing.

yes the serial monitor with processing sometimes work sometimes not. thanks it makes sense the new line character!
but with the other issues i am really stucked :~

I can't run both processing and the serial monitor on the same port at the same time,

No one can. That is basically your problem.

i still dont get it... try to send 8bytes to controll 8 leds:

processing

byte[] output = {1,3,5,7,9,11,13,15};

void setup(){
   myPort = new Serial(this, Serial.list()[1], 9600);
   myPort.buffer(1);
}

void draw(){
   myPort.write('>');
   myPort.write(output);
}



arduino code:

...
void serialEvent(){
   if(Serial.available()>0){
      val = Serial.read();
      if(val=='>'){
         index=0;
      }
      else{
         input[index] = val;
         index++;
     }
  }
}

any suggestion? :expressionless: (i know it wrong because the leds are changing instead of stay constant, it's like the arduino skips the beginner character '>')

You can add a serialEvent() method in Processing, too, and read what the Arduino sends back. On the Arduino, then, use Serial.print() and Serial.println() to send data back to Processing, like:

Serial.print("I got a [");
Serial.print(val);
Serial.println("]");

i did some test and it seems that if i send a number > 9, somwhere it was splitted but i don't know why.

like this:

i send this 21,21,21 and the feedback show me this: 1st: 2, 2nd: 1, 3rd: 2, 4th: 1, 5th: 2, 6th: 1....
why does it do that? and no matters byte or int the result is the same...

This is because the data is always sent in ASCII, each digit is a single character. This is fundamental to understanding serial communications.

thanks i solved that part.
BUT i was a little dumb because the previous ones were not my full of code :roll_eyes:
i test it with only that what i posted above and turned out that it works.
so my real question is why doesn't it work, and this time here is the full of code:
(perhaps because the interrupt?)

#define __spi_clock 13   // SCK - hardware SPI
#define __spi_latch 10
#define __spi_data 11    // MOSI - hardware SPI
//#define __spi_data_in 12 // MISO - hardware SPI (unused)
#define __display_enable 9
#define __rows 8
#define __max_row __rows-1
#define __leds_per_row 8
#define __max_led __leds_per_row-1
#define __brightness_levels 32 // 0...15 above 28 is bad for ISR ( move to timer1, lower irq freq ! )
#define __max_brightness __brightness_levels-1
#define __fade_delay 4

#define __TIMER1_MAX 0xFFFF // 16 bit CTR
#define __TIMER1_CNT 0x0130 // 32 levels --> 0x0130; 38 --> 0x0157 (flicker)
#define __TIMER2_MAX 0xFF // 8 bit CTR
#define __TIMER2_CNT 0xFF // max 28 levels !
#include <avr/interrupt.h>   
#include <avr/io.h>
#include <stdint.h>

byte input[8]; // = {1,3,5,7,9,11,13,15};
byte sor = 0;
byte val=0;

boolean ok = true;
byte bred[8][8];
byte brightness_red[__leds_per_row][__rows]; 
byte brightness_green[__leds_per_row][__rows];
byte brightness_blue[__leds_per_row][__rows]; 



ISR(TIMER1_OVF_vect) {
  //TCNT2 = __TIMER2_MAX - __TIMER2_CNT; // precharge TIMER2 to maximize ISR time --> max led brightness
  TCNT1 = __TIMER1_MAX - __TIMER1_CNT;
  byte cycle;
  
  digitalWrite(__display_enable,LOW); // enable display inside ISR
  
  for(cycle = 0; cycle < __max_brightness; cycle++) {
    byte led;
    byte row = B00000000;    // row: current source. on when (1)
    byte red;    // current sinker when on (0)
    byte green;  // current sinker when on (0)
    byte blue;   // current sinker when on (0)

    for(row = 0; row <= __max_row; row++) {
      
      red = B00000000;    // off
      green = B00000000;  // off
      blue = B00000000;   // off
      
      for(led = 0; led <= __max_led; led++) {
        if(cycle < brightness_red[row][led]) {
          red |= (1<<led);
        }
        if(cycle < brightness_green[row][led]) {
          green |= (1<<led);
        }
        if(cycle < brightness_blue[row][led]) {
          blue |= (1<<led);
        }
      }

      digitalWrite(__spi_latch,LOW);
      spi_transfer(B00000001<<row);
      spi_transfer(green);
      spi_transfer(blue);
      spi_transfer(red);
      digitalWrite(__spi_latch,HIGH);
      digitalWrite(__spi_latch,LOW);
    }
  }
  
  digitalWrite(__spi_latch,LOW);
  spi_transfer(B00000000); // blue off
  spi_transfer(B00000000); // green off
  spi_transfer(B00000000); // red off
  spi_transfer(B00000000); // rows off
  digitalWrite(__spi_latch,HIGH);
  digitalWrite(__spi_latch,LOW);
  
  digitalWrite(__display_enable,HIGH);    // disable display outside ISR
}





void setup() {
  randomSeed(555);
  byte ctr1;
  byte ctr2;
  pinMode(__spi_clock,OUTPUT);
  pinMode(__spi_latch,OUTPUT);
  pinMode(__spi_data,OUTPUT);
  pinMode(__display_enable,OUTPUT);
  digitalWrite(__spi_latch,LOW);
  digitalWrite(__spi_data,LOW);
  digitalWrite(__spi_clock,LOW);
  Serial.begin(9600);  
  setup_hardware_spi();
  delay(10);
  set_matrix_rgb(0,0,0);
  //setup_timer2_ovf();  
  setup_timer1_ovf();
  // display enable/disable is done inside the ISR !
}


void loop(){
  for(int i=0; i < 8; i++) { 
    set_row_rgb(i,0,0,input);
  }
}


void serialEvent() {
  while(Serial.available()) {
  val = Serial.read();
  if(val=='>'){
    sor=0;
  }
  else
  {
    input[sor] = val;
    sor++;  
  }
 }
}




////////////////////////////LED SPECIFIC STUFF/////////////////////////////////////

void set_led_red(byte row, byte led, byte red) {
  brightness_red[row][led] = red;
}


void set_led_green(byte row, byte led, byte green) {
  brightness_green[row][led] = green;
}


void set_led_blue(byte row, byte led, byte blue) {
  brightness_blue[row][led] = blue;
}


void set_led_rgb(byte row, byte led, byte red, byte green, byte blue) {
  set_led_red(row,led,red);
  set_led_green(row,led,green);
  set_led_blue(row,led,blue);
}


void set_matrix_rgb(byte red, byte green, byte blue) {
  byte ctr1;
  byte ctr2;
  for(ctr2 = 0; ctr2 <= __max_row; ctr2++) {
    for(ctr1 = 0; ctr1 <= __max_led; ctr1++) {
      set_led_rgb(ctr2,ctr1,red,green,blue);
    }
  }
}


void set_row_rgb(byte row, byte red, byte green, byte blue) {
  byte ctr1;
  for(ctr1 = 0; ctr1 <= __max_led; ctr1++) {
      set_led_rgb(row,ctr1,red,green,blue);
  }
}


void set_column_rgb(byte column, byte red, byte green, byte blue) {
  byte ctr1;
  for(ctr1 = 0; ctr1 <= __max_row; ctr1++) {
      set_led_rgb(ctr1,column,red,green,blue);
  }
}


byte spi_transfer(byte data)
{
  SPDR = data;                    // Start the transmission
  while (!(SPSR & (1<<SPIF)))     // Wait the end of the transmission
  {
  };
  return SPDR;                    // return the received byte, we don't need that
}


void setup_hardware_spi(void) {
  byte clr;
  // spi prescaler: 
  // SPI2X SPR1 SPR0
  //   0     0     0    fosc/4
  //   0     0     1    fosc/16
  //   0     1     0    fosc/64
  //   0     1     1    fosc/128
  //   1     0     0    fosc/2
  //   1     0     1    fosc/8
  //   1     1     0    fosc/32
  //   1     1     1    fosc/64
  SPCR |= ( (1<<SPE) | (1<<MSTR) ); // enable SPI as master
  //SPCR |= ( (1<<SPR1) ); // set prescaler bits
  SPCR &= ~ ( (1<<SPR1) | (1<<SPR0) ); // clear prescaler bits
  clr=SPSR; // clear SPI status reg
  clr=SPDR; // clear SPI data reg
  SPSR |= (1<<SPI2X); // set prescaler bits
  //SPSR &= ~(1<<SPI2X); // clear prescaler bits
}


void setup_timer1_ovf(void) {
  // Arduino runs at 16 Mhz...
  // Timer1 (16bit) Settings:
  // prescaler (frequency divider) values:   CS12    CS11   CS10
  //                                           0       0      0    stopped
  //                                           0       0      1      /1  
  //                                           0       1      0      /8  
  //                                           0       1      1      /64
  //                                           1       0      0      /256 
  //                                           1       0      1      /1024
  //                                           1       1      0      external clock on T1 pin, falling edge
  //                                           1       1      1      external clock on T1 pin, rising edge
  //
  TCCR1B &= ~ ( (1<<CS11) );
  TCCR1B |= ( (1<<CS12) | (1<<CS10) );      
  //normal mode
  TCCR1B &= ~ ( (1<<WGM13) | (1<<WGM12) );
  TCCR1A &= ~ ( (1<<WGM11) | (1<<WGM10) );
  //Timer1 Overflow Interrupt Enable  
  TIMSK1 |= (1<<TOIE1);
  TCNT1 = __TIMER1_MAX - __TIMER1_CNT;
  // enable all interrupts
  sei(); 
}

so my real question is why doesn't it work,

So what does it do?

oh sry i forget the issue to write down :smiley:
my processing output is a byte[8] array: {1,3,5,7,9,11,13,15} with a '>' character to tell to the array index =0.
and my arduino input is a little strange:

,1,3,5,7,9,11,13,15,
,1,3,5,15,9,11,13,15,
,1,5,7,9,11,13,15,15,
,1,3,5,7,9,11,13,15,
,1,3,5,7,9,15,13,15...

It looks like this bit is wrong:-

void serialEvent() {
  while(Serial.available()) {
  val = Serial.read();
  if(val=='>'){
    sor=0;
  }
  else
  {
    input[sor] = Serial.read();
    sor++;  
  }
 }
}

You read a value and check that it is not a '>' character. If it is not you throw it away and get the next byte in the buffer whether there is one or not.

I guess you need to replace

input[sor] = Serial.read();

with

input[sor] = val;

ohhh i think i am getting tired because copied it wrong to here, in my original code it is good, as you suggested, so still have the problem :confused:

because copied it wrong to here

Do you not use cut and paste to make up your posts?

So can you update that code and have it correct. It is a bit of a waste of time going through code that contains errors you know about. People are apt to give up trying to help you.

no, i type it on ipad because (i dont know why) the osx lion dont want to work with my router at home :confused:

ok, i found a thread that says the interrupt that i use can cause this problem because the serial itself uses interrupt too. how can i avoid that the two process bother each others? or it isnt that simple to solve this issue?

edit: or can i use somehow the serial.read inside my interrupt function that itself wont use its interrupt process?