Total newbie scripting question



  • I'm using the Omega to illuminate LEDs, and for this purpose software PWM works great. For example, the command-line command

    fast-gpio pwm 14 200 25

    lights a (red) LED connected to GPIO 14 at 25% duty cycle with a frequency of 200 Hz.

    I have created a simple sh script ("red.sh) containing just a single line:

    fast-gpio pwm 14 200 $1

    so I can just type

    red 25

    at the command line to obtain the same results. Everything is working fine, but I'd like to do some range checking on the argument $1 to ensure that it is between 0 and 100 (duty cycle between 0% and 100%). I've tried things like

    if [$1 < 0] then echo "value is under-range"
    else if [$1 > 100] then echo "value is over-range"
    else fast-gpio pwm 14 200 $1

    but I keep getting errors. How do I accomplish this simple task? I've tried looking at various tutorials on "sh scripting" but nothing is working so far. I admit to being a total newbie at Linux and its variants, so I don't really know where to turn. I believe that the Omega is running a version of OpenWRT, but I haven't been able to uncover much about scripting in OpenWRT. Help!!


  • administrators

    @Jeff-Verive sh is really picky about the syntax, try the following:

    if [ $1 < 0 ]
    then
       echo "value is under-range"
    elif [ $1 > 100 ] 
    then
       echo "value is over-range"
    else
       fast-gpio pwm 14 200 $1
    fi
    

    A few things to note:

    1. For the square brackets in your if statements, there needs to be a space after [ and a space before ]
    2. To create an else if you need to use the keyword elif
    3. The then after an if or elif needs to either be on the next line or have a semi-colon after the ]
    4. The whole thing has to be closed with a fi


  • @Jeff-Verive see my request about starting with markdown ...
    https://community.onion.io/topic/707/markdown-how-to-start-with-it
    this might help you to display correct code in the forum.



  • @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!


Log in to reply
 

Looks like your connection to Onion Community was lost, please wait while we try to reconnect.