// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
#include <ArduinoJson.h>
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
// IMPORTANT: Set pixel COUNT, PIN and TYPE
#if (PLATFORM_ID == 32)
#define PIXEL_PIN SPI1
#else //
#define PIXEL_PIN D2
#endif
#define PIXEL_COUNT 3
#define PIXEL_TYPE WS2812B
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
SYSTEM_MODE(AUTOMATIC);
SerialLogHandler logHandler(LOG_LEVEL_INFO);
Servo myServo;
JsonDocument doc;
String jsonText = "";
int button = 6;
bool requestSent = false;
unsigned long lastRequestTime = 0;
const unsigned long debounceDelay = 2000;
void setup() {
Serial.begin(9600);
pinMode(button, INPUT_PULLUP);
strip.begin();
strip.show();
myServo.attach(1); // Attach servo to pin 4
Particle.subscribe("hook-response/ssdsds", handleEventResponse, MY_DEVICES);
}
void loop() {
int btnState = digitalRead(button);
Serial.println("Button: " + String(btnState));
if (btnState == LOW && !requestSent) {
Particle.publish("ssdsds", PRIVATE); // Consider changing event name
Serial.println("Just published!");
requestSent = true;
lastRequestTime = millis();
}
if (requestSent && (millis() - lastRequestTime > debounceDelay)) {
requestSent = false;
}
delay(500); // Loops every 500ms
Serial.println("Loop ends!");
}
void handleEventResponse(const char *event, const char *data) {
long debt = stod(data);
Serial.println(debt);
if (debt >366666.53) {
myServo.write(150);
colorAll(strip.Color(250,200, 0), 900);
delay(700);
strip.clear();
strip.show();
delay(700);
colorAll(strip.Color(250,200, 0), 900);
delay(700);
strip.clear();
strip.show();
delay(700);
colorAll(strip.Color(250,200, 0), 900);
strip.clear();
strip.show();
delay(700);
colorAll(strip.Color(250,200, 0), 900);
strip.clear();
strip.show();
myServo.write(90);// Turn off the LEDs after 5 seconds
}
}
void colorAll(uint32_t c, uint16_t wait) {
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
}
strip.show();
delay(wait); // Delay for the specified wait time
}
Comments