newby question int to char

edited January 2015 in Arduino

Hi, I am very new to programming. The script is not writing a character from input serial. Writes 14 numbers. How can I convert to the right value of the input? How to convert to char

import processing.serial.*;

Serial myPort; // The serial port:
int temperatuur = 0;

void setup() {
  size(400, 300);

  //println(Serial.list());
  myPort = new Serial(this, "COM3", 9600);

  textSize(50);
  fill(10, 27, 217);
}

void draw() {
  background(200); 
  while (myPort.available () > 0) {
    temperatuur = myPort.read();
    print (temperatuur);
    text ((temperatuur), width/2, height/2);
  }
}

Comments

  • What do you mean by "right value"? What kinds of values are you getting as ints? What do you want them to be when you convert them to char?

    You'd have better luck if you posted an MCVE without any of the serial stuff. Just hardcode some int values and convert them to char values, and show us what you're getting vs what you're expecting.

  • edited January 2015

    Ok, What I mean is this. Now with the code below the temperature is printed to the screen. In arduino it is for example 24.20 . In processing it is printing in screen but all chacraters printed at the same location on top of each other. Just want to print to pc screen same as arduino like 24.20. Hope someone can help me with that.

    The arduino code:

    {
          Serial.println(t);
          delay (1000);
    } 
    

    Processing code:

    void draw() {
        background(200); 
        while (myPort.available () > 0) {
        char temperatuur = myPort.readChar();
        text ((temperatuur), width/2, height/2);
      }
    
  • You can either build a String by appending each char to a String, or you can position the chars on the screen instead of always displaying them in the center.

    Which approach you take depends entirely on what effect you're going for.

  • Sorry I am really new to this. Can you give ma an example

  • edited January 2015

    Here's an example that creates a String out of chars. You would have to do something similar in your while loop.

    char x = 'x';
    char y = 'y';
    char z = 'z';
    
    String xyz = "";
    xyz = xyz + x;
    xyz = xyz + y;
    xyz = xyz + z;
    
    println(xyz);
    
  • edited January 2015
    String display = "";
    void draw() {
        background(200); 
        while (myPort.available () > 0) {
        char temperatuur = myPort.readChar();
        display = display + temperatuur;
    
      }
      text (display, width/2, height/2);
    }
    
  • That's working thanks

  • Yea, let me know if you have questions. I didn't help you learn by posting that code chunk. It would be helpful for you to learn about the String class.

  • I didn't help you learn by posting that code chunk.

    I am glad you recognize this.

  • how do I keep the output at the same location, not scrolling?

  • What do you mean? Can you post your updated code? What have you tried?

  • edited January 2015

    Now the temperature is displayed correctly. The next value after delay(1000) underneath. The next again below the last one etc. Want them displayed at the same location on the screen. Thanks

    String display = "";
    void draw() {
        background(225); 
        while (myPort.available () > 0) {
            char temperatuur = myPort.readChar();
            display = display + temperatuur;
        }
        text (display, 50, 300);
    }
    
  • edited January 2015

    What String are you reading in? It looks like you only ever read in to a single String. What are some examples of what that String contains?

    It sounds like you want to parse the input and determine when a new value is being read in, then display the new value on a new line. Have you tried that?

  • this is the output of the temperature

    25.40 25.40 25.40 25.40

    Etc

    I just want one value at the same location on the screen which can change if input is changed

  • Okay, and what char do you receive when you've got a new value? Is it a space?

    Can you detect that char and reset the String instead of constantly appending to it?

  • I changed the variable to int. Need 2 values to be on PC screen at 2 different location on the screen. variable t1 at for example 100,200 and t2 at 100,300. They need to stay on that exact location and refresh every xxx second as set in arduino

    int t1 = dht1.readTemperature(); int t2 = dht2.readTemperature();

    Serial.print(t1); Serial.print(t2);

    thanks

  • Sorry, but do you still have a question?

    If so, please post your updated code, along with what specifically in that code you're confused about. What have you tried? What happened when you tried that?

    It's hard to answer general "how do I do this" type questions, other than by simply doing your work for you (which does not help you learn). You'll have much better luck if you try something out and ask specific technical questions about the code you've tried.

  • I tried to split the sting on t element. not working I added a delimiter in the 2 values from arduino in arduino. I changed 2 values of arduino into integer and added comma delimiter. I am very new to this but cannot get it to work. Hope for a solution from which I can continue. Thanks

  • }

    String display = ""; void draw() { background(225); while (myPort.available () > 0) { char temperatuur = myPort.readChar(); display = display + temperatuur;

    }

    text (display, 50, 300);
    

    }

  •  I'm not sure I understand
    
    arduino:
    
                   {
                     Serial.print(t0); 
                     Serial.print(",");
                     Serial.println(t1);
                     delay (1000);
                    } 
    
    processing:
                        float[] temp = new float[2];
                        ....
                        ....
                        ....
                        void draw() {
                          background(200); 
                          while (myPort.available () > 0) {
                            String temperatuur = myPort.readStringUntil ('\n') ;
                            if (temperatuur != null) {
                              temp = float(split(temperatuur, ",")) ;
                            }
                          }
                          if (temp.length >=2) {   
                            text (temp[0], 10, 50);
                            text (temp[1], 10, 150);
                          }
                        }
    
  • @Camperos, thanks, that's exactly what I mean

  • @Camperos , how to get only 2 decimals?

  •   if (temp.length >=2) {   
        text (temp[0], 10, 50);
        text (temp[1], 10, 100);
    
        String t0 =nf (temp[0], 1, 2);
        text (t0, 10, 150);
    
        String t1 =nf (temp[1], 1, 2);
        text (t1, 10, 200);
      }
    }
    
  • thanks, was looking at that but no time to change that already. I will post the complete code soon

Sign In or Register to comment.