Data Type Conversion

Okay so how do I do data type conversions? I think it’ such a basic question that I’m worried I’m asking a dumb question.

I want to publish a boolean value (a door is open or closed). So I can get the value of the sensor on the door by checking variable = digitalRead(D0). But when I publish the value I want to do spark.publish(“variable”, variable).

Variable must be declared as: int variable=0; but the datatype of the publish function is char* how do I convert?

This works:

char *v;

void setup( void) {
  Spark.variable( "v", v, String);
};

void loop( void) {
  int itswhat;
  //code to determine itswhat
  //these are equivalent, choose one
  v = itswhat ? "It's true" : "It's false";
  //v = itswhat==0 ? "It's false" : "It's true";
  //if( itswhat) v="It's true" else v="It's false";
  //if( itswhat == 0) v="It's false" else v="It's true";
}

You can also use sprintf() but I think you need to read a C-primer.

Checkout the string class here: http://docs.spark.io/firmware/#string-class-string for easy Arduino-y conversions from types to strings :smile:

Thanks,
David

Yeah I saw that in the docs, but I’m not getting it to work. Check this:

int ledval = 0;
char ledstring = “”;
String ledstringtwo ="";

void setup() {
    pinMode(D7, OUTPUT);
}

void loop() {
    ledval = digitalRead(D0);
    ledstring = String(ledval);
ledstringtwo = String(ledval);
}

seems like it should work to me but it won’t even verify. I get an error with the char data type declaration at the top, and the string data type which works on arduino, and I get an error on converting to string.