0

I'm trying to create a class to define various led status using 3 leds. For some status only I need to use GPIO.output method, but in other cases I want to use GPIO.PWM method to create a blink effect on the leds.

I spent about three hours but I can't discover the trouble, the code is the following:

#!/usr/bin/env python

import RPi.GPIO as GPIO
import time

LED_RED = 11
LED_GREEN = 7
LED_YELLOW = 12

LEDS = [LED_RED, LED_GREEN, LED_YELLOW]


##################################################################################
##                          LEDS STATUS DEFINITIONS                             ##
##################################################################################
##       STATUS       ##  LED RED  ##  LED GREEN  ##  LED YELLOW  ##  FUNCTION  ##
##################################################################################
## 'ON'               ##     1     ##      0      ##       0      ##   OUTPUT   ##
## 'FORWARD'          ##     1     ##      0      ##       1      ##   OUTPUT   ## 
## 'BACKWARD'         ##     1     ##      1      ##       0      ##   OUTPUT   ##
## 'MECH. BRAKE'      ##     1     ##      1      ##       1      ##   OUTPUT   ##
## 'INERTIAL BRAKE'   ##     1     ##    1(0.5)   ##    1(0.5)    ##    PWM     ##
## 'OVER CURRENT A'   ##     1     ##      1      ##    1(0.5)    ##    PWM     ## 
## 'OVER CURRENT B'   ##     1     ##    1(0.5)   ##       1      ##    PWM     ##
## 'LOSS CONNECTION'  ##   1(0.5)  ##    1(0.5)   ##    1(0.5)    ##    PWM     ##
################################################################################## 

STATUS = {'ON': (1, 0, 0), 'FORWARD': (1, 0, 1), 'BACKWARD': (1, 1, 0), 'M_BRAKE': (1, 1, 1), 'I_BRAKE': (0,2,2), 'OC_A': (0, 0, 2), 'OC_B': (2, 2, 2)}

DC = 50


class LedStatus():
    global STATUS, LEDS, DC

    def __init__(self):
        self.STATUS = STATUS
        self.LEDS = LEDS
        self.setup_pins()

    def on(self):
        print("[INFO]: Led Status ON")
        for i in range(len(LEDS)):
            GPIO.output(LEDS[i], STATUS['ON'][i])

    def forward(self):
        print("[INFO]: Led Status FORWARD")
        for i in range(len(LEDS)):
            GPIO.output(LEDS[i], STATUS['FORWARD'][i])

    def backward(self):
        print("[INFO]: Led Status BACKWARD")
        for i in range(len(LEDS)):
            GPIO.output(LEDS[i], STATUS['BACKWARD'][i])

    def m_brake(self):
        print("[INFO]: Led Status M_BRAKE")
        for i in range(len(LEDS)):
            GPIO.output(LEDS[i], STATUS['M_BRAKE'][i])

    def i_brake(self):
        print("[INFO]: Led Status I_BRAKE")
        L_R = GPIO.PWM(LEDS[0], STATUS['M_BRAKE'][0])
        L_R.start(DC)
        L_G = GPIO.PWM(LEDS[1], STATUS['M_BRAKE'][1])
        L_G.start(DC)
        L_Y = GPIO.PWM(LEDS[2], STATUS['M_BRAKE'][2])
        L_Y.start(DC)       

    def ocurrent_a(self):
        print("[INFO]: Led Status OVER CURRENT MOTOR A - OC_A")
        L_R = GPIO.PWM(LEDS[0], STATUS['OC_A'][0])
        L_R.start(DC)
        L_G = GPIO.PWM(LEDS[1], STATUS['OC_A'][1])
        L_G.start(DC)
        L_Y = GPIO.PWM(LEDS[2], STATUS['OC_A'][2])
        L_Y.start(DC)

    def ocurrent_b(self):
        print("[INFO]: Led Status OVER CURRENT MOTOR B - OC_B")
        L_R = GPIO.PWM(LEDS[0], STATUS['OC_B'][0])
        L_R.start(DC)
        L_G = GPIO.PWM(LEDS[1], STATUS['OC_B'][1])
        L_G.start(DC)
        L_Y = GPIO.PWM(LEDS[2], STATUS['OC_B'][2])
        L_Y.start(DC)

    def setup_pins(self):
        GPIO.setmode(GPIO.BOARD)
        GPIO.setwarnings(False)
        for i in range(len(LEDS)):
            GPIO.setup(LEDS[i], GPIO.OUT)
        print("[INFO]: Pines {}, {} y {}, seteados como salidas").format(LEDS[0], LEDS[1], LEDS[2])


def test():
    status = LedStatus()
    status.on()
    time.sleep(5)
    status.forward()
    time.sleep(5)
    status.backward()
    time.sleep(5)
    status.m_brake()
    time.sleep(5)
    status.i_brake()
    time.sleep(5)
    status.ocurrent_a()
    time.sleep(5)
    status.ocurrent_b()
    time.sleep(5)
    status.on()

test()

The returned error is the follow:

L_R.start(DC)
NameError: name 'L_R' is not defined

I accept any kind of help or comments about the code, greetings and thanks for your time.

1

Finally I discover that the problem it was not the code. The problem was an indentantion error imperceptible in Sublime Text. I only can see this problem opening the code with nano editor. The solution was select all the code (ctrl+a) and then click on the "Tab Size:" message on the right-bottom corner of Sublime Text interface and select "Convert indentation to tabs" option.

Reference: https://stackoverflow.com/questions/10022018/how-to-debug-indentation-errors-in-python

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.