4

I'm a beginner in programming and looking for a solution for a windows user- interface to change parameters on a arduino board.

The whole target is a control unit for camera, flash and valves to make photos of water-drops. Hardware and arduino IDE is already running, but changing timing in Arduino IDE sketch is not very friendly.

So I made my first tests for serial data transfer based on the answer of user apodidae in How can I use Processing to make a GUI that sends data to Arduino and receives data back to display in the GUI?

Now I have the following problem:

When I use it without changes, it will send 2 datasets. The second will overwrite the first with value 0. image serial overwrite

Excluding line 120-122 will send only one dataset, but there is a shift and the first value is missing image serial datashift

Can some explain and show me a solution?

Processing code

import controlP5.*;
import java.util.*;
import processing.serial.*;

ControlP5 cp5;

Serial myPort;


PFont font;
PFont fontB;
PFont fontT;

int ypos_event_1 = 50;
int ypos_event_step = 50;

int xpos_event_A = 80;

int xpos_event_S = 180;

int xpos_event_D = 320;

int[] sliderVal = {0, 10, 20, 0, 30, 40};

int slider0Value = sliderVal[0];
int slider1Value = sliderVal[1];
int slider2Value = sliderVal[2];

int slider3Value = sliderVal[3];
int slider4Value = sliderVal[4];
int slider5Value = sliderVal[5];

void setup() {
  size(600, 1000);
  surface.setTitle("ValveTime");
  cp5 = new ControlP5(this);
  font = createFont("calibri bold", 8);    // custom fonts for 
  fontB = createFont("calibri bold", 12);    // custom fonts for buttons
  fontT = createFont("calibri bold", 20);    // custom fonts for title
  
  
  //* add a ScrollableList
List l = Arrays.asList("Leer","Kamera", "Blitz_1", "Blitz_2", "Blitz_3", "Vent_1", "Vent_2", "Vent_3", "Vent_4");

  // slider value linked to variable 'sliderNumValue'
  cp5.addScrollableList("slider0Value")
    .setPosition( xpos_event_A, ypos_event_1)
    .setSize(80, 50)
    .setBarHeight(20)
    .setItemHeight(20)
    .setFont(font)
    .addItems(l)
    .setCaptionLabel(" A1")
    ;
  cp5.addSlider("slider1Value")
    .setPosition(xpos_event_S, ypos_event_1)
    .setHeight(24)
    .setRange(0, 10000)
    .setFont(font)
    .setCaptionLabel(" S1")
    ;
  cp5.addSlider("slider2Value")
    .setPosition(xpos_event_D, ypos_event_1)
    .setHeight(24)
    .setRange(0, 1000)
    .setFont(font)
    .setCaptionLabel(" D1")
    ;

  cp5.addScrollableList("slider3Value")
    .setPosition( xpos_event_A, (ypos_event_1+1*ypos_event_step))
    .setSize(80, 50)
    .setBarHeight(20)
    .setItemHeight(20)
    .setFont(font)
    .addItems(l)
    .setCaptionLabel(" A2")
    ;
  cp5.addSlider("slider4Value")
    .setPosition(xpos_event_S, (ypos_event_1+1*ypos_event_step))
    .setHeight(24)
    .setRange(0, 10000)
    .setFont(font)
    .setCaptionLabel(" S2")
    ;  
  cp5.addSlider("slider5Value")
    .setPosition(xpos_event_D, (ypos_event_1+1*ypos_event_step))
    .setHeight(24)
    .setRange(0, 1000)
    .setFont(font)
    .setCaptionLabel(" D2")
    ;

  // Send_Data button
  cp5.addButton("Send_Data")
    .setPosition(220, 900)
    .setSize(100, 24)
    ;
// printArray(Serial.list());
  myPort = new Serial(this, "COM3", 9600); 
 
}

void draw() {
  background(0);
}

public void Send_Data() {
  println("Send_Data button hit.");
  sliderVal[0] = slider0Value;
  sliderVal[1] = slider1Value;
  sliderVal[2] = slider2Value;
  sliderVal[3] = slider3Value;
  sliderVal[4] = slider4Value;
  sliderVal[5] = slider5Value;

  printArray(sliderVal);

  for (int i = 0; i < (sliderVal.length); ++i) {
//   if (i == 0) {
//      myPort.write("\n"); //So first value will show up
//    }
    myPort.write(sliderVal[i]+"\n");
  }
}

void serialEvent(Serial p) {
  String inStr = p.readStringUntil('\n');
  if (inStr != null) {
    print("received: ", inStr);
  }  
}

Arduino code

int event_1A = 0;
int event_1S = 0;
int event_1D = 0;
int event_2A = 0;
int event_2S = 0;
int event_2D = 20;

int busyPin = 8;

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    Serial.readStringUntil('\n');
    Serial.println("#################");
    event_1A = Serial.parseInt();
    Serial.println(event_1A);
    event_1S = Serial.parseInt();
    Serial.println(event_1S);
    event_1D = Serial.parseInt();
    Serial.println(event_1D);
    event_2A = Serial.parseInt();
    Serial.println(event_2A);
    event_2S = Serial.parseInt();
    Serial.println(event_2S);
    event_2D = Serial.parseInt();
    Serial.println(event_2D);
    Serial.println("=================");
 
  }
  if (event_1A == 6) 
    digitalWrite (busyPin, HIGH);
    
  else
    digitalWrite (busyPin, LOW);
  
}
2
  • Try to remove or comment Serial.readStringUntil('\n'); Has the issue gone? Commented Nov 16 at 20:10
  • when I comment Serial.readStringUntil('\n'); in Arduino sketch, I'll get always 2 datasets, also when I comment line 120-122 in processing sketch. Commented Nov 16 at 20:25

1 Answer 1

3

I can reproduce the problem using an Arduino UNO R3 and the Processing IDE.

The Java code needs to send a carriage return to signal the start of transmission. Without this start signal, the microcontroller misses the first array value and the entire array is shifted.

  myPort.write("\n");  //<-- added

  for (int i = 0; i < (sliderVal.length); ++i) {
    // send data array
  }

And after reading the integer array sent by the above code, the Arduino needs to clear the final carriage return. Otherwise it mistakes the final return as the start of of another data packet.

...
event_2D = Serial.parseInt();
Serial.println(event_2D);
Serial.readString();  // <---- added

These changes fix the problems described. However, the code is not very robust and reviewing examples in Serial Input Basics - updated might be helpful.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.