Python: Commandline parameters
The circuit used in this chapterFigure 1:
Two LEDs and a servo are switched in the examples of this chapter. The servo should be a microservo (also called a 9g servo), as these draw less current than standard servos.
More about how to connect LEDs to GPIOs is written in the chapter on switching LEDs via GPIOs.
Which pins have which function can be found in the chapter on the hardware of the Raspberry Pi.
Hand parameters to a script from the command lineIn addition to reading parameters from a file, as demonstrated in the previous chapter, these can also be handed to a script while started from the commandline. The following example processes parameters for two LEDs and a servo:
Save the script as "/home/pi/commandline.py" and make it executable:
chmod a+x background.py
If you call the script, you can hand the servo lever position or the duty cycle of the LEDs as parameter. The following example sets the pulse width of LED 16 to 40%:
./commandline.py -pwm16 0.4
The command to move the servo lever to 45 degrees:
./commandline.py -servo12 0.5
or with the long version:
./commandline.py --servo-angle-pin-12 0.5
Several parameters can also be transferred at the same time:
./commandline.py -servo12 -0.5 -pwm16 0.1 -pwm22 0.3