Struggling with my code

hi or should i say HIGH

i have been trying to write code to publish an event and also turn on and output when a button is pressed
i can not get it to work and the led to come on. (it works when i flash a blink demo) i have gone over my code multiple times but can not see where i am going wrong . please could you point me in the right direction


int highfloat = D3; // high float will be connected to pin d2 on the unit
int highlevellight = D7;

void setup() 
{
pinMode(highfloat, INPUT_PULLUP); // declare that pin an INPUT
pinMode(highlevellight, OUTPUT);// declare the highlevellight pin as an output
//highfloat = LOW; // set the input as low to make sure  the program is not started with it on
//highlevellight = HIGH;
}

void loop() {

   
    
    if (digitalRead(highfloat) == HIGH)
    {
    Particle.publish("highlevelreached");
    digitalWrite(highlevellight, HIGH);
 
    delay(10000);
    }
    else
    {
    digitalWrite(highlevellight, LOW);
    }
    delay(10000);
    
}

Thanks

I think one of two things is backwards, depending on how your high float switch is connected to D3.

If the high float switch connects D3 to GND and is closed when the level is high:
Use instead: if (digitalRead(highfloat) == LOW)

If the float switch connects D3 to GND and is open when the level is high:
This should work.

If the float switch connects D3 to 3V3 and is closed when the level is high:
pinMode(highfloat, INPUT_PULLDOWN) is required instead.

If the float switch connects D3 to 3V3 and is open when the level is high:
pinMode(highfloat, INPUT_PULLDOWN) is required instead.
And: if (digitalRead(highfloat) == LOW)

Thanks very much i think it was a bit of wiring and code i did not realise i could use 3v3 or gnd for my signal.
its up and working now (until i do some more playing around)

Ben

1 Like