Processing with SoftwareSerial Trouble

Hello,

Right now I’m trying to send input from Processing over to Arduino for me to observe on the Serial Reader. I’ve read from both here and here on how to make it happen, but the serial is still busy whenever opening the serial reader, probably signaling that Processing is still using the normal serial port and not the software one.

Here is my Processing code :

import processing.serial.*;

Serial port;
boolean isPressed = false;

void setup() {
  frameRate(300);
  port = new Serial(this, Serial.list()[1], 9600);
  println("HIGH");
}

void draw() {
  if (keyPressed && !isPressed) {
   isPressed = true;
   port.write("LOW");
   println("LOW");
  }
  else if (!keyPressed && isPressed) {
   isPressed = false;
   port.write("HIGH");
   println("HIGH");
  }
}

Here are the snippets from the Arduino code that matter in this case (from Federico Terzi) :

#include <Wire.h>
#include <SoftwareSerial.h>

// Pins used for I/O
SoftwareSerial buttonPort(8, 9);

...

int precBtn1 = HIGH;
int resultBtn1;


void setup(){

...

  // Start the serial communication
  buttonPort.begin(38400);
  Serial.begin(38400);
}

void loop(){
  
  buttonPort.listen();
  // Transmit Processing values
  while (buttonPort.available() > 0)
   resultBtn1 = buttonPort.read();

  // ON btn1 pressed, start the batch and light up the yellow LED
  if (precBtn1 == HIGH && resultBtn1 == LOW)
  {
    startBatch();
  }

  // If the btn1 is pressed, reads the data from the sensor and sends it through the communication channel
  if (resultBtn1==LOW)
  {
    // Start the transmission with the MPU-6050 sensor
    Wire.beginTransmission(MPU_addr);
    Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
    Wire.endTransmission(false);
    Wire.requestFrom(MPU_addr,14,true);  // request a total of 14 registers
    
    // Reads the data from the sensor
    AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)    
    AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
    AcZ=Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
    Tmp=Wire.read()<<8|Wire.read();  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
    GyX=Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
    GyY=Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
    GyZ=Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)

    Serial.print("START");
    Serial.print(" "); Serial.print(AcX);
    Serial.print(" "); Serial.print(AcY);
    Serial.print(" "); Serial.print(AcZ);
    Serial.print(" "); Serial.print(GyX);
    Serial.print(" "); Serial.print(GyY);
    Serial.print(" "); Serial.print(GyZ);
    Serial.println(" END");
  }

  // Closes the batch when the button is released
  if (precBtn1 == LOW && resultBtn1 == HIGH)
  {
    closeBatch();
  }

  // Saves the button states
  precBtn1 = resultBtn1;
}

// Sends the started batch signal
void startBatch()
{
  Serial.println("STARTING BATCH");
}

// Sends the closed batch signal
void closeBatch()
{
  Serial.println("CLOSING BATCH");
}

If you want to connect a PC to a SoftwareSerial port on an Arduino you will need a USB-TTL cable.

Why do you want to use SoftwareSerial? If you explain what you are trying to achieve you will get better advice.

...R

Robin2:
If you want to connect a PC to a SoftwareSerial port on an Arduino you will need a USB-TTL cable.

Why do you want to use SoftwareSerial? If you explain what you are trying to achieve you will get better advice.

...R

Oh, I thought I could use it in a virtual sense. Yikes.

Essentially, I want to have a Processing window where I can have the keyboard act as a physical button, so if you hold a key it's like holding down a button. The information of the accelerometer being used is going to be used in a Python script for storage, and is only outputted once the input of the button/keyboard is on. I tried to get this input directly with Python, but it was surprisingly difficult to include a segment in said script to have the keyboard like the button. I made a seperate post that was made before this Python hindsight, and my suggestions were either another Arduino (which I have, but I'm not sure how I could make it work since both power GNDs and A4 and A5 are being taken up by the accelerometer), including the input in the python script (attempted to but couldn't get), or just getting the button which I would like to avoid.

It's just for personal use, so the Processing method, while being a bit excessive having another program on the Arduino, is the easiest method in my case hypothetically, so I'm looking for advice to make it possible.

...and if you want a second serial port on pins 8 & 9, use AltSoftSerial. It is very efficient, unlike SoftwareSerial. To connect it to your PC, you must use a USB-to-serial adapter (aka FTDI), connected to pins 8 & 9.

Here are the options for connecting something through a serial port, from best to worst:

* BEST: The fastest, most reliable way to connect a device (or the PC via USB) is to use a HardwareSerial port (e.g., the Serial object).

On any Arduino board, you can connect a device (or the PC via USB) to the Serial pins (0 & 1). You can still print debug statements, and they will show up on the Serial Monitor window (or in a PC app). Characters sent to the Arduino (from the device or the PC) will not interfere with those prints, and you will not see those characters on the Serial Monitor window.

However, when you need to upload a new sketch to the Arduino, you must disconnect from the Arduino RX pin 0. Otherwise, the device will interfere with the uploaded data. If it's just the PC connected, you (obviously) don't have to disconnect it.

For Mega, Due and Teensy boards, you can connect the device (or PC via a USB-to-serial adapter) to the Serial1, Serial2 or Serial3 pins.

For Micro and Leo (and other 32U4-based Arduinos), you can connect the device/PC to the Serial1 pins.

* 2nd Best: If you can't connect the device to a HardwareSerial port, you should download and install the AltSoftSerial library. This library only works on two specific pins (8 & 9 on an UNO). This library is very efficient and reliable. It uses one of the hardware TIMERs, so it may conflict with libraries that use TIMERs or PWM output (e.g., servo).

* 3rd Best: If you can't use the pins required by AltSoftSerial, and your device runs at 9600, 19200 or 38400 baud, you should download and install the NeoSWSerial library. This library is almost as efficient. It will help you avoid common timing problems caused by SoftwareSerial. It does not need an extra TIMER, so it can be used with most other libraries. It does use Pin Change Interrupts, but there is an option in the header file that allows you to coordinate other PCI usage with NeoSWSerial.

NeoSWSerial can be used with AltSoftSerial at the same time, allowing your sketch to have two extra serial ports.

* WORST: SoftwareSerial is NOT RECOMMENDED, because it disables interrupts for long periods of time. This can interfere with other parts of your sketch, or with other libraries. It cannot transmit and receive at the same time, and your sketch can only receive from one SoftwareSerial instance at time.

AltSoftSerial and NeoSWSerial are available from the Arduino IDE Library Manager, under the menu Sketch -> Include Library -> Manage Libraries.