I2C Communication

Hi everybody,
I am on a project where I would like follow a micro-line with a robot and I have a big problem on an I2C communication between an arduino mega(SLAVE) and the OpenMV cam M7 (MASTER).
Problem :
The communication run perfectly when the arduino and the cam are connected at the computer. When I unplug the cam, it’s running but if I restart them, the communication does not start.

OpenMV code :

enable_lens_corr = False # turn on for straighter lines...

from pyb import LED
import sensor, image, time, pyb
from pyb import DAC
from  pyb import I2C

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE) # grayscale is faster
sensor.set_framesize(sensor.VGA) #
sensor.skip_frames(time = 2000)

i2c = pyb.I2C(2,I2C.MASTER)
#i2c.scan()
clock = time.clock()
dac = DAC("P6")

Yun=0
Ydeux=0
min_degree = 1
max_degree = 179
teensy = 8
angle = 0
code1 = 90

while(True):
    clock.tick()
    clock.fps()

    img = sensor.snapshot()
    #img.draw_line(0,240,640,240,color=(0,0,255),thickness=2)
    #img.draw_keypoints(1,color = (255,0,0))
    for l in img.find_lines(threshold = 4500, theta_margin = 25, rho_margin = 25):
        if (min_degree <= l.theta()) and (l.theta() <= max_degree):
            img.draw_line(l.line(), color = (255, 0, 0))
        dac.write(l.theta())
        #print("Theta %f" % l.theta())
        Yun=l.y1()
        Ydeux=l.y2()
        print("Y1= %f" %Yun)
        print("Y2= %f" %Ydeux)
        Yc=Ydeux-((Yun-Ydeux)/2)
        print("Yc    = %.4f" % Yc)
        code1 = int(l.theta())
        #print("Theta = %i" % code1)
        if(i2c.is_ready(teensy)):
            i2c.send(90 ,addr = 8, timeout=9999999)
        time.sleep(10)

Arduino code :

#include <Wire.h>

void setup() {
  
  Wire.begin(8);
  Wire.onReceive(receiveEvent);
  Serial.begin(9600);
}

void loop() {
  delay(1000);
}

void receiveEvent(int x){
  while(Wire.available()){
  int c = Wire.read();
  Serial.print("receveid: ");
  Serial.print(c);
  Serial.println();
  }
}

I’m sorry for my english, i’m french student and it’s difficult :mrgreen: .

I don’t understand the issue, do you save the code as main.py ?

Hum… When I used the computer, i click on the green arrow, to put the code on the cam. I don’t know what is the main.py… you have mat be the solution… I will test that tomorrow because it’s the night in France… Thank you a lot!

You need to save the script on the camera so it runs when you disconnect it. It should be saved as main.py. The IDE can save the script from Tools->save open script…

Okay I test that this morning. It’s strange because before this problem, when I just put the code as I say before and it ran. It’s a memory problem? It’s an I2C constraint?

Thanks a lot!!

The camera will continue to execute whatever script it is running when disconnected from the IDE until it loses power. Then on startup if not connected to the IDE it will run main.py.