Pages: [1]   Go Down
Author Topic: int values with serial.print  (Read 1740 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 1
Posts: 140
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am working on my first project submitting two temperatures to Processing. I first tried that with "byte" variables and it went fine. But I can't submit int files...

This is the arduino code
Code:
//declare variables
int tempC;
int tempD;
int tempPin = 0;

void setup()
{
Serial.begin(9600); //opens serial port, sets data rate to 9600 bps
}

void loop()
//Mit Sicherheitsbit
{
//Serial.print((byte)255);    //send the header
tempC = 254;  //convert the analog data to temperature
//Serial.print((byte)tempC);             //send the byte data to the computer- works
Serial.print((int)tempC);             //send the int data to the computer- does not work

tempD = 65;  
//Serial.print((byte)tempD);          
Serial.print((int)tempD);
delay(300);
}


And here if the Processing code
Code:
//import Serial communication library
import processing.serial.*;

//init variables
Serial commPort;
int tempC;
int tempD;
PFont font24;

void setup()
{
  //setup fonts for use throughout the application
  font24 = loadFont("serif.vlw");
  size(210, 200);
 
  //init serial communication port
  commPort = new Serial(this, "COM7", 9600);
}

void draw()
{
  //get the temp from the serial port
//Hier mit Orientierungsbit
if (commPort.available() > 2) // don't do anything until you get 3 characters (header plus the two data bytes
{
 if(commPort.read() == 255) // only proceed if you get your header
 {
   tempD = commPort.read();
   tempC = commPort.read();
 }
}   background(123);
    fill(0,0,0);
    textFont(font24);
    textAlign(LEFT);
    text(tempC, 115, 45);
    text(str(int(tempC)) + " C", 115, 65);
    text(str(int(tempD)) + " D", 115, 80);
  }

Can someone help me?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46042
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Serial.print method has several overloads. The single argument method takes whatever value it receives and sends it as a character.

The two argument method allows you to define how the value is sent.

You want to use the two argument method like this:

Serial.print(tempC, DEC);

This will send the value as an int rather than as a char.

On the receiving end, you are expecting some known value as input, but you are no longer sending it. That code is commented out.
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 140
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi PaulS,
I did what you proposed and do receive data...but wrong values.

This the arduino code:
Code:
//declare variables
int tempC;
int tempD;
int tempPin = 0;

void setup()
{
Serial.begin(9600); //opens serial port, sets data rate to 9600 bps
}

void loop()
//Mit Sicherheitsbit
{
Serial.print((byte)255);    //send the header

tempC = 54;  //convert the analog data to temperature
//Serial.print((byte)tempC);             //send the byte data to the computer- works
//Serial.print((int)tempC);             //send the int data to the computer- does not work

tempD = 65;  
Serial.print(tempC, DEC);
Serial.print(tempD, DEC);
//Serial.print((byte)tempD);          
//Serial.print((int)tempD);


delay(300);
}

The processing code remained unchanged. The shown values are 53 and 52 instead of 54 and 65.

May I did not understand the possible code change in Processing described by you with
Quote
On the receiving end, you are expecting some known value as input, but you are no longer sending it. That code is commented out.

What did you mean?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46042
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I modified your program a bit. The arduino code looks like this:

Code:
int tempC;
int tempD;

void setup()
{
  Serial.begin(9600); //opens serial port, sets data rate to 9600 bps
}

void loop()
//Mit Sicherheitsbit
{
  Serial.print("C:");    //send the header
  tempC = 54;  //convert the analog data to temperature
  Serial.print(tempC);

  Serial.print("D:");    //send the header
  tempD = 65;  
  Serial.print(tempD);

  delay(10000);
}

The processing code looks like this:

Code:
//import Serial communication library
import processing.serial.*;

//init variables
Serial commPort;
int tempC;
int tempD;
int val;
int byteCount;

PFont font24;

void setup()
{
  //setup fonts for use throughout the application
  String[] fontList = PFont.list();

  font24 = createFont(PFont.list()[156], 12);
  size(210, 200);

  //init serial communication port
  commPort = new Serial(this, "COM4", 9600);
}

void draw()
{
  //get the temp from the serial port
//Hier mit Orientierungsbit
  byteCount = commPort.available();
  if (byteCount > 5) // don't do anything until you get 3 characters (header plus the two data bytes
  {
    print("byteCount = ");
    println(byteCount);
    
    for(int b=0; b<byteCount; b++)
    {
      print(b);
      print(") ");
      val = commPort.read();
      print("val = ");
      println((char)val);
    }
  }
  background(123);
  fill(0,0,0);
  textFont(font24);
  textAlign(LEFT);
  text(tempC, 115, 45);
  text(str(int(tempC)) + " C", 115, 65);
  text(str(int(tempD)) + " D", 115, 80);
}

When I upload the code to the arduino, and start the processing program, I get this output:

byteCount = 8
0) val = C
1) val = :
2) val = 5
3) val = 4
4) val = D
5) val = :
6) val = 6
7) val = 5

The Serial.print on the arduino is converting the byte to a string, and sending the string one character at a time.

If you put some delimiter between the values you send, like this:

Serial.print("Temps:");
Serial.print(tempC);
Serial.print(";");
Serial.print(tempD);
Serial.println("!");

The processing program would then receive the characters:

T e m p s : 5 4 ; 6 5 !

The processing program would need to look for the : as the marker for the start of the 1st temperature, the ; as the marker for the end of the first temperature and the start of the second temperature, and the ! as the marker for the end of the second temperature.
Logged

Pages: [1]   Go Up
Jump to: