Serial port disconnects, when programmable power socket is activated

Hi there :slight_smile:

I´m working on an experiment atm that requires an arduino to listen to key presses ("1" to "7") from my laptop (via CoolTerm Serial Terminal). Furthermore, i also use a programmable power socket, in order to start/power another device when a specific (different) key ("z") is pressed on the keyboard.
Both parts work perfectly fine on their own. But when both are being used, the key press that starts the power socket, also leads to CoolTerm disconnecting the serial port, before reconnecting after 2-3 sec.
With this delay my experiment unfortunately doesnt work.

Does anyone have an idea where my problem/mistake could be located?
I´m very thankful for any help i can get :slight_smile:

this is the short python code, that starts the programmable power socket by running a manufacturer .exe :

import subprocess, keyboard

from pynput.keyboard import Listener


def on_press(key):
    if keyboard.is_pressed('z'):
        subprocess.call(['C:\\Users\\Julius\\USBswitchCmd.exe', '1'])


def on_release(key):
    subprocess.call(['C:\\Users\\Julius\\USBswitchCmd.exe', '0'])


with Listener(on_press=on_press, on_release=on_release) as listener:  # Create an instance of Listener
    listener.join()

and this is the arduino code, that starts a specific servo when a specific key is pressed on the keyboard:

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
int val; // initial value of input

Adafruit_PWMServoDriver myServo = Adafruit_PWMServoDriver();

#define SERVOMIN 150
#define SERVOMAX 600

uint8_t servonum = 0;
uint8_t numberOfServos = 7;

void setup() {
  Serial.begin(9600);
  myServo.begin();
  myServo.setPWMFreq(60);
  delay(10);

  myServo.setPWM(0, 0, 150); // set starting point
  myServo.setPWM(1, 0, 150);
  myServo.setPWM(2, 0, 150);
  myServo.setPWM(3, 0, 150);
  myServo.setPWM(4, 0, 150);
  myServo.setPWM(5, 0, 150);
  myServo.setPWM(6, 0, 150);

}

void loop() {

if (Serial.available()) // if serial value is available 

  {
    val = Serial.read();// then read the serial value

    if (val == '1') //if value input is equals to 1

    {
      myServo.setPWM(0, 0, 400);
      delay(15);//delay for the servo to get to the position
     }

    if (val == '2') //if value input is equals to 2
    {
      myServo.setPWM(1, 0, 400);
      delay(15);//delay for the servo to get to the position
    }
    if (val == '3') //if value input is equals to 2
    {
      myServo.setPWM(2, 0, 400);
      delay(15);//delay for the servo to get to the position
    }
    if (val == '4') //if value input is equals to 2
    {
      myServo.setPWM(3, 0, 400);
      delay(15);//delay for the servo to get to the position
    }
    if (val == '5') //if value input is equals to 2
    {
      myServo.setPWM(4, 0, 400);
      delay(15);//delay for the servo to get to the position
    }
    if (val == '6') //if value input is equals to 2
    {
      myServo.setPWM(5, 0, 400);
      delay(15);//delay for the servo to get to the position
    }
    if (val == '7') //if value input is equals to 2
    {
      myServo.setPWM(6, 0, 400);
      delay(15);//delay for the servo to get to the position
    }

    if (val == 'a') // zum wieder schließen!

    {
      myServo.setPWM(0, 0, 150);
      delay(15);//delay for the servo to get to the position
     }

    if (val == 's') //if value input is equals to 2
    {
      myServo.setPWM(1, 0, 150);
      delay(15);//delay for the servo to get to the position
    }
    if (val == 'd') //if value input is equals to 2
    {
      myServo.setPWM(2, 0, 150);
      delay(15);//delay for the servo to get to the position
    }
    if (val == 'f') //if value input is equals to 2
    {
      myServo.setPWM(3, 0, 150);
      delay(15);//delay for the servo to get to the position
    }
    if (val == 'g') //if value input is equals to 2
    {
      myServo.setPWM(4, 0, 150);
      delay(15);//delay for the servo to get to the position
    }
    if (val == 'h') //if value input is equals to 2
    {
      myServo.setPWM(5, 0, 150);
      delay(15);//delay for the servo to get to the position
    }
    if (val == 'j') //if value input is equals to 2
    {
      myServo.setPWM(6, 0, 150);
      delay(15);//delay for the servo to get to the position
    }

  }

}

I´m sorry, if this isnt the right category for my question - I´m completely new to this forum!

Most likely you do a connect/reconnect to the arduino for each keypress -> arduino resets and bootloder waits 3 sec.

1 Like

Do you know of a way to avoid that?

Try ArduinoIDE serial monitor and try again. If it's the same problem: most likely your power connection is bad (power spike or momentary short)

There are a few tutorials on the forum for using python in combination with Arduino. One is in "Introductory Tutorials" and one in this section of the forum (Demo of PC-Arduino comms using Python). Study both of them.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.