Pixy2.3 camera connected to Arduino H7 lite, Please can you check my code if its good or not :)

Hello :slight_smile: i am using pixy2.3 camera connected to Arduino H7 lite, and i am driving two 12bit 333hz servos on Arduino H7 lite, for tracking an object. Please can you check my code if its good or not: PositionXmotor=MinServo+float(double((pixy.ccc.blocks[0].m_x)/pixy.frameWidth*(MaxServo - MinServo)); PositionYmotor=MinServo+float(double(pixy.ccc.blocks[0].m_y)/pixy.frameHeight*(MaxServo - MinServo));

i asked the GPT4 and he told me to use this better code with mapping:PositionXmotor = map(pixy.ccc.blocks[0].m_x, 0, pixy.frameWidth, MinServo, MaxServo); PositionYmotor = map(pixy.ccc.blocks[0].m_y, 0, pixy.frameHeight, MinServo, MaxServo);GPT4: If you want to use the Arduino H7 Lite to control your servos, you don’t need to cast the result of the calculation to float or double. so what do you Think :camera:

to get better 12bit resolution output do i need to add (int32_t) for the code like this: PositionXmotor=MinServo+float(double((int32_t)pixy.ccc.blocks[0].m_x)/(int32_t)pixy.frameWidth*(MaxServo - MinServo)); PositionYmotor=MinServo+float(double((int32_t)pixy.ccc.blocks[0].m_y)/(int32_t)pixy.frameHeight*(MaxServo - MinServo));

i got the idea from this code: panOffset = (int32_t)pixy.frameWidth/2 - (int32_t)pixy.ccc.blocks[0].m_x; tiltOffset = (int32_t)pixy.ccc.blocks[0].m_y - (int32_t)pixy.frameHeight/2;

please let me know! Thanks

Show your complete sketch. When posting your sketch, click the < CODE > icon in the edit window and paste your code where you see "type or paste code here." One way to check your code is good is to verify/compile it. If it is not correct, you will receive errors.

ChatGPT is wrong. Do the programming yourself.

1 Like

Is mapping not what you want?

int PositionXmotor = map (xposition, 0, 319, 544, 2400); // variable, minInput, maxInput, minOutput, maxOutput
1 Like

Thanks for your reply :slight_smile:
yes i need mapping, but i dont know yet if its better than this method:
PositionXmotor=MinServo+long(double(pixy.ccc.blocks[0].m_x)/pixy.frameWidth*(MaxServo - MinServo));
or this

PositionXmotor=MinServo+int(float(pixy.blocks[0].x)/pixy.frameWidth*(MaxServo - MinServo));

or this

PositionXmotor = map(pixy.ccc.blocks[0].m_x, 0, pixy.frameWidth, MinServo, MaxServo);

which one faster response time and better , or are they same performance

i dont know if need to use long(double) or not, what do you think
i am still learning programing in free time :slight_smile:

Thanks

I do not know.

1 Like