Before we copy the code, let's setup a project in the WebIDE for this guide.
First, let's create a new "Debugging" project within the "my-pi-projects" folder.
Next, let's copy and paste the below code into a newly created "blink.py" file in that "Debugging" project.
import RPi.GPIO as GPIO from time import sleep LED_PIN = 25 print "Setting up GPIO" GPIO.setmode(GPIO.BCM) GPIO.setup(LED_PIN, GPIO.OUT) def enable_led(should_enable): if should_enable: GPIO.output(LED_PIN, False) else: GPIO.output(LED_PIN, True) enable_led(False) print "LED is OFF" sleep(2) enable_led(True) print "LED is ON" sleep(2) enable_led(False) print "LED is OFF" sleep(2) enable_led(True) print "LED is ON" sleep(2) GPIO.cleanup()
We first import the required libraries. We're using GPIO pins, so we'll need GPIO, and the python sleep library to slow things down a bit at times. Next is setting up the GPIO for the LED. Starting on line 10 is our enable_led method that either turns the LED on or off based on the value of "should_enable". After that, starting at line 16, is just turning the LED on and off, with the print statements, and sleep to slow things down.
For some reason, the output from our print statements isn't matching what the LED is doing during the sleep statements.
Let's fire this up in the debugger, and see if we can find out what's going wrong.
When the file is open in the WebIDE, click the "Debug" link to activate the Debugger.
This is a rather simple program, so what we can do is just click "Step Over" for each line in our program. As we click "Step Over", the code that has the red line over it will execute, and the red line will continue on to the next line, waiting for your next instruction.
In the below screenshot you can see that we're now on line 19, and when we click "Step Over", line 19 will execute, and the red line will continue on to line 20.
An interesting thing to note here is that if you were to click "Step In" on line 19, it would then jump up into the enable_led function, and allow you to step through that function until it returned.
So, if you have a bug in one of your functions, you may need to occasionally "Step In" to one of your functions to see why it's behaving a certain way.
You should see Debug Output "print" statements as you step through, letting you know what the LED should be doing at any given moment. You can also see the Debug Variables output in the lower right. You can see that the LED_PIN is set to 25, and then you can also see we're using the sleep and GPIO imports.
Now that we've gotten this far, we can start narrowing down where we're having issues with our particular code.
When you stepped over line 16, and then line 17, you were expecting the LED to be off. We can see that we are intending on having the enable_led(False), so that line looks good. This is where "Step In" would be useful, as it looks like the issue is in our enable_led function.
It looks like we've mixed up some True and False statements in the enable_led function, so if we switch those around, and then click "Save/Restart", we should be ready to test this out again. You can either step through the program again, or just click "Run", to see if your program does what you'd expect it to.
import RPi.GPIO as GPIO from time import sleep LED_PIN = 25 print "Setting up GPIO" GPIO.setmode(GPIO.BCM) GPIO.setup(LED_PIN, GPIO.OUT) def enable_led(should_enable): if should_enable: GPIO.output(LED_PIN, True) else: GPIO.output(LED_PIN, False) enable_led(False) print "LED is OFF" sleep(2) enable_led(True) print "LED is ON" sleep(2) enable_led(False) print "LED is OFF" sleep(2) enable_led(True) print "LED is ON" sleep(2) GPIO.cleanup()
Stepping through your program, and viewing the output, and variables in real time is a pretty powerful tool that you can add in to your code/test cycle.