(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);
}
}