Issues in tutorials for blink led over the net and variables and functions (pt1)

Hi there,
If you are having some issues with some of the tutorials, I noticed something a bit weird that caused me an issue. Hope this helps.

First, in the Controlling LEDs over the NET example, I added the LED to pin 0 and set up the internal LED on pin 7.

  // -----------------------------------
  // Controlling LEDs over the Internet
  // -----------------------------------

  // name the pins
  int led1 = D0;
  int led2 = D7;

  // This routine runs only once upon reset
  void setup()
  {
     //Register our Spark function here
     Spark.function("led", ledControl);

     // Configure the pins to be outputs
     pinMode(led1, OUTPUT);
     pinMode(led2, OUTPUT);

     // Initialize both the LEDs to be OFF
     digitalWrite(led1, LOW);
     digitalWrite(led2, LOW);
  }


  // This routine loops forever
  void loop()
  {
     // Nothing to do here
  }


  // This function gets called whenever there is a matching API request
  // the command string format is l<led number>,<state>
  // for example: l1,HIGH or l1,LOW
  //              l2,HIGH or l2,LOW

  int ledControl(String command)
  {
     int state = 0;
     //find out the pin number and convert the ascii to integer
     int pinNumber = (command.charAt(1) - '0') - 1;
     //Sanity check to see if the pin numbers are within limits
     if (pinNumber < 0 || pinNumber > 8) return -1;

     // find out the state of the led
     if(command.substring(3,7) == "HIGH") state = 1;
     else if(command.substring(3,6) == "LOW") state = 0;
     else return -1;

     // write to the appropriate pin
     digitalWrite(pinNumber, state);
     return 1;
  }

No major changes there, However, I noticed that if you pass l1, the led on D0 lights, but if you pass l2, the internal does not. That’s because the code is looking for pin number which doesn’t seem to be zero indexed. It’s a bit odd and confusing since you define the pins as led1 and led2, so you think that 1 and 2 get mapped, but that’s not what the code does. My version above will pass the HIGH to pin d0 or d7… if you send it l1 or l8. Which brings me to my next helpful hint.

There is a bit of HTML code in the Variables and Functions (part1) tutorial that doesn’t make any sense because it pushes to the function “LED”, which is not defined in “Variables and Functions”. However, that HTML is hella handy if you want to turn on the LED using the net example without curl (shudder).

Here’s my adapted code that turns on the internal LED if you are using my adapted code above.

    <!-- Replace your-device-ID-goes-here with your actual device ID
and replace your-access-token-goes-here with your actual access token-->
<center>
<br>
<br>
<br>
<form action="https://api.particle.io/v1/devices/device id here/led?access_token=put your access token here" method="POST">
Tell your device what to do!<br>
<br>
<input type="radio" name="args" value="l8,HIGH">Turn the LED on.
<br>
<input type="radio" name="args" value="l8,LOW">Turn the LED off.
<br>
<br>
<input type="submit" value="Do it!">
</form>
</center>

Anyhoo, hope that helps save some confusion. The tutorial probably could be cleaned up a bit, since there’s a bunch of superfluous code in there, but… It’s bedtime :slight_smile:

1 Like

Cool we were talking about editing it to use D7 instead.

Will update the example soon.

Thanks! :smile:

Hi, all! Sorry for any confusion in the Variables and Functions code, looks like someone had pushed a change that made it really confusing. I have set it right now, hopefully that won’t happen again!

2 Likes