Help with updating a variable

I am probably missing something easy, but, I can not seem to get this to work.

Basically, PIN D2 and D3 are both inputs and initially pulled up to HIGH.

I set a variable that I would like to be text to indicate status of a door based on the readings from an upper and lower sensor.

No matter what I try, the value stays at “No sensors”. I can get the values to change by reverting to “int” or numbers only, but, I would like to return text when the variable is called.

Any thoughts?

char *door1state = "No Sensors";     // Initially set door1state to 3 which means no sensor logic applied
int Door1upsensor;
int Door1downsensor;
char *Door1statu;
int Door1UP;



void setup() {
    
    
    Spark.variable("Door1statu", door1state, STRING);
    Spark.variable("Door1UP", &Door1upsensor, INT);
    Spark.variable("Door1DOWN", &Door1downsensor, INT);
    
    
    // Configure D2 - D3 as Inputs with the pull-up resistor "On"
   pinMode(D2, INPUT_PULLUP);       // set pin to INPUT mode, this pin is for door 1 open microswitch
   pinMode(D3, INPUT_PULLUP);       // set pin to INPUT mode, this pin is for door 1 closed microswitch
   

}

void loop() {
    
Door1upsensor = digitalRead(D2);
Door1downsensor = digitalRead(D3);


if (Door1upsensor == 0 && Door1downsensor == 1) door1state = "OPEN";

// if (Door1upsensor == 1) door1state = "UP1TEST";

if (Door1upsensor == 1 && Door1downsensor == 0) door1state = "CLOSED";


}

Thank you in advance!

–Rod

Try avoiding using comparison operators (like ==) for checking the value of int data types. Maybe your conditions are never being met in this “if” statements…

Hrmm… nope, nevermind, it must be something else.

I am not sure what you mean. Door1upsensor is tied to the digital read of D2. I setup two other variables so I could indeed see the value from digital read of D2 and D3 and I am able to create both a 0 and 1 for each. Basically, the pull up puts both pins at a 1 and then when I short a pin to ground it becomes a zero.

In other words, I can read Door1UP and get a 0 or a 1 depending on my jumpers and the same for Door1DOWN.

Can you clarify what you mean?

I should also note that if I change the variable Door1statu to an int, I am able to then use the exact same if conditions to change the value of that variable to any integer i want when the conditions are met.

–Rod

Hi @Jovack

I think you are having a problem with string pointers. C style strings don’t work like regular variables such a int or char. The Spark variable get a pointer to a specific memory location that holds the value “No Sensors” and is 11 locations long, 10 for the string and one for the null terminator. When you try to set this to a new value , you cannot just make it equal to a new value since that just makes the pointer point to a new spot in memory.

Try it this way:

const char nomsg[] = "No Sensors";
const char opmsg[] = "Open";
const char clmsg[] = "Closed";

char *door1state[32];     
int Door1upsensor;
int Door1downsensor;
char *Door1statu;
int Door1UP;



void setup() {

    strcpy(door1state, nomsg);
    Spark.variable("Door1statu", door1state, STRING);
    Spark.variable("Door1UP", &Door1upsensor, INT);
    Spark.variable("Door1DOWN", &Door1downsensor, INT);


    // Configure D2 - D3 as Inputs with the pull-up resistor "On"
   pinMode(D2, INPUT_PULLUP);       // set pin to INPUT mode, this pin is for door 1 open microswitch
   pinMode(D3, INPUT_PULLUP);       // set pin to INPUT mode, this pin is for door 1 closed microswitch


}

void loop() {

Door1upsensor = digitalRead(D2);
Door1downsensor = digitalRead(D3);


if (Door1upsensor == 0 && Door1downsensor == 1) {
  strcpy(door1state, opmsg);
}

// if (Door1upsensor == 1) door1state = "UP1TEST";

if (Door1upsensor == 1 && Door1downsensor == 0) {
  strcpy(door1state,clmsg);
}

}

I didn’t get a chance to test this so there might be typos.

2 Likes

Well, I think the issue is assigning pointers to character arrays like “OPEN” and “CLOSED” to a char* with a specific block of memory set aside. I think you might want to look at String references for Wiring for Arduino (since Wiring is compatible with spark core).

Otherwise you can try to delete and redeclare the variable whenever you need to change it like:

if (door1state) delete door1state;
if ((door1state = new char[5])!=NULL) {
    memset(door1state, 0, 5);
  } /* 5 for 'O', 'P', 'E', 'N', 'NULL', more for 'CLOSED' */
door1state = "OPEN";

Hey All!

I opened an issue here that I think might also make this easier:

Thanks!
David