MicroPython Color detection

Hello, I am working on having the OpenMV cam h7 detect between green and red using pin P0 for red and pin P1 for green that I can then use in Arduino to detect what color is present and have my LCD display its findings such as “red found”, “green found” or “no color detected”. The problem is after I show the camera either color, for example green the value does reset and will have the LCD keep repeating that same message and when I show it the other color it wont print either color only “no color detected”. I am having trouble on how to write the code in micropython to have the pin go high to activate and low to turn off so that the value does not stay high it will only print the message as long as the color is present or no color is present. (If I worded my question right)

Please post your code.

As for color tracking… find_blobs() returns a python list. You are probably trying to do everything in the for loop. You should assign the return value of fund_blobs() to a variable and then use that in the for loop. Finally, reset the LED if the list is empty

len(list) == 0

import sensor,image,time,math,pyb
from pyb import Pin


thresholds = [(47,52,70,77,49,62), # Red threshold
(28,32,-38,-31,18,32)] # Green threshold

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, pixels_threshold=100, area_threshold=100, merge=True):
if blob.code() == 1: #RED
p = pyb.Pin(“P0”,pyb.Pin.OUT_PP)
p.value(1)# high 3.3v
p.value(0) # low 0v
print(“RED”)
if blob.code() == 2: #GREEN
p = pyb.Pin(“P1”,pyb.Pin.OUT_PP)
p.value(1)# high 3.3v
p.value(0) # low 0v
print(“GREEN”)

(for arduino)
#include<SoftwareSerial.h>    //Libary Already in your IDE
const int Cam = 3; //where sensor is connected on board 
const int Cam2 = 10; //where second cam pin is 
int val = 0; //red 
int val2 = 0; // green
const int rxPin= 7; // Not used, but needed for command below
const int txPin= 8; // Connect SER input to this pin
const char message1[] = "Flash run like hell";
const char message2[] = "Green Lantern fly away";
const char message3[] = "No sign of life";
SoftwareSerial mySerial= SoftwareSerial(rxPin, txPin);

void setup() {
  // put your setup code here, to run once:
pinMode(Cam,INPUT);       //set the prin direction to input
pinMode(Cam2,INPUT); 
Serial.begin(9600);       //establish serial buad rate
pinMode(txPin, OUTPUT);   // define pin mode for tx:
digitalWrite(txPin, LOW); // Stop bit state for inverted serial
mySerial.begin(9600);     // Set the data rate
delay(1000);             // wait for 1000ms to establish com
}

void loop() {
  // put your main code here, to run repeatedly:
 int val = digitalRead(Cam); //read value from sensor 
  Serial.println(val);   
  delay(1000); //wait 1sec 
 int val2 = digitalRead(Cam2); //read value from sensor 
  Serial.println(val2);   
  delay(1000); //wait 1sec 
  
if (val < 1 && val2 > 0)   //red
{
mySerial.print(message1); //Prints "flash run like hell"
delay(5000);
mySerial.write(12);       //Clears screen and set to position 0,0
delay(1000);               //Need a minimum of 5ms after a clear command
}
else if (val > 0 && val2 < 1)    //green
{
mySerial.print(message2);   //Prints "Green Lantern fly away"
delay(1000);
mySerial.write(12);
delay(1000);
}
else      //no color found 
{
mySerial.print(message3);   //Prints  "No sign of life"
delay(1000);
mySerial.write(12);
delay(1000);
}
}

Can you repost you code using the code tags? I can’t read it.

the first or second

Both pieces of code. The code tags button in the editor preserves formatting.

import sensor,image,time,math,pyb
from pyb import Pin


thresholds = [(47,52,70,77,49,62),  # Red threshold
             (28,32,-38,-31,18,32)] # Green threshold

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, pixels_threshold=100, area_threshold=100, merge=True):
            if blob.code() == 1: #RED
                p1 = pyb.Pin("P0",pyb.Pin.OUT_PP)
                p1.value(1)# high 3.3v
                p1.value(0) # low 0v
                print("RED")
            if blob.code() == 2: #GREEN
                p2 = pyb.Pin("P1",pyb.Pin.OUT_PP)
                p2.value(1)# high 3.3v
                p2.value(0) # low 0v
                print("GREEN")
import sensor,image,time,math,pyb
from pyb import Pin


thresholds = [(47,52,70,77,49,62),  # Red threshold
             (28,32,-38,-31,18,32)] # Green threshold

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()
        blob_list = img.find_blobs(thresholds, pixels_threshold=100, area_threshold=100, merge=True)
        for blob in blob_list:
            if blob.code() == 1: #RED
                p1 = pyb.Pin("P0",pyb.Pin.OUT_PP)
                p1.value(1)# high 3.3v
                p1.value(0) # low 0v
                print("RED")
            if blob.code() == 2: #GREEN
                p2 = pyb.Pin("P1",pyb.Pin.OUT_PP)
                p2.value(1)# high 3.3v
                p2.value(0) # low 0v
                print("GREEN")
        if not blob_list:
            print("no color detected")