J
@Lazar-Demin
Thanks a bunch! Your code almost worked, but the syntax that actually worked (to control an LED connected to GPIO 14) was as follows:
#!/bin/sh
if [ $1 -gt "100" ]
then
echo "value is over-range"
echo "use a whole number from 0 to 100"
elif [ $1 -lt "0" ]
then
echo "value is under-range"
echo "use a whole number from 0 to 100"
elif [ $1 -eq "0" ]
then
fast-gpio pwm 14 200 101
else
fast-gpio pwm 14 200 $1
fi
Indeed, sh is extremely picky about syntax, but once I saw some working examples, I was able to develop working code. By the way, this code is stored as file called red, so the way it is used is as follows:
red value
where value is the duty-cycle percentage requested, from 0 for minimum brightness (OFF) to 100 (fully on). For example, to set the duty cycle to 40% we enter
red 40
which lights up the LED using a 50% duty cycle. One peculiar aspect of PWM using fast-gpio is that at 0% duty-cycle (which should theoretically have no ON time) the output still contains pulses (probably to satisfy servos), so an LED still illuminates - though dimly. Using a duty-cycle value greater than 100:
fast-gpio pwm 14 200 101
actually produces 0% duty cycle. Since this is not intuitive for the average intended user of my code, I substitute 101 for 0 in the code. Cheating? Maybe, but the user is none the wiser!