strange beavier

edited May 2015 in Arduino

Hello all,

I am trying to get in the processing world but still the first steps.

My problem is that i am trying to write a value to the serial port, it is all ok as long i do not use capital letter or simbols if so it intruduces a ? before.

How can i avoid it?

My code is:

import processing.serial.*;

String letter = "";
String words = "";

String portName;
Serial myPort;                       // The serial port

String cont;

void setup() {
  size(300, 300);
  textFont(createFont("Georgia", 36));
  portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
}

void draw() {
  if ( myPort.available() > 0) {  // If data is available,read it and store it in val
    inByte();}
  background(220);

  textSize(25);
  fill(0,102,153);
  text("Insert text:" + letter, 10, 50); 

  textSize(25);
  fill(0,102,153);
  text("Text inserted: " + words, 10, 100);

  textSize(25);
  fill(0,102,0);
  text("Contagens: " + cont, 10, 200);   
}

void keyPressed() { 
  if (key == '\n'){
    words = letter;
    myPort.write(words);
    println(words);
    letter = ""; }
  else {
    letter = letter + key; }
}

void inByte(){
  boolean teste = false;
  int in = myPort.read();
  if (in == 67){  // C em ascii
    cont="";
    cont = myPort.readStringUntil('\n'); }
}

Thanks alot, Timoteo

Answers

  • edited April 2015

    It is because you probably press shift to get the capital letter or symbol... So keypress records that too... Make line 42 as else if(key>=' ' && key <='~'){ if you check the ascii table you'll find that space and tilde('~') are the left and right extremes of all graphic character so basically what this snippet does is append only graphics characters to letter.

  • Hello srinag, Thanks for the answer. i have already found that was the problem but your sugestion is must simpler to implement than my solution. Thanks, Timóteo

  • I first read "strange beaver"...

    made me laugh...

    ;-)

  • Answer ✓

    Could you hit the 'yes' button and accept the ans? This q is still in the unanswered section..

Sign In or Register to comment.