object creation


#1

Hello
 I’m using ARDUINO DUE
I tried to change the instance of the IDE that turns on and off the LED0 shaping the LED as a class and then creating and launching an LED object, the code compiles correctly, the problem is that at run once you get to the creation Item everything hangs. Could you help me fix it?
All implemented without using the class is working correctly.






#2

please past your code


#3

These are the sources


#4

Sorry this is the correct main with comments in English, I have uploaded the wrong file


#5

hi @francesco 
let’s go step by step.

1)if you define a class inside a module for the definition of the object you need to do obj=module_name.class_name()
in your case l=Led.Led()

2) in Viper pins are named as D0, D1, D2 etc as described here: http://doc.viperize.it/core.viper.stdlib/r0.3.3/pin_mapping.html
So, you can’t pass a number through a serial port to be used as it is for indicating the pin number to be used for the led.

here my suggestion (I don’t have any board with me right now so please try it and let me know)

Led.py

class Led:
    def init(self):
        self.pins=[D0,D1,D2,D3,D4,D5,D6] #define an array of pin names to be indexed through a number passed by the serial.read()
        
    def setPin(self, pin):
        print(“start”)
        ledpin=self.pins[pin]
        print(ledpin)
        pinMode(ledpin,OUTPUT)
        while True:
            digitalWrite(ledpin,HIGH)
            print(“high”)
            sleep(2000)
            digitalWrite(ledpin,LOW)
            print(“low”)
            sleep(2000)
main.py<br><pre class=" CodeBlock">import Led<br>import streams<br>s=streams.serial() #serial port<br>print("Insert the LED")<br><br>try:<br>&nbsp; &nbsp; pin=s.readline() #read and return the value letter<br>&nbsp; &nbsp; l=Led.Led() #object of Led class<br>&nbsp; &nbsp; print("create")<br>&nbsp; &nbsp; l.setPin(int(pin))<br>except Exception as e:<br>&nbsp; &nbsp; print(e) #use exception for tracking errorse in your code, you can read it directly in the serial monitor.&nbsp;


#6

 now it works all, thanks :slight_smile: