0

I want to control the LEDs with my hand, for this I used the repository https://github.com/paveldat/finger_counter/tree/main. I want to control the LEDs with my hand, for this I used the repository. To connect to the arduino mega serial port, I use the pyserial library, here is the code to which I pass variables.

arduinoData = serial.Serial("com9", 9600)
arduinoData.write(bytes(str(variable), "utf-8"))

to control the LEDs and process data from the serial port, I wrote the following code in the Arduino IDE:

void setup() {
  Serial.begin(9600);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
}

void loop() {
/*
  if (Serial.available() > 0){
    serialData = Serial.read();

    for (int i = 3; i<serialData; i++){
      digitalWrite(i, HIGH);
    }
    }
*/
  if (Serial.available() > 0){
    char serialData = Serial.read();
    if (serialData == '3'){
      digitalWrite(3, HIGH);
      }
    else if (serialData == '4'){
      digitalWrite(3, HIGH);
      digitalWrite(4, HIGH);
      }
    else if (serialData == '5'){
      digitalWrite(3, HIGH);
      digitalWrite(4, HIGH);
      digitalWrite(5, HIGH);
      }
    else if (serialData == '6'){
      digitalWrite(3, HIGH);
      digitalWrite(4, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, HIGH);
      }
    else if (serialData == '7'){
      digitalWrite(3, HIGH);
      digitalWrite(4, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, HIGH);
      digitalWrite(7, HIGH);
      }
    else if (serialData == '0'){
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      }
      
    
    //Serial.println(serialData);
    //digitalWrite(serialData, HIGH);
    //delay(1000);
  }
}

when I run this code without connecting the camera and just pass the values to the serial port, then everything works. However, when I try to run this code together with the camera, the following error occurs:

serial.serialutil.SerialException: could not open port 'com9': PermissionError(13, 'Access denied.', None, 5)

The camera is a laptop webcam, I have not tested the code with the USB camera, the assumption is that the USB camera will not occupy all serial ports.

5
  • 2
    What operating system do you use? Are you sure, that "com9" is the correct port, especially when the camera is connected? (as this might change) Can you connect the camera and then open the port in the Arduino IDE?
    – chrisl
    Sep 25 at 10:48
  • There is the remote possibility that you also opened the Serial Monitor of Arduino IDE when you connected the USB camera. This may seem unrelated, but the Serial Monitor blocks other programs from using that COM port while it is running.
    – orithena
    Sep 25 at 11:21
  • when I tested the code, my IDE was basically closed @orithena Sep 25 at 13:54
  • I use windows 10 when the camera is turned on in the device manager as well as before COM9 is busy with arduino mega @chrisl Sep 25 at 13:56
  • If it works fine with one part but not the other, then it may be that both parts are attempting to use the same resources, or that your old run is still using a port and has not been killed. End your python piece with an exit call or simply del arduinoData
    – Abel
    Sep 25 at 22:52

1 Answer 1

0

The problem with accessing the port is solved, it was busy since I tried to open it every time in a loop that receives an image and displays it on the screen. I took out the code for a cycle, but the LEDs did not start. here is the updated code:

import cv2
import time
import os
import serial
import HandTrackingModule as htm

wCam, hCam = 640, 480

cap = cv2.VideoCapture(0)
cap.set(3, wCam)
cap.set(4, hCam)


folderPath = "fingers" # name of the folder, where there are images of fingers
fingerList = os.listdir(folderPath) # list of image titles in 'fingers' folder
overlayList = []
for imgPath in fingerList:
    image = cv2.imread(f'{folderPath}/{imgPath}')
    overlayList.append(image)


pTime = 0

detector = htm.handDetector(detectionCon=0.75)
totalFingers = 0

def arduino_write(totalFingers):
    arduinoData = serial.Serial("com9", 9600)
    arduinoData.write(bytes(str(totalFingers+2)+"\n", "utf-8"))
    arduinoData.close()

while True:
    sucess, img = cap.read()
    img = cv2.flip(img, 1)

    img = detector.findHands(img)
    lmList, bbox = detector.findPosition(img, draw=False)

    if lmList:
        fingersUp = detector.fingersUp()
        totalFingers = fingersUp.count(1)

    h, w, c = overlayList[totalFingers].shape
    img[0:h, 0:w] = overlayList[totalFingers]

    cTime = time.time()
    fps = 1/ (cTime-pTime)
    pTime = cTime

    cv2.putText(img, f'FPS: {int(fps)}', (400, 70), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)
    cv2.rectangle(img, (20, 225), (170, 425), (0, 255, 0), cv2.FILLED)
    cv2.putText(img, str(totalFingers), (45, 375), cv2.FONT_HERSHEY_PLAIN, 10, (255, 0, 0), 25)

    arduino_write(totalFingers)
      
    cv2.imshow("Image", img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break


cap.release()
cv2.destroyAllWindows()

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.