UART data communication to arduino board question?

Hello there Openmv ,
I’m having problems trying to read color blob data coming into my Arduino board,
I’m trying to read and serial print same blob data from openmv
I don’t know whats wrong, I’m new to openmv cam and I hope
bellow is the picture.I see 49,57 instead of 16 …please I appreciate your help, as I have spent days trying to solve the problem …thanks

Hi, can you test with a serial terminal and see if you’re getting the expected data ?

Please post your OpenMV Cam code and Arduino code.

OK ,Here’s the openmv code

# Single Color RGB565 Blob Tracking Example
import sensor, image, time
import time
from pyb import UART
uart = UART(3,9600, timeout_char = 1000)

threshold_index = 0 # 0 for red, 1 for green, 2 for blue
thresholds = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds
              (30, 100, -64, -8, -32, 32), # generic_green_thresholds
              (0, 30, 0, 64, -128, 0)] # generic_blue_thresholds

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()

while(True):
    clock.tick()
    img = sensor.snapshot()
    for blob in img.find_blobs([thresholds[threshold_index]], pixels_threshold=200, area_threshold=200, merge=True):
        img.draw_rectangle(blob.rect())
        img.draw_cross(blob.cx(), blob.cy())
    uart.write("%d"%blob.cx())   

    print(blob.cx())

and here’s the arduino code

int indata;
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
Serial.begin(9600);
mySerial.begin(9600); 
}
void loop() {
indata = mySerial.read();


 Serial.print(indata);
 Serial.print("\n");
}

Try this code:

# Single Color RGB565 Blob Tracking Example
import sensor, image, time
import time
from pyb import UART
uart = UART(3,9600, timeout_char = 1000)

threshold_index = 0 # 0 for red, 1 for green, 2 for blue
thresholds = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds
              (30, 100, -64, -8, -32, 32), # generic_green_thresholds
              (0, 30, 0, 64, -128, 0)] # generic_blue_thresholds

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()

while(True):
    clock.tick()
    img = sensor.snapshot()
    for blob in img.find_blobs([thresholds[threshold_index]], pixels_threshold=200, area_threshold=200, merge=True):
        img.draw_rectangle(blob.rect())
        img.draw_cross(blob.cx(), blob.cy())
        uart.write("%d\n"%blob.cx())   
        print("%d\n"%blob.cx(), end='')

You had the wrong indentation in python. Also, you can’t use “blob” outside of the loop. Whatever value you were using was a stale value.

On the Arduino side:

https://www.google.com/search?q=arduino+readline&oq=arduino+readline&aqs=chrome..69i57j0l5.2799j0j7&sourceid=chrome&ie=UTF-8

You need a method to read a line of text. I.e. keep reading text until you see ‘/n’.

Thanks so much for your reply Sir,
now I’m 80% done, I have used the openmv code u sent and I have adjusted the Arduino code, the results are like this
I can receive the data from openmv but I need to reset before I can get a new data , that means I have to reset every time to get real-time data from openmv
here is the result …Any help to resolve this i will really appreciate .thanks in advance

the arduino code

String str;
int ds = 4;
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
Serial.begin(9600);
mySerial.begin(9600); 
}

void loop(){
   if (mySerial.available()){
    delay(100); // Wait for all data.
    while (mySerial.available()) {
        char d = mySerial.read();
        str.concat(d);
    }
        }
        char t[str.length()+1];
        str.toCharArray(t, (sizeof(t)));
        int intdata = atoi(t);
        Serial.print(intdata);
         Serial.print("\n");
         delay(100);
   }


Try the attached. You might see some compile errors.
sketch_feb17a.zip (449 Bytes)

Thanks Sir , Everything worked well finally made a simple robot here Arduino + Openmv Cam7 ,Object Tracking Robot - YouTube

Good job! Yo may wish to turn your PID gains down a bit.