Allow 2 Programs to Send/Receive Serial Data?

Hello,

Right now, I'm trying to use this program to translate pen movement to computer strings of text. I'm very experienced with Processing but pretty new to the Arduino, so I apologize if this is a noob question that's really dumb :stuck_out_tongue:

My problem is the arduino file the program uses for input. Whenever a physical button is pressed, the program sends data that a python script uses that tracks the movement of an accelerometer. I'd like to instead use just a key on a keyboard instead of a physical button, and thus, I'm trying to change the code to allow for this.

My idea was to use Processing that sends a '1' as a char to the Arduino which translates to LOW in the program, and '0' which signifies HIGH. This was pretty easy to make in processing and the logic is just

  if (keyPressed && isPressed != "1") {
   isPressed = "LOW";
   port.write(isPressed);
   println("LOW");
  }
  else if (!keyPressed && isPressed != "0") {
   isPressed = "HIGH";
   port.write(isPressed);
   println("HIGH");
  }

The problem is, since I'm using both a Python script for the data, but I'm also trying to send data through the Processing program, I'm getting the error that the serial port is busy. I tried to use a SoftwareSerial port for processing that mimics wires at port 9, but I'm not sure if I understood it properly as Processing couldn't find any COM ports besides the normal Arduno one. I'm guessing I'm probably fundamentally not understanding it. I'm kind of confused on what I should do to replace a physical button in the code with keyboard strokes instead, so any help is appreciated.

Here's the Arduino code :

// AUTHOR: Federico Terzi
// Arduino sketch for the gesture controlled keyboard.
// This sketch is the BASIC version, and does not include Bluetooth capabilities,
// If you want Bluetooth, use the normal version instead.
// Part of this sketch is taken from an example made by JohnChi

#include <Wire.h>

// Pins used for I/O
int btnPin1 = 9;

// I2C address of the MPU-6050
const int MPU_addr=0x68;
// Variables that will store sensor data
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;

// Status variables, used with buttons
int precBtn1 = HIGH;

void setup(){
  // Set the pin mode of the buttons using the internal pullup resistor
  pinMode(btnPin1, INPUT_PULLUP);

  // Start the comunication with the MPU-6050 sensor
  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);

  // Start the serial communication
  Serial.begin(38400);
}
void loop(){
  // Read the values of the buttons
  int resultBtn1 = digitalRead(btnPin1);

  // 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");
}

Dinoswarleafs:
I tried to use a SoftwareSerial port for processing that mimics wires at port 9, but I'm not sure if I understood it properly as Processing couldn't find any COM ports besides the normal Arduno one.

Not surprising since SoftwareSerial is an Arduino library.

It sounds like you only have 1 Arduino and you want to send data to it with one PC program and receive data from it with a second one. Amirite?

The PC won't let two programs share a COM port. Even though one is only sending and one is only receiving. One of them has to "own" the port and it has total control. That program may talk to others on the PC, so maybe that's how two programs can work with one Arduino.

If you have a second Arduino, then that can be be a second serial port and it can send the appropriate signal to the first one as required.

If you have one of the "native USB" Arduinos such as a Due, Leonardo, Micro or Teensy then they can do multiple things - they can act as a keyboard and a joystick and a serial port all at the same time. The Teensy has the best support for this.

Or spend a few bucks on wires and a button and put the physical button next to the keyboard?

ieee488:
Not surprising since SoftwareSerial is an Arduino library.

I meant Processing couldn't find the software serial port set up on the Arduino program, making me think I don't understand it correctly since I couldn't even get that to work.

MorganS:
It sounds like you only have 1 Arduino and you want to send data to it with one PC program and receive data from it with a second one. Amirite?

The PC won't let two programs share a COM port. Even though one is only sending and one is only receiving. One of them has to "own" the port and it has total control. That program may talk to others on the PC, so maybe that's how two programs can work with one Arduino.

If you have a second Arduino, then that can be be a second serial port and it can send the appropriate signal to the first one as required.

If you have one of the "native USB" Arduinos such as a Due, Leonardo, Micro or Teensy then they can do multiple things - they can act as a keyboard and a joystick and a serial port all at the same time. The Teensy has the best support for this.

Or spend a few bucks on wires and a button and put the physical button next to the keyboard?

I do have another Arduino coming in so I suppose I'll just use that. Does SoftwareSerial not accomplish the same goal though? Is it used for a different purpose, because as I read it on the Arduino pages (https://www.arduino.cc/en/Reference/SoftwareSerial) it seems I could use it in some way with Processing.

Another way I've thought about it is implementing the key detection in the python script being used, so I guess I'll try that out.

If you use SoftwareSerial to create a second serial port on two differenr pins on the Uno you then need to connect those pins to the PC with a USB-TTL cable. If you do that then there will be two COM ports on the PC and two PC programs will be happy.

However I'm wondering why you need two PC programs. Can't you extend either the Processing or the Python program to do both jobs?

...R

Robin2:
If you use SoftwareSerial to create a second serial port on two differenr pins on the Uno you then need to connect those pins to the PC with a USB-TTL cable. If you do that then there will be two COM ports on the PC and two PC programs will be happy.

However I'm wondering why you need two PC programs. Can't you extend either the Processing or the Python program to do both jobs?

...R

Yeah I woke up this morning realizing I could probably just implement it in the python script :stuck_out_tongue: bit of fixed object use in my brain for whatever reason