Problems sending strings to arduino

Hello arduino community,i’d like some help regarding processing commmunication via the serial port
Here’s the deal,i have installed the controlp5 library and created a GUI to type 30 values which i need to map to 3 arrays in my sketch,but they are not mapping correctly.
Here’s the relevant part of my code…
Processing

myPort.write(GPH1.getText());  
  delay(100);
  myPort.write(GPH2.getText());
  delay(100);
  myPort.write(GPH3.getText());
  delay(100);
  myPort.write(GPH4.getText());
  delay(100);
  myPort.write(GPH5.getText());
  delay(100);
  myPort.write(GPH6.getText());
  delay(100);
  myPort.write(GPH7.getText());
  delay(100);
  myPort.write(GPH8.getText());
  delay(100);
  myPort.write(GPH9.getText());
  delay(100);
  myPort.write(GPH10.getText());
  delay(100);
  myPort.write(TEMP1.getText());
  delay(100);
  myPort.write(TEMP2.getText());
  delay(100);
  myPort.write(TEMP3.getText());
  delay(100);
  myPort.write(TEMP4.getText());
  delay(100);
  myPort.write(TEMP5.getText());
  delay(100);
  myPort.write(TEMP6.getText());
  delay(100);
  myPort.write(TEMP7.getText());
  delay(100);
  myPort.write(TEMP8.getText());
  delay(100);
  myPort.write(TEMP9.getText());
  delay(100);
  myPort.write(TEMP10.getText());
  delay(100);
  myPort.write(TEMPO1.getText());
  delay(100);
  myPort.write(TEMPO2.getText());
  delay(100);
  myPort.write(TEMPO3.getText());
  delay(100);
  myPort.write(TEMPO4.getText());
  delay(100);
  myPort.write(TEMPO5.getText());
  delay(100);
  myPort.write(TEMPO6.getText());
  delay(100);
  myPort.write(TEMPO7.getText());
  delay(100);
  myPort.write(TEMPO8.getText());
  delay(100);
  myPort.write(TEMPO9.getText());
  delay(100);
  myPort.write(TEMPO10.getText());
  delay(100);

Keep in mind the getText() call returns a string

Arduino

while (go == false){
      if(Serial.available() > 0){
      while (Serial.available() > 0) {
    int inChar = Serial.read();
    if (isDigit(inChar)) {
      // convert the incoming byte to a char 
      // and add it to the string:
      inString += (char)inChar; 
    }
    
    
  }
  if(x < 10){
gph[x] = inString.toInt();
       x++;
       inString = "";
   }else if (y <10){
     tpat[y] = inString.toInt();
     y++;
     inString = "";
   }else if(z<10){
     temppat[z] = inString.toInt();
     z++;
     inString = "";
     if (z == 9){
       go = true;
     }
   }
      }
    }

Edit:
{
GPH is a float array
tpat is a double array
temppat is an int array
}
Thanks in advance! :slight_smile:

You will have to post your complete Arduino sketch. And please use Tools / Auto Format to make it easy to follow. You have some very strange bracket indentation.

Why are you trying to integer values into floating point variables?

…R

Well,it’s pretty big and a mess,but here goes…
Arduino

 #include <PID_v1.h>
 #define RelayPin 7
 String inString = "";
 int x = 0;
 int y = 0;
 int z = 0;
 boolean go = false;
  int relay = 7;
  float tempC, tempM;
  int tempin = 0;
  double t = millis();
  double t2 = millis(), tpid = 0, tfap = 0;
  double ton = 0;
  double toff = 1000;
  double tciclo = millis();
  boolean estado = false;
  float oldtemp;
  float gph[9];
  float gphm;
  float r;
  float n;
  int i, ipid;
  int e = 0;
  int temppat[9]; //temperaturas nas quais se quer estabilizar antes de prosseguir com a rampa até a tempalvo
  double tpat[9];//tempo de duração para cada patamar da curva
  int v = 0; //variavel para indice de temppat
  int j = 0; //variavel para indice de tpat
  double t10 = 0; //usado no while do patamar
  double Setpoint, Input, Output;
  
  PID myPID(&Input, &Output, &Setpoint,20,0.07,0.002, DIRECT);
  
  int WindowSize = 1000;
  unsigned long windowStartTime;
  
  void setup()
  {
    pinMode(7,OUTPUT);
    Serial.begin(9600);
    windowStartTime = millis();
    Setpoint = temppat[v];
    myPID.SetOutputLimits(0, WindowSize);
    myPID.SetMode(AUTOMATIC);
    pinMode(8,OUTPUT);
    pinMode(9,OUTPUT);
    pinMode(10,OUTPUT);
  }
  
  void loop()
  {
    while (go == false){
      if(Serial.available() > 0){
      while (Serial.available() > 0) {
    int inChar = Serial.read();
    if (isDigit(inChar)) {
      // convert the incoming byte to a char 
      // and add it to the string:
      inString += (char)inChar; 
    }
    
    
  }
  if(x < 10){
gph[x] = inString.toInt();
       x++;
       inString = "";
   }else if (y <10){
     tpat[y] = inString.toInt();
     y++;
     inString = "";
   }else if(z<10){
     temppat[z] = inString.toInt();
     z++;
     inString = "";
   }else{
    go = true;
         ton = 1;
         toff = 999; 
   }
      }
    }
     
    
  if(tempC<(temppat[v]-3) && e <= 1000){ 
     e = 0;
         tempC = analogRead(tempin);
         tempC = tempC/1023 *5 *100;
    trocar();
    trocat();
    
   if (millis() - t> 2000)
    {
   //  Serial.print("Temperatura: ");
   //  Serial.println(tempC);
   //  Serial.print("Graus por min: ");
   //  Serial.println(gphm/60);
   //  Serial.print("Fator de aumento R: ");
   //  Serial.println(r);
   //  Serial.print("gph[v]: ");
     Serial.println(gph[v]);
     Serial.println(temppat[v]);
     Serial.println(tpat[v]);
   //  Serial.print("Tempo ligado: ");
    // Serial.println(ton);
   //  Serial.print("Tempo desligado: ");
   //  Serial.println(toff);
      t = millis();
    }
  
   }else {
     e++;
   }
  
  //PID começa aqui//
   if (e > 1000){
     
    tpid = millis();
    while(millis() - tpid < tpat[j]){
     tempC = analogRead(tempin);
     tempC = tempC/1023 *5 *100;
      for(ipid=0,tempM = 0;ipid<25;ipid++){ 
      tempC = analogRead(tempin);
      tempM = tempM + tempC;
      }
      tempM = tempM/25;
      Input = (tempM);
      myPID.Compute();
    
      if(millis() - windowStartTime>WindowSize)
      { 
        windowStartTime += WindowSize;
      }
      if(Output < millis() - windowStartTime) {
        digitalWrite(RelayPin,LOW);
    
      }
      
      else {
        digitalWrite(RelayPin,HIGH);
    
      }  
       tfap = millis();
       tfap = tfap/1000;
       if (millis() - t> 1000)
        {
          Serial.print(tempM);
          Serial.print("   ");
          Serial.println(tfap);
          t = millis();
        }
       }  //fecha o while do PID
  
      v++;
      j++;
      e=0;
      t2=millis();
      if(digitalRead(relay)==HIGH){
        estado = true;
      }else{
        estado =false;
      }
      }
  
  
  }
  void trocar() {
      n = 0;
      i = 0;
     while(millis() - t2 < 3000){
      trocat();
        tempC = analogRead(tempin);
         tempC = tempC/1023 *5 *100;
        i++;
        n = n+tempC;
      
      }
      n = n/i;
      t2 = millis();
       
    
     gphm = (n - oldtemp)*1200;
     
     r = 1 + (gph[v] - gphm)/(100*abs(ton));
     if(r < 0.05 || r>15){ 
       r = 1;
     }
     
    
     ton = ton*r;
     if(ton == 0){
       ton = 2;
     }
     toff = 1000-ton;
     oldtemp = n;
     
    }
  
  void trocat() {
    if(estado == false){
      if (millis() - tciclo > ton){
        tciclo = millis();
      digitalWrite(relay,LOW);
      estado = true;
    //  Serial.println("liguei");
      }
    }
    if(estado == true){
      if (millis() - tciclo > toff){
        tciclo = millis();
      digitalWrite(relay,HIGH);
      estado = false;
     // Serial.println("desliguei");
      }
    }
  }

Im using floating point because i need precision in the temperature readings…
Attached a picture of my project,it’s a toaster turned into a programable sample heater
Sending the processing sketch in the next post,too big for a single one

Processing:

String go;
import controlP5.*;
import processing.serial.*;
ControlP5 cp5;
Serial myPort;
int myColor = color(255);

int c1,c2;

float n,n1;
Textfield GPH1;
Textfield GPH2;
Textfield GPH3;
Textfield GPH4;
Textfield GPH5;
Textfield GPH6;
Textfield GPH7;
Textfield GPH8;
Textfield GPH9;
Textfield GPH10;
Textfield TEMP1;
Textfield TEMP2;
Textfield TEMP3;
Textfield TEMP4;
Textfield TEMP5;
Textfield TEMP6;
Textfield TEMP7;
Textfield TEMP8;
Textfield TEMP9;
Textfield TEMP10;
Textfield TEMPO1;
Textfield TEMPO2;
Textfield TEMPO3;
Textfield TEMPO4;
Textfield TEMPO5;
Textfield TEMPO6;
Textfield TEMPO7;
Textfield TEMPO8;
Textfield TEMPO9;
Textfield TEMPO10;
void setup() {
  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
  size(1000,700);
  noStroke();
  cp5 = new ControlP5(this);
  PFont font = createFont("arial",20);
  // create a new button with name 'buttonA'
  cp5.addButton("Start")
     .setValue(37)
     .setPosition(600,300)
     .setSize(200,19)
     ;
  
  // and add another 2 buttons

GPH1 = cp5.addTextfield("GPH1")
     .setPosition(20,20)
     .setSize(200,19)
     .setFont(font)
     .setFocus(true)
     .setColor(color(255,0,0))
     .setText("0")
     ;
GPH2 = cp5.addTextfield("GPH2")
     .setPosition(20,60)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setText("0")
     .setColor(color(255,0,0))
     ; 
 GPH3 = cp5.addTextfield("GPH3")
 .setText("0")
     .setPosition(20,100)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ;
 GPH4 = cp5.addTextfield("GPH4")
 .setText("0")
     .setPosition(20,140)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ;  
 GPH5 = cp5.addTextfield("GPH5")
 .setText("0")
     .setPosition(20,180)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ;      
 GPH6 = cp5.addTextfield("GPH6")
 .setText("0")
     .setPosition(300,20)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ;      
 GPH7 = cp5.addTextfield("GPH7")
 .setText("0")
     .setPosition(300,60)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ;  
 GPH8 = cp5.addTextfield("GPH8")
 .setText("0")
     .setPosition(300,100)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ;      
      GPH9 = cp5.addTextfield("GPH9")
      .setText("0")
     .setPosition(300,140)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ;      
      GPH10 = cp5.addTextfield("GPH10")
      .setText("0")
     .setPosition(300,180)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ;      
TEMP1 = cp5.addTextfield("TEMP1")
.setText("0")
     .setPosition(20,220)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ;
TEMP2 = cp5.addTextfield("TEMP2")
.setText("0")
     .setPosition(20,260)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ; 
 TEMP3 = cp5.addTextfield("TEMP3")
 .setText("0")
     .setPosition(20,300)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ;
 TEMP4 = cp5.addTextfield("TEMP4")
 .setText("0")
     .setPosition(20,340)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ;  
 TEMP5 = cp5.addTextfield("TEMP5")
 .setText("0")
     .setPosition(20,380)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ;      
 TEMP6 = cp5.addTextfield("TEMP6")
 .setText("0")
     .setPosition(300,220)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ;      
 TEMP7 = cp5.addTextfield("TEMP7")
 .setText("0")
     .setPosition(300,260)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ;  
 TEMP8 = cp5.addTextfield("TEMP8")
 .setText("0")
     .setPosition(300,300)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ;      
  TEMP9 = cp5.addTextfield("TEMP9")
  .setText("0")
     .setPosition(300,340)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ;      
  TEMP10 = cp5.addTextfield("TEMP10")
  .setText("0")
     .setPosition(300,380)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ; 
     TEMPO1 = cp5.addTextfield("TEMPO1")
     .setText("0")
     .setPosition(20,420)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ;
TEMPO2 = cp5.addTextfield("TEMPO2")
.setText("0")
     .setPosition(20,460)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ; 
 TEMPO3 = cp5.addTextfield("TEMPO3")
 .setText("0")
     .setPosition(20,500)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ;
 TEMPO4 = cp5.addTextfield("TEMPO4")
 .setText("0")
     .setPosition(20,540)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ;  
 TEMPO5 = cp5.addTextfield("TEMPO5")
 .setText("0")
     .setPosition(20,580)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ;      
 TEMPO6 = cp5.addTextfield("TEMPO6")
 .setText("0")
     .setPosition(300,420)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ;      
 TEMPO7 = cp5.addTextfield("TEMPO7")
 .setText("0")
     .setPosition(300,460)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ;  
 TEMPO8 = cp5.addTextfield("TEMPO8")
 .setText("0")
     .setPosition(300,500)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ;      
  TEMPO9 = cp5.addTextfield("TEMPO9")
  .setText("0")
     .setPosition(300,540)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ;      
  TEMPO10 = cp5.addTextfield("TEMPO1O")
  .setText("0")
     .setPosition(300,580)
     .setSize(200,19)
     .setFont(font)
     .setFocus(false)
     .setColor(color(255,0,0))
     ; 
}

void draw() {
  int val;
  if (0 < myPort.available()) {  // If data is available,
   val = myPort.read();    // read it and store it in val
   println(val);
 }
  
  background(myColor);
  myColor = lerpColor(c1,c2,n);
  n += (1-n)* 0.1;
 
}

public void controlEvent(ControlEvent theEvent) {
  println(theEvent.getController().getName());
  n = 0;
}

public void Start(int theValue) {
  
  myPort.write(GPH1.getText());
  
  delay(100);
  
  myPort.write(GPH2.getText());
  
  delay(100);
  myPort.write(GPH3.getText());
 
  delay(100);
  myPort.write(GPH4.getText());
  delay(100);
  myPort.write(GPH5.getText());
  delay(100);
  myPort.write(GPH6.getText());
  delay(100);
  myPort.write(GPH7.getText());
  delay(100);
  myPort.write(GPH8.getText());
  delay(100);
  myPort.write(GPH9.getText());
  delay(100);
  myPort.write(GPH10.getText());
  delay(100);
  myPort.write(TEMP1.getText());
  delay(100);
  myPort.write(TEMP2.getText());
  delay(100);
  myPort.write(TEMP3.getText());
  delay(100);
  myPort.write(TEMP4.getText());
  delay(100);
  myPort.write(TEMP5.getText());
  delay(100);
  myPort.write(TEMP6.getText());
  delay(100);
  myPort.write(TEMP7.getText());
  delay(100);
  myPort.write(TEMP8.getText());
  delay(100);
  myPort.write(TEMP9.getText());
  delay(100);
  myPort.write(TEMP10.getText());
  delay(100);
  myPort.write(TEMPO1.getText());
  delay(100);
  myPort.write(TEMPO2.getText());
  delay(100);
  myPort.write(TEMPO3.getText());
  delay(100);
  myPort.write(TEMPO4.getText());
  delay(100);
  myPort.write(TEMPO5.getText());
  delay(100);
  myPort.write(TEMPO6.getText());
  delay(100);
  myPort.write(TEMPO7.getText());
  delay(100);
  myPort.write(TEMPO8.getText());
  delay(100);
  myPort.write(TEMPO9.getText());
  delay(100);
  myPort.write(TEMPO10.getText());
  delay(100);
  
  
  
  c1 = c2;
  c2 = color(0,160,100);
  
}