I power on the device, and it connects to the cloud right away. However, once I press the button I added to execute that part of the code, it works, but then it occasionally disconnects. I am not even sure how to debug this. If it was something wrong with my code, wouldn’t it be disconnecting each time?
I’ve reworked 5 dev boards and I am about to put them in plastic enclosures to do some beta testing, but I am afraid of the reliability. Please help.
PRODUCT_ID(*hidden*);
PRODUCT_VERSION(*hidden*);
int led1 = D6; // LED is connected to D6
int led2 = A0; // LED is connected to A0
int led3 = A5; // LED is connected to A5
int led4 = D0; // LED is connected to D0
int pushButton = D5; // Push button is connected to D5
// This routine runs only once upon reset
void setup()
{
pinMode(led1, OUTPUT); // Initialize D6 pin as output
pinMode(led2, OUTPUT); // Initialize A0 pin as output
pinMode(led3, OUTPUT); // Initialize A5 pin as output
pinMode(led4, OUTPUT); // Initialize D0 pin as output
pinMode(pushButton, INPUT_PULLUP);
// Initialize D2 pin as input with an internal pull-up resistor
}
// This routine loops forever
void loop()
{
int pushButtonState;
pushButtonState = digitalRead(pushButton);
if(pushButtonState == LOW){ //If we push down on the push button
digitalWrite(led1, HIGH); // Turn ON the LED
digitalWrite(led2, HIGH); // Turn ON the LED
digitalWrite(led3, HIGH); // Turn ON the LED
digitalWrite(led4, HIGH); // Turn ON the LED
Particle.publish("*hidden*", PRIVATE);
// Add a delay to prevent getting tons of emails from IFTTT
delay(10000);
}
else
{
digitalWrite(led1, LOW); // Turn OFF the LED
digitalWrite(led2, LOW); // Turn OFF the LED
digitalWrite(led3, LOW); // Turn OFF the LED
digitalWrite(led4, LOW); // Turn OFF the LED
}
}
I would not use such long delay() periodes.
If you want a simple solution you could use this alternative delay
inline void softDelay(uint32_t d) {
for(uint32_t ms = millis(); millis() - ms < d; Particle.process());
}
to see if this changes anything.
But I’d suggest you try to adapt non-blocking programming techniques to keep your code free running as much as possible.
Additinally how much current are your LEDs drawing when they are all on?
Do you have suitable current limiting resistors in place for your LEDs?
I actually plan on having the delay be for an hour. The idea is that they can only press the button once an hour and the LEDs stay on for that hour.
What is the disadvantage to using such a long delay period?