Sending and Receiving 10 Bit integers to/from Arduino and Processing
in
Integration and Hardware
•
11 months ago
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
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
- 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;
}
}
}
- 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);
}
}
1