from machine import Pin
import time
# Initialisation des variables et compteurs
compteur_OK = 0 # Compteur des pièces OK
compteur_NOK = 0 # Compteur des pièces NOK
total_pieces = 0 # Compteur total de pièces
# Configuration des GPIO
signal_cognex_ok_pin = Pin(14, Pin.IN)
signal_cognex_nok_pin = Pin(27, Pin.IN)
reset_commande_hmi_pin = Pin(26, Pin.IN)
# Fonction pour incrémenter les compteurs
def increment_ok():
global compteur_OK
compteur_OK += 1
def increment_nok():
global compteur_NOK
compteur_NOK += 1
# Détection des défauts
def detection_defauts():
global compteur_OK, compteur_NOK, total_pieces
if signal_cognex_ok_pin.value(): # Signal OK reçu de la caméra
increment_ok()
elif signal_cognex_nok_pin.value(): # Signal NOK reçu de la caméra
increment_nok()
total_pieces = compteur_OK + compteur_NOK
# Gestion des alarmes
def gestion_alarmes():
if compteur_NOK >= 50: # Seuil NOK critique
alarme_nok = True # Active une alarme si dépassement
else:
alarme_nok = False
# Commandes HMI
def reinitialisation_hmi():
global compteur_OK, compteur_NOK, total_pieces
if reset_commande_hmi_pin.value(): # Commande depuis HMI
compteur_OK = 0
compteur_NOK = 0
total_pieces = 0
# Boucle principale
while True:
detection_defauts()
gestion_alarmes()
reinitialisation_hmi()
# Affichage des compteurs
print("Compteur OK:", compteur_OK)
print("Compteur NOK:", compteur_NOK)
print("Total pièces:", total_pieces)
time.sleep_ms(2000)