Rookie question about classes
Posted: Tue Jan 14, 2020 10:42 am
I've got a question about classes. Learning micropython for work related stuff, and I need to use limit switches to stop motors. For ease of expansion, I thought I'd use a class and a function to setup the sensors. I followed the tutorial here https://randomnerdtutorials.com/micropy ... 2-esp8266/, and that worked. But when I try to use a pin callout instead, it doesn't work. Any thoughts?
Code: Select all
from machine import Pin from time import sleep #class Opto setup to handle an expandable number of sensors class Opto (): def __init__(self, name, sensor, motor): self.name = name self.sensor = sensor self.motor = motor def stop(self): if sensor.value() == 1: <—line 11 motor.value(0) print(name, "stopped") #one sensor for test. Input on pin 4, output on pin 5. opto1 = Opto("opto1", Pin(4, Pin.IN, Pin.PULL_DOWN), Pin(5, Pin.OUT)) #other variables led = Pin(5, Pin.OUT) #main loop while True: led.value(1) opto1.stop() <—line 23 sleep(1) /////////////////////////////////////////// Ready to download this file,please wait! ….. download ok exec(open(‘function interrupt test.py’).read(),globals()) Traceback (most recent call last): File”<stdin>”, line 1, in <module> File”<string>”, line 23, in <module> File”<string>”, line 11, in stop NameError: name ‘sensor’ isn’t defined