system
September 12, 2010, 4:24pm
1
Hi,
I'm working with a project and I need my arduino to read the value of an Int sent from processing via serial.
I'm just using this code to send the variable:
port.write(displayVal);
But how do I get my arduino to read the variable as it's value can vary every time I send it?
I hope you understand what my problem is. If not, tell me.
By the way, the serial communication between Processing and my Arduino is working fine so that's not a problem. =)
system
September 12, 2010, 7:46pm
2
What type is displayVal?
When the Arduino receives serial data, the Serial.available() function tells how many bytes are available to read. The Serial.read() function reads one byte.
If displayVal is being sent as an integer, it's sent using two bytes. You need to read two bytes, and put the integer back together (by shifting one byte left 8 places and adding the other byte).
If displayVal is being sent as a string, it's sent using a variable number of bytes. Not all of them are available to read immediately. You'll need to read the bytes into a character array, NULL terminating it after each additional character is added. Then, use atoi to convert the string to an integer.
system
September 12, 2010, 7:49pm
3
Thank you very much for being willing to help!
The displayVal is a number between 90 - 115.
system
September 12, 2010, 7:51pm
4
The displayVal is a number between 90 - 115.
That'll fit comfortably into a 'byte' (aka 'unsigned char').
(It would also fit into a 'signed char')
system
September 12, 2010, 7:55pm
5
Okey, yes. I understand it this far. But how do I make the arduino to recognize displayValue as it's sent? Because I've got some other data that's being sent through the serial as well.
system
September 12, 2010, 8:24pm
6
Because I've got some other data that's being sent through the serial as well.
That's news.
I'm just using this code to send the variable:
So, why don't you tell us what it is you're sending?
system
September 12, 2010, 8:26pm
7
I'm sorry guys! I'll post the whole processing code for you now.
import processing.serial.*;
Serial port;
float val;
int rectPlace = 24;
byte displayVal;
PFont f;
void setup(){
size(400,300);
port = new Serial (this, Serial.list()[0], 9600);
f = loadFont("ArialMT-48.vlw");
}
void draw () {
if (rectPlace > 80){
rectPlace = 80;
}
if (rectPlace < 30) {
rectPlace = 30;
}
background(48,139,206);
textFont(f,25);
textAlign(CENTER);
fill(0);
text("Speed", 67, 35);
text(displayVal, 67, 110);
fill(50);
stroke(50);
rect(20, 47, 3, 30); //linjen bakom reglaget
rect(20, 60, 87, 3); //linjen bakom reglaget
rect(107, 47, 3, 30);//linjen bakom reglaget
fill(255);
stroke(255);
rect(rectPlace, 40, 20, 40);//reglaget
}
void keyPressed() {
if (key == 'p') {
if(rectPlace <= 86 && rectPlace >= 24){
val = map(rectPlace, 30, 80, 90, 115);
displayVal = (byte)val;
rectPlace = rectPlace + 2;
}
}
if (key == 'ö') {
if(rectPlace <= 80 && rectPlace >= 30){
val = map(rectPlace, 30, 80, 90, 115);
displayVal = (byte)val;
rectPlace = rectPlace - 2;
port.write(displayVal);
}
}
}
system
September 12, 2010, 8:29pm
8
So, in fact, it is the only value you're writing to the Arduino?
What's the problem?
system
September 12, 2010, 8:35pm
9
Oh, s**t. I'm really sry again. Think I'm too tired for this again.
I forgot to tell you that this code is most for testing my slider-thingy out. I'm later going to put it together with this code:
import processing.serial.*;
int colour = 50;
boolean keyUp, keyDown, keyLeft, keyRight;
Serial port;
void setup(){
size(200,200);
background(0);
port = new Serial(this, Serial.list()[0], 9600);
}
void draw() {
rect(100,100,100,100);
fill(colour);
if (keyUp && keyLeft) {
port.write('K');
}
else if (keyUp && keyRight) {
port.write('J');
}
else if (keyDown && keyLeft) {
port.write('O');
}
else if (keyDown && keyRight) {
port.write('P');
}
else if (keyUp) {
port.write('U');
}
else if (keyDown) {
port.write('D');
}
else if (keyLeft) {
port.write('L');
}
else if (keyRight) {
port.write('R');
}
else {
port.write('S');
}
}
void keyPressed(){
if(key == CODED){
switch (keyCode) {
case UP:
keyUp = true;
break;
case LEFT:
keyLeft = true;
break;
case RIGHT:
keyRight = true;
break;
case DOWN:
keyDown = true;
break;
default:
break;
}
}
else{
colour = 70;
}
}
void keyReleased(){
if (keyCode == UP){
keyUp = false;
}
if (keyCode == DOWN){
keyDown = false;
}
if (keyCode == LEFT){
keyLeft = false;
}
if (keyCode == RIGHT){
keyRight = false;
}
}
system
September 12, 2010, 10:38pm
10
You didn't directly answer the question as to what type displayVal is. From the posted code, though, I see that it is a byte. A byte is 8 bits
Each of the characters that you want to send are chars, which are also 8 bits long.
How will you distinguish whether the vyte received by Serial.read() represents a value or a letter?
system
September 13, 2010, 1:36pm
11
Yes, as you said it's a byte. And to answer your question: I don't know. That's what I need help with.
Thanks for trying to help me anyway, even though I've done it really hard for you.
system
September 13, 2010, 2:04pm
12
The simplest solution is to send the data as strings. Something like "<K:U>" or "<V:120>", where < is a start of packet marker, K or V defines the type of data, : is a delimiter, the value comes next, and > is an end of packet marker.
Reading this kind of data on the Arduino is simple. See this thread (read all the way through):
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1273230655/
system
September 13, 2010, 7:24pm
13
Thanks! I'm gonna read the thread and see if I can get this thing working. =)