Publish - button prob, pull down resistor?

(modified photobeam example) I want to simulate 2 seperate 24v dc sensors sending a signal to a boron, each to its own pin; A4 and A3. I have a voltage divider whos ~2.5v output goes to 2 buttons and then through to A4 and A3. Once in a while it works but super unreliablely. Do I need a pull down resistor even with the INPUT_PULLUP? What do I have wrong here?

int boardLed = D7;
int cellstart = A4;
int cellstop = A3;
volatile int8_t toolsignal = 0;

void myHandler(const char *event, const char *data); // forward declaration
void CellStatusStop(void);
void CellStatusStart(void);

// We start with the setup function.
void setup()
{
    // This part is catching the signals on the input pins:
    pinMode(cellstart, INPUT_PULLUP);      // the toolstart pin
    attachInterrupt(cellstart,CellStatusStart, RISING); // interrupt a signal from tool
    // second tool
    pinMode(cellstop, INPUT_PULLUP);       // the toolstop pin
    attachInterrupt(cellstop,CellStatusStop, RISING); // interrupt a signal from tool
    pinMode(boardLed, OUTPUT); // Our on-board LED is output as well
    // Here we are going to subscribe to "beamStatus" events
    Particle.subscribe("CellStatus", myHandler);
    // Subscribe will listen for the event beamStatus and, when it finds it, will run the function myHandler()
    // Now flash the D7 LED on and off three times to let us know that we're ready to go!
    digitalWrite(boardLed, HIGH);
    delay(100);
    digitalWrite(boardLed, LOW);
    delay(100);
    digitalWrite(boardLed, HIGH);
    delay(100);
    digitalWrite(boardLed, LOW);
    delay(100);
    digitalWrite(boardLed, HIGH);
    delay(100);
    digitalWrite(boardLed, LOW);
}

void loop()
{
    if(toolsignal>0)
    switch(toolsignal){
        case 1:
            Particle.publish("CellStatus", "start");
            // publish this event
            toolsignal=0;
            break;
        case 2:
            Particle.publish("CellStatus", "stop");
            // publish this event
            toolsignal=0;
            break;
    }
}

void CellStatusStart()
{
    toolsignal=1;
}

void CellStatusStop()
{
    toolsignal=2;
}

// Now for the myHandler function, which is called when the cloud tells us that an event is published.
void myHandler(const char *event, const char *data)
{
    if (strcmp(data, "stop") == 0)
    {
        // if the beam is intact, then turn your board LED off
        digitalWrite(boardLed, LOW);
    }
    else if (strcmp(data, "start") == 0)
    {
        // if the beam is broken, turn your board LED on
        digitalWrite(boardLed, HIGH);
    }
    else
    {
        // if the data is something else.
        digitalWrite(boardLed, HIGH);
        delay(100);
        digitalWrite(boardLed, LOW);
        delay(100);
        digitalWrite(boardLed, HIGH);
        delay(100);
        digitalWrite(boardLed, LOW);
        delay(100);
        digitalWrite(boardLed, HIGH);
        delay(100);
        digitalWrite(boardLed, LOW);
    }
}

I can't quite understand your setup especially this

With INPUT_PULLUP you'd have your button open (the pull-up keeping the signal on the pin at 3.3V) when not pressed and connect to GND (0V) when pressed.
How do your 2.5V fit into that picture?

If you intend to detect the transition from 3.3V (non-active) to 2.5V (active) then I'm not surprised that this gives you unreliable readings since you intend to detect the transition from HIGH to "not quite HIGH".

On the other hand if your button should apply 2.5V when pressed, then you'd not use INPUT_PULLUP but rather INPUT_PULLDOWN to keep the non-active level at 0V.
However, ~2.5V isn't a perfect HIGH signal either. The minimum for a HIGH signal is given as 0.7 * 3.3V = 2.31V so your ~2.5V may not always provide a clean switching level.

Thank you ScruffR, I think this must be the problem as I didnt know thats how power to the pin works. I have a 9v battery set up as im waiting on a bench power supply to come to supply the 24v. I ran that to a voltage divider i cobbled up out of the few resistors i have on hand and Im drawing the power from it to 2 buttons (just to simulate the 2 sensor wires) and through to the 2 pins. The voltage is lower ~2.1-2.2v so I will see if I can find the right combination of resistors to get it to 3.3 and change to the INPUT_PULLDOWN.

Two other noob questions: once thats sorted, can I run the Publish commands straight out from the ISRs? I was getting SOS and 14 (heap) errors when doing that so I reverted to putting the publish calls into the loop.
Also, Im wondering in general if thats how one should handle an external 24v dc signal froma a sensor? I mean, is a voltage divider the best, safest and reliable way to do it?
thanks kindly!!

That's a no-no. You should only set a flag in the ISR and then deal with that in loop().

It's the simplest way, but a safer way would be an opto isolator.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.