Pages: [1]   Go Down
Author Topic: Sending and Receiving 10 Bit integers to/from Arduino and Processing  (Read 922 times)
0 Members and 1 Guest are viewing this topic.
Vancouver, Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to send and receive 10 bit integers (the range of analogRead()) first from processing to Arduino and then arduino will reply back the same number to processing.
I am aware that Serial.write and Serial.read both send and receive 8 bit (1 Byte) values and I have that working. Now want to add 2 more bits to my Byte. I am sure I am not the first person to ask this.

The idea of bit-shifting is very interesting and I think it will solve my problem, however I need help on doing this; preferably an example code would be nice.
Or maybe help me build on top of the code I already have.

Thanks a bunch,

Processing
Code:
import processing.serial.*;
Serial myPort;

int inByte = 0;
boolean proceed = false;
boolean sending = true;

void setup(){
  println(Serial.list());
  String portName = Serial.list()[0];
  // change [0] for the Arduino serial port
  myPort = new Serial(this, "COM5", 57600);
  myPort.clear();
 
}

void draw() {
  // first contact
  while (!proceed) {
    if ( myPort.available() > 0) {
      inByte = myPort.read();
      if (inByte == 'A') {
        println("Received " + inByte);
        myPort.clear();
        proceed = true;
      }
    }
  }
  // send and receive test
  while (proceed) {
    if (!sending) {
      if ( myPort.available() > 0) {
        inByte = myPort.read();
        println("Received " + inByte);
        myPort.clear();
        sending = true;
      }
    }
    if (sending){
      myPort.write(255);
      sending = false;
    }
  }
}
Arduino
Code:
int inByte = 0;

void setup() {
  // setup Serial comm with computer
  Serial.begin(57600);
  Serial.print('A'); // send letter "A"
}

void loop() {
  if (Serial.available() > 0) {
      inByte = Serial.read();
      Serial.write(inByte);
  }
}
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3359
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Now want to add 2 more bits to my Byte
A byte has 8 bits, you can't add two more bits, you have to send another byte.

Code:
int x = 1023;
byte low = x;
byte high = x >> 8;
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25763
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Or highByte  and lowByte.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Vancouver, Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This is great!

The problem here is how to assemble it in Processing and Arduino? Its a 2 way comm link

Thanks
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25763
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Reverse the technique in reply #1
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Vancouver, Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How? Example?
Logged

Valencia, Spain
Online Online
Faraday Member
**
Karma: 144
Posts: 5336
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this, it lets you treat an integer as two bytes:

Code:
union ByteInt {
  int val;
  struct {
    // Access to the bytes of 'val'
    byte lo, hi;
  } bytes;
  ByteInt& operator=(int n) { val = n;  }
  operator int() const { return val;      }
};

// examples
void sendInt(int n)
{
  ByteInt b;
  b = n;
  Serial.write(b.bytes.lo);
  Serial.write(b.bytes.hi);
}

int readInt()
{
  ByteInt b;
  b.bytes.lo = Serial.read();
  b.bytes.hi = Serial.read();
  return b;
}

Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3359
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How? Example?

Think about it for more than 1 minute. You took the low byte first. You then took the high byte by shifting to the right 8. So, to reconstruct you do it in reverse. The only thing extra needed is a logical OR or addition.
Logged

Vancouver, Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks a lot fungus...
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12541
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you are sending data packets of more than one byte then you should provide some way for the receiver to work out where each message starts and ends. If you naively send out alternate low and high bytes then any serial error which caused a byte to get lost would lead to the receiver getting out of step - in this case, swapping the high byte from one sample with the low byte from the adjacent sample.

One way to make this transfer more robust is to encode the numbers as decimal strings and use a textual separator between the values (comma, linefeed etc).

Another way is to encode the value so that you can tell whether a given byte is the high byte or the low byte. For example if you reserve one bit in each byte to tell you which is the high byte, then you still have seven bits for your data so a two byte message would support up to fourteen bits of data. With this approach you would use simple bit shifting and bit masking to encode and decode the number to/from each two byte message.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: