# Digital write returns success but does not affect the device

**URL:** https://community.particle.io/t/digital-write-returns-success-but-does-not-affect-the-device/12675
**Category:** Developer Tools
**Created:** [June 19, 2015, 12:03am UTC](https://community.particle.io/t/digital-write-returns-success-but-does-not-affect-the-device/12675 "2015-06-19T00:03:56Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![AlDul](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@AlDul](https://community.particle.io/u/AlDul)
#### Post date: [June 19, 2015, 12:03am UTC](https://community.particle.io/t/digital-write-returns-success-but-does-not-affect-the-device/12675/1 "2015-06-19T00:03:56Z")

</div>

I am using the iOS SDK. SparkCloud is connected. I have a valid SparkDevice object.

> SparkDevice 0x1740cca90, type: Spark device, id: xxxxxxx, name: XXX, connected: true, variables: {\n}, functions: (\n digitalread,\n digitalwrite,\n analogread,\n analogwrite\n), version: (null), requires update: false, last app: (null), last heard: 2015-06-18 23:42:52 +0000

I am trying to turn on a LED and reflect its state on the screen.

```
[_myPhoton callFunction:@"digitalwrite" withArguments:@[@"D7", @1] completion:^(NSNumber *resultCode, NSError *error) {
    if (!error) {
        _blueLEDState = _tmpBlueLEDState;
        _blueLED.backgroundColor = [_blueLEDState boolValue] ? _blueOn : _blueOff;
    }
}];

```

error comes back as nil, which would indicate success. resultCode has a value of -2. What does that mean?

The device remains unaffected by this call. It is running on an iPhone, and the Particle app running on the same phone is working fine. Will the source to the Particle all be released so I can see how they did it, or is there any way I can diagnose why this call is not succeeding?

---

<div class="post-metadata">

### Author: ![AlDul](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@AlDul](https://community.particle.io/u/AlDul)
#### Post date: [June 19, 2015, 12:41am UTC](https://community.particle.io/t/digital-write-returns-success-but-does-not-affect-the-device/12675/2 "2015-06-19T00:41:24Z")

</div>

The example code for the SDK gave an argument of @1 to turn on the LED. The argument is supposed to be @“HIGH” or @“LOW”, not @1 or @0. I fixed that and my code works now. Huzzah!

---

<div class="post-metadata">

### Author: ![ido](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ido/32/11595_2.png) [@ido](https://community.particle.io/u/ido)
#### Post date: [August 17, 2015, 10:28pm UTC](https://community.particle.io/t/digital-write-returns-success-but-does-not-affect-the-device/12675/3 "2015-08-17T22:28:07Z")

</div>

Glad you found the solution.

---

<div class="post-metadata">

### Author: ![jshankara](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@jshankara](https://community.particle.io/u/jshankara)
#### Post date: [April 23, 2016, 4:22am UTC](https://community.particle.io/t/digital-write-returns-success-but-does-not-affect-the-device/12675/4 "2016-04-23T04:22:25Z")

</div>

I am trying to do the same with function name “led” and argument as “on”/“off” to turn ON/OFF the led D7 on electron

with application code:

```cpp
void setup() {
    pinMode(led,OUTPUT); // Our LED pin is output (lighting up the LED)
    Particle.variable("analogvalue", &analogvalue, INT);
    Particle.function("led",ledToggle);
}

int ledToggle(String command) {

    if (command=="on") {
        digitalWrite(led,HIGH);
        Particle.publish("G1", "tData", 60, PRIVATE);
        return 1;
    }
    else if (command=="off") {
        digitalWrite(led,LOW);
        
        Particle.publish("B", 
              "v:" + String::format("%.2f",fuel.getVCell()) + 
              ",c:" + String::format("%.2f",fuel.getSoC()),
              60, PRIVATE);
        return 0;
    }
    else if(command=="sync")
    {
        //Do something
    }
    else {
        
        return -1;
    }
}

```

ios code:

```auto
        SparkCloud.sharedInstance().getDevices { (sparkDevicesList : [AnyObject]?, error :NSError?) -> Void in
            // 2
            if let sparkDevices = sparkDevicesList as? [SparkDevice]
            {
                print(sparkDevices)
                // 3
                for device in sparkDevices
                {
                    if device.name == "myDevice"
                    {
                        // 4
                        device.callFunction("led", withArguments: ["off"], completion: { (resultCode :NSNumber?, error : NSError?) -> Void in
                            // 5
                            print("Called a function on myDevice with error ")
                            print(error)
                        })
                    }
                }
            }
        }

```

Result

```nohighlight
[<SparkDevice 0x7e01db70, type: Photon, id: XXXXXXXX, name: myDevice, connected: true, variables: {
    analogvalue = int32;
}, functions: (
    led
), version: (null), requires update: false, last app: (null), last heard: 2016-04-23 03:57:37 +0000>]

```

Called a function on myDevice with error  
nil

What is wrong here? Could you please suggest me a solution?

Thanks,  
Jay

---

<div class="post-metadata">

### Author: ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)
#### Post date: [April 23, 2016, 12:18pm UTC](https://community.particle.io/t/digital-write-returns-success-but-does-not-affect-the-device/12675/5 "2016-04-23T12:18:36Z")

</div>

Not a solution for your issue, but a side note on your firmware

You are using `String::format()` here

```cpp
Particle.publish("B", 
              "v:" + String::format("%.2f",fuel.getVCell()) + 
              ",c:" + String::format("%.2f",fuel.getSoC()),
              60, PRIVATE);

```

But why so complicated?

You can just write

```cpp
Particle.publish("B", String::format("v:%.2f,c:%.2f", fuel.getVCell(), fuel.getSoC()), PRIVATE);

```

That’s the whole point of `String::format()`.

---

<div class="post-metadata">

### Author: ![jshankara](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@jshankara](https://community.particle.io/u/jshankara)
#### Post date: [April 25, 2016, 1:50am UTC](https://community.particle.io/t/digital-write-returns-success-but-does-not-affect-the-device/12675/6 "2016-04-25T01:50:18Z")

</div>

Thanks @ScruffR.
