Code Works on Arduino, but not Photon?

So I’ve been using my Arduino Uno to sense when my door opens and have ti then send an SMS using a module. For that I wrote this code for which when the door is closed it keeps looping ‘0’ until the door opens, then it returns ‘1’ once and does the code to send an sms once, during this time when it return ‘1’ once, it doesn’t return anything, until the door is closed again for then it keeps repeating ‘0’ until any further action (Open Door = 1; Closed Door = 0).

Works great on the Arduino, but I wanted to switch it to the Photon for more WiFi options (email, web app, etc). So I took the Arduino’s code verbatim, except for the pin numbers and it’s behaving quite unnaturally. Instead of stopping when the loop returns ‘1’, it keeps looping back and returning ‘0’ after returning ‘1’ once, even though the door is open.

UPDATE: THANKS CODE FIXED

else if(digitalRead(buttonPin) == 0){            //Door Closed
    digitalWrite(ledPin, LOW);
    x = 1;
    Serial.println(digitalRead(buttonPin));            //Prints 0
    delay(100);    
  }

because you are not looking for the state change in the above else if statement.

but you are here:

if ((digitalRead(buttonPin) == 1) && (x == 1))

recommend that you use a non-blocking version like this (untested):

const int ledPin = D7;
const int buttonPin = D0;
const int debounceTime = 50;

void setup() 
{
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}

void loop() 
{
  int pressed = checkButton(buttonPin);
  switch(pressed)
  {
    case 1:
      Serial.println("Door Closed");
      digitalWrite(ledPin, LOW);
      // send message here
      break;
    case 2:
      Serial.println("Door Open");
      digitalWrite(ledPin, HIGH);
      // send message here
      break;
    default:
      // nothing to do here
      break;
  }
}

int checkButton(int pin)
{
  static unsigned long lastTime = 0; 
  static bool lastState = false;
  bool state = digitalRead(pin);
  if(state != lastState && millis() - lastTime > debounceTime)
  {
    return state? 2 : 1;
    lastTime = millis();
  }
  return 0;
}
3 Likes

@rsaiyid, @BulldogLowell’s advice is excellent, as usual. You can also consider using the super simple clickButton library in the web IDE which takes care of button debounce and allows you to specify single, double or tripple click as well as long clicks. :wink:

2 Likes

Thank you @BulldogLowell and @peekay123, I’ll test both methods to try and find a solution.

@BulldogLowell, I like your code because it seems to do my method in a more understandable way, but I only have a few questions because there are a few things in your code I don’t understand:

What is the debounceTime for?
In the checkButton function, what is the ‘unsigned’ before long?

This conditional is also kind of puzzling me, I still don’t understand the debounceTime’s use?

I don’t mean to sound rude, I’m just not very familiar with programming.
Thanks!

Debouncing is to suppress the several (dozens or even hundreds) of electrical contacts that will happen during the closing of certain types os switches. You program, with its delays is a crude debouncing tool: Just do nothing once a button press is detected for 100ms.

millis() returns an unsigned long data type. Unsigned integers may only be non-negative.

3 Likes