I am making a wireless gesture control hand and for that I am taking input through serial port from pycharm and then I am sending that data array through nrf24l01 I am facing a issue with confirming is data i am sending is correct and is receiving at the receiver end or not also. There can be a issue of data transmission also.
Python Opencv Output [1,0,0,0,1]
expected output from transmitter $10001
Expected input from receiver=$10001
> Transmitter Code
#include <Servo.h>
#include <SPI.h>
#include <RF24.h>
#define numOfValsRec 5
#define digitsPerValRec 1
RF24 radio(9, 8);
const byte address[6] = "00001"; //either add radio address here or in pipe
int msg[5];
int valsRec[numOfValsRec];
int stringLength = numOfValsRec * digitsPerValRec +1;
int counter=0;
bool counterStart=false;
String receivedString;
//const uint64_t pipe = 0xE8E8F0F0E1LL;
void setup() {
Serial.begin(19200);
radio.begin();
radio.openWritingPipe(address);
radio.stopListening();
}
void SendingData(){
while(Serial.available())
{
char c = Serial.read();
if(c=='$'){
counterStart=true;
}
if (counterStart){
if(counter<stringLength){
receivedString = String(receivedString+c);
counter++;
}
if(counter>=stringLength){
for(int i=0;i<numOfValsRec;i++){
int num = (i * digitsPerValRec)+1;
valsRec[i]=receivedString.substring(num,num+digitsPerValRec).toInt();
}
receivedString = "";
counter = 0;
counterStart = false;
}
}
}
}
void loop() {
SendingData();
for (int i = 0; i < 5; i = i + 1){
radio.write(valsRec, sizeof(valsRec));
delay(5);}
}
>Receiver Code
#include <RF24.h>
#include <RF24_config.h>
#include <nRF24L01.h>
#include <printf.h>
#include <Servo.h>
#include <SPI.h>
#include <RF24.h>
#define numOfValsRec 5
#define digitsPerValRec 1
Servo servoThumb;
Servo servoIndex;
Servo servoMiddle;
Servo servoRing;
Servo servoPinky;
RF24 radio(9, 8);
const byte address [6] = "00001"; //either add radio address here or in pipe
//const uint64_t pipe = 0xE8E8F0F0E1LL;
struct Data_Package {
byte servoThumb;
byte servoIndex;
byte servoMiddle;
byte servoRing;
byte servoPinky;
};
void setup() {
Serial.begin(19200);
servoThumb.attach(1);
servoIndex.attach(2);
servoMiddle.attach(3);
servoRing.attach(4);
servoPinky.attach(5);
radio.begin();
radio.openReadingPipe(1, address);
radio.startListening();
}
void loop() {
while(radio.available()){
Serial.println("data come ");
int valsRec[5];
radio.read(&valsRec, sizeof(valsRec));
if (valsRec[0]==1){servoThumb.write(180);}else{servoThumb.write(0);}
if (valsRec[1]==1){servoIndex.write(180);}else{servoIndex.write(0);}
if (valsRec[2]==1){servoMiddle.write(180);}else{servoMiddle.write(0);}
if (valsRec[3]==1){servoRing.write(180);}else{servoRing.write(0);}
if (valsRec[4]==1){servoPinky.write(180);}else{servoPinky.write(0);}
}
}
Python Script
import cv2
from cvzone.HandTrackingModule import HandDetector
from cvzone.SerialModule import SerialObject
arduino = SerialObject("COM5")
cap = cv2.VideoCapture(0)
detector = HandDetector(detectionCon=0.8, maxHands=2)
while True:
success, img = cap.read()
hands, img = detector.findHands(img)
if hands:
hand1 = hands[0]
lmList1 = hand1["lmList"]
bbox1 = hand1["bbox"]
centerPoint1 = hand1["center"]
handType1 = hand1["type"]
fingers1 = detector.fingersUp(hand1)
if len(hands) == 2:
hand2 = hands[1]
lmList2 = hand2["lmList"]
bbox2 = hand2["bbox"]
centerPoint2 = hand2["center"]
handType2 = hand2["type"]
fingers2 = detector.fingersUp(hand2)
print(fingers1, fingers2)
else:
# print(len(lmList1),lmList1)
# print(bbox1)
# print(centerPoint1)
print(fingers1)
arduino.sendData(fingers1)
cv2.imshow("Image", img)
cv2.waitKey(1)
Output Of Python that need to be communicated through serial
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[0, 1, 1, 1, 1]
[0, 1, 1, 1, 1]
[0, 1, 1, 1, 1]
[0, 1, 1, 1, 1]
[0, 1, 1, 1, 1]
[0, 1, 1, 1, 1]
[0, 1, 1, 1, 1]
[0, 1, 1, 1, 1]
[0, 1, 1, 1, 1]
[0, 1, 1, 1, 1]
[0, 1, 1, 1, 1]
[0, 1, 1, 1, 1]
[0, 1, 1, 1, 1]
[0, 1, 1, 1, 1]
[0, 1, 1, 1, 1]
[0, 1, 1, 1, 1]
[0, 1, 1, 1, 1]
[0, 1, 1, 1, 1]
Please help with this issue