Connect Processing with Arduino

edited September 2017 in Arduino

Hi everybody; I'm new in this forum, well I have a question about the conecction with arduino and processing, I try to connect a simple button in arduino, the code is:

 const int botonPin =2;     // the number of the pushbutton pin

// variables will change:
int botonEstado = 1;         // variable for reading the pushbutton status

void setup() {
    Serial.begin(9600);
    pinMode(botonPin, INPUT);
}

void loop() {
    // read the state of the pushbutton value:
    botonEstado = digitalRead(botonPin);

    // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    if (botonEstado == 0) {
        Serial.println("do");
    } else {
        Serial.println("no");
    }
}

for arduino the code it's ok, for arduino the code it's ok, but when I try to get data in processing, never I can get the correct data: CODE IN PROCESSING

import processing.serial.*;

Pianos piano;
Serial mipuerto;
String numero="no";

void setup(){
  size(600,500);
  piano = new Pianos(this);
  mipuerto = new Serial(this,Serial.list()[1],9600);  //it's ok
  //printArray(Serial.list());
}

void draw(){
  background(0);
  piano.dibujar();
  if (mipuerto.available() > 0) {
    numero=mipuerto.readStringUntil('\n');
  }
  if (numero.equals("do")) {
    piano.tecla(1);
  }
  print(numero.equals("do"));
}

then, when I push the button, the serial.print must print "do", I see in the monitor serial in arduino that is correct, then I get the data with numero=mipuerto.readStringUntil('\n'); but the data never is "do" even pushing the button, the I used numero.equals("do"), but the value is false. What is I missing int this code?,

Answers

  • To help debug this you can add

    println("numero [" + numero + "]");
    

    after line 18 above. What do you see?

  • If i do: println(numero), I can see "no" or "do" string value if I press the button, I thinked that I am getting a string, but when I try to use that value in : if (numero.equals("do")), the value never is "do", I tried check the value using print(numero.equals("do")) and the return everytime is false even if I press the button

  • Hi korderoman, have you tried with: numero.strip() ?

  • hi, I dont know why, but I talked with my friend about it and him recommended me use the char type variable and all worked fine

Sign In or Register to comment.