Combining Strings, converting them to an integer/ array for serial communication

edited March 2016 in Arduino

I have some code here that asks for variables one at a time and then prints them at the end. Very basic UI stuff. What I need to do is find a way to turn each string (or character as they are typed) and turn them into a long, float or variable. End goal is to send to an arduino but that comes later. Also, I know this is programmed very inefficiently. It'd be great if I could slim it down. First thing's first though. Make those strings to ints. Thanks in advance! Already tried parseInt, may not be invoking it correctly...

import processing.serial.*;
Serial myPort;

int letter;
int ypos = 25;
int Step = 0;

String input = " ";
String currentString = " ";

String distance; 
boolean distanceRec = false;

String timer1;
boolean timer1Rec = false;

String relayTimer;
boolean relayTimerRec = false;

String timer2;
boolean timer2Rec = false;

String speed;
boolean speedRec = false;

String confirm;
boolean confirmVal = false;


void setup() {
  size(640, 900);
  // Create the font
  textFont(createFont("SourceCodePro-Regular.ttf", 36));
//printArray(Serial.list());
//myPort = new Serial(this, Serial.list()[1], 9600);
}

void draw() {
  background(0);
  textSize(14);
  text("Click on the program to begin, enter distance", 25, 25);
  text(input, 25, ypos, 540, 800);

  if(distanceRec == true) {
    text("Distance= ", 25, 400); 
    text(distance, 100 ,400);
    text("Enter Timer 1" ,25, 50);
  }

  if(timer1Rec == true) {
    text("Timer 1= ", 25, 425);
    text(timer1, 100 ,425);
    text("Enter Relay Time", 25, 75);
  }

   if(relayTimerRec == true) {
    text("Relay Timer= ", 25, 450);
    text(relayTimer, 125 ,450);
    text("Enter Timer 2", 25, 100);
  }

   if(timer2Rec == true) {
    text("Timer 2= ", 25, 475);
    text(timer2, 100 ,475);
    text("Enter Speed", 25, 125);
  }

  if(speedRec == true) {
    text("Speed= ", 25, 500);
    text(speed, 100 ,500);
    text("CONFIRM THESE VALUES? Y/N", 25, 150);    
  }

  if(confirmVal == true) {
    text("Executing...", 25, 525);    
    text(distance, 0, 550);
    text(timer1, 100, 550);
    text(relayTimer, 200, 550);
    text(timer2, 300, 550);
    text(speed, 400, 550); 


   // myPort.write(distance);
   // println(distance);
   noLoop();
  }
}

void keyTyped() {
    letter = key;
    input = input + key;  

      if(letter == ENTER && Step == 0) {
      distance = input;
      distanceRec = true;
      Step++;
      letter = 0;
      input = " ";
    } 

     if(letter == ENTER && Step == 1) {
      timer1 = input;
      timer1Rec = true;
      Step++;
      letter = 0;
      input = " ";
    }

     if(letter == ENTER && Step == 2) {
      relayTimer = input;
      relayTimerRec = true;
      Step++;
      letter = 0;
      input = " ";     
   }

     if(letter == ENTER && Step == 3) {
      timer2 = input;
      timer2Rec = true;
      Step++;
      letter = 0;
      input = " ";
   }

     if(letter == ENTER && Step == 4) {
      speed = input;
      speedRec = true;
      Step++;
      letter = 0;
      input = " ";
   }

     if(letter == 121 || letter == 89 && Step == 5) {
      confirm = input;
      confirmVal = true;
      Step++;
      letter = 0;
      input = " ";
   }

    if(letter == 78 || letter == 110 && Step == 5) {
    exit();
    // redraw();
   }
}
Tagged:

Answers

Sign In or Register to comment.