(SOLVED) Color tracking with opencv and Controllino with stepper.

Hi,

i m trying to move camera on slider based on position of color i m tracking. Camera is moved on slider with stepper motor which is controled by Controllino mini. Stepper if working nice on other code so it is tested.

i use Opencv with Python 3.5 to track color and to send to arduino over serial 1 or 2.

My idea is if Controllino recives 1 to move in one direction of for 2 in other.

i spend many hours to figure out why Controllino doesnt react while i see that Python is sending 1 or 2 like it should.

Here are my codes

Serial_control_stepper.ino (730 Bytes)

Color tracking Python.txt (2.54 KB)

I think you need: void loop() { char ch; if (Serial.available() > 0) { ch = Serial.read(); if (ch == '1') { myStepper.step(200); } else if (ch == '2') { myStepper.step(-200); } Serial.read(); } }

@kivelo, as soon as something is available then you read it and throw it away.

The Serial.available() tells you how many bytes are available. That is often just one byte. A Serial.read() will read that byte, after that it is removed from the buffer an that byte is no longer available.
The value returned by Serial.read() is put into a variable. You can see that in almost every example (Serial.read(), Serial.available(), Serial Call response, and so on).

You should define the protocol.
Do you want to send the binary code of ‘1’, or the readable ASCII character ‘1’ ?
When you use readable ASCII, then you could type it with a keyboard as well.
Is that number followed by CarriageReturn or LineFeed, or both or none or it does not matter ?

Hi,

@outsider - thanks and you are right, i change it but still i dont get stepper to move.

@koepel - im not sure i understand, indeed i want to send from python 1 (or whatever)to Controllino so it turn stepper once clockwise and 2 for counterclockwise.

i change it but still i dont get stepper to move.

You are NOT running the code you posted, then. Post your current code!

I gave three links to show examples that the return value Serial.read() is put into a variable.

Hi,

you are completly right for Serial.read().

I changed it, also there was problem in python code, it needs to be

if int(y) < 210:
ser.write(b’2’)

and arduino reads == 50, in this case.

Thank you guys!!

I changed it, also there was problem in python code, it needs to be

if int(y) < 210:
ser.write(b’2’)

and arduino reads == 50, in this case.

Depending on the type of variable that you store the value in, the Arduino might have read ‘2’ or 50.

Actually, they are the same value, it is just how that value is interpreted that changes.

If you know that you are sending ‘2’, you can test that you received 50 or ‘2’. To me, it makes more sense to test for ‘2’ if you are sending ‘2’. Looking at code that tests for the incoming character being 73 makes me fire up the web browser and look up what to send to get the code to do that (whatever that is). Looking at code that tests for the incoming character being ‘D’ does not require much brain power to know what to send to get the code to do that (whatever that is).