i/o with int's

hi, I just got a diecimila am trying to make a program like the one example “dimmer” but with 2 LEDs instead of one. The problem im having is that I cant send more than a byte from my computer without having problems trying to read it with serial.read(). I’d appreciate it if someone would help me out. here is my code so far (srry for the lack of comments)

//arduino code
int ledPin_1 = 5;
int ledPin_2 = 9;
int ledGrd = 6;

int val = 0;

void setup()
{
  // begin the serial communication
  Serial.begin(9600);
  pinMode(ledPin_1, OUTPUT);
  pinMode(ledPin_2, OUTPUT);
  pinMode(ledGrd, OUTPUT);
}

int getInt(){
  if(Serial.available()){
    int s1 = Serial.read();
    int s2 = Serial.read();
    val = s1 + s2;
  }
  return val;
}

void loop(){
  if (Serial.available()){
    getInt();
    if(val <= 255){
      analogWrite(ledPin_1, val);
      analogWrite(ledPin_2, 0);
      analogWrite(ledGrd, 0);
    }else{
      analogWrite(ledPin_1, 255);
      analogWrite(ledPin_2, 255 - abs(val - 510));
      analogWrite(ledGrd, 0);
    }
  }
}
//proccesing code
//ignore lines 24 to 42

import processing.serial.*;

Serial port;
int slider_x = -10;
int pos = 0;

void setup()
{
  size(512, 130);
  
  println("Available serial ports:");
  println(Serial.list());
  
  port = new Serial(this, "COM5", 9600);  
}

void draw(){
  for (int i = 0; i < 512; i++){
    stroke(i/2, 0, 0);
    line(i, 0, i, 130);
    stroke(256 - (i/2), 0, 0);
    point(i, 65);
  }
  if(mouseY > 45 && mouseY < 85 && mousePressed && (mouseButton == LEFT)){
    slider_x = mouseX - 10;
  }
  if(mouseX > 493 && mouseX < 508 && mouseY > 3 && mouseY < 18 && mousePressed && (mouseButton == LEFT)){
    slider_x = -10;
  }
  pos = (slider_x + 10);
  if(pos >= 510){
    pos = 510;
  }else if(pos <= 0){
    pos = 0;
  }
  noFill();
  stroke(256 - (slider_x/2), 0, 0);
  rect(slider_x, 45, 20, 40);
  stroke(0);
  rect(493, 3, 15, 15);
  line(493, 3, 508, 18);
  line(508, 3, 493, 18);
  port.write(pos);
}

The easiest way to deal wirth numbers larger than one byte is to split the number up in two bytes, MSB and LSB (Most Significant Byte and Least Significant Byte)

You can use this formula To do it

MSB = Large number / 256 (throw the decimals away)
LSB = Large number - (MSB * 256)

Using 987 as an example

MSB = 987 / 256 = 3.85… (throw decimals away so MSB = 3)
LSB = 987 - (3 * 256) = 219

send these 2 bytes to Arduino

In the Arduino code rebuild the original number as:

Large number = (MSB * 256) + LSB

(3 * 256) + 219 = 987

This works for positive numbers in the range 0 - 65536

thanks, that helped a lot. But the thing I don’t get is how to actually send more than one byte from processing and receive it on the arduino

I’m afraid my processing knowledge is zero, but i can imagine that you just use serveral write statements.

I think in your case serveral port.write(“byte”) statements would probably do the trick. Maybe theers a way to send more bytes in one call ?

Maybe some of the processing gurus can chip in with a little expert advise here ?

ya, that would help. Right now im just using 2 "port.write()"s in processing and 2 "serial.read()"s in my arduino but it doesn’t work

Your processing code is sending an int to the arduinio but I believe that an int is a 32 bit value (four bytes) in processing whereas on the arduino its only 16 bits (2 bytes).

So you need to read the 4 bytes from processing on the arduino but (if the value is less than 32767, the largest value that an arduino int can hold) you can throw two of the processing bytes away. I am not sure which two or what order the bytes are sent but you can do the following experiment to find out by running the following code in a sketch.

int getInt(){
// this should only be called if Serial.available() >= 2)
byte b1 = Serial.read();
byte b2 = Serial.read();
int val = b1 + (b2 << 8); // One if these two should work
// int val = b2 + (b1 << 8); // depending on the byte order
return val;
}

void loop(){
int val;
if (Serial.available() > 2){
val = getInt();
Serial.println(val)
}
}

The code should send 2 decimal values back to the serial port. One will be 0, the other should be the number you expect. If its not, comment out the line: int val = b1 + (b2 << 8); and uncomment the following line.

I hope that helps.