Back to Parent

//Device2
// This #include statement was automatically added by the Particle IDE.
#include <ArduinoJson.h>

// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>

#include "Particle.h"

#define PIXEL_PIN SPI
#define PIXEL_COUNT 12
#define PIXEL_TYPE WS2812
#define MAX_LIST_SIZE 100

//particle serial monitor

Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
uint16_t i;
uint32_t c = strip.Color(0, 0, 0);
int buttonPin = D2;
unsigned long lastFade = 0;
int Interval = 3000;

//recieve
bool receive_or_not = false;
bool signall = true;
bool runonce = false;
int breathecountreceive = 0;
int delaytimereceive = 5;
int buttonsignal = true;
int buttonState = digitalRead( buttonPin );
bool listreceive = false;

//servo
int servoPin = A5;
int servoPosition = 0;
Servo servo;

int breathreceiveList[MAX_LIST_SIZE];
int listSize = 0; 
int listmax = 12;
int j=0;

void setup(){
    strip.begin();
    strip.show();
    Particle.subscribe("kissReceive", handlekiss);
    Particle.subscribe("delayReceive",handledelay);
    Serial.begin( 9600 );
    servo.attach(servoPin);
    pinMode( buttonPin , INPUT_PULLUP);
}

void handlekiss(const char *event, const char *data) {
    Serial.println(data);
    breathecountreceive = atoi(data);
    receive_or_not = true;
    signall = true;
    runonce=true;
}

void handledelay(const char *event, const char *data) {
    
    // Serial.println("Received delayPublish event:");
    Serial.println(data);  // Log the raw JSON string

    StaticJsonDocument<256> doc;
    DeserializationError error = deserializeJson(doc, data);

    if (error) {
        // Serial.print("JSON deserialization failed: ");
        // Serial.println(error.c_str());
        return;
    }

    // Extract the array from the JSON object
    JsonArray array = doc["breathecountlist"];
    if (!array.isNull()) {
        // Serial.println("Parsed values:");
        for (JsonVariant v : array) {
            int value = v.as<int>();  // Convert each element to an integer
            if (listSize < MAX_LIST_SIZE) {
                breathreceiveList[listSize] = v.as<int>();
                listSize++;
                // Serial.println(listSize);
            }
            Serial.println(value);   // Log the value
        }
    } else {
        Serial.println("breathecountlist is null!");
    }
    
}

void loop(){
    // Serial.println(breathecountreceive);
    // Serial.println(delaytimereceive);
    // Serial.println(receive_or_not);
    // Serial.println(buttonState);
    // Serial.println(runonce);
    // Serial.println(listreceive);
    
    
    buttonState = digitalRead( buttonPin );
    // int breathecountreceive1 = atoi(data);
    // if(breathecountreceive1!=breathecountreceive){
    //     receive_or_not = true;
    // }
    
    if(receive_or_not == true){
        if (signall == true){
            Receivepinksignal();
        }
        
        while(buttonState==LOW){
            signall = false;
            Particle.publish("kissSuccess");
            if(runonce==true){
                j=0;
                while(j < listSize){
                    // Serial.println(breathecountreceive);
                    // unsigned long now = millis();
                    Secondstagebreathe1();
                    // servomovement();
                    // lastFade = now;
                    j++;
                }
                runonce=false;
            }
            runonce=false;
            receive_or_not = false;
            buttonState = digitalRead( buttonPin);
            clearList();
            
            // endbreathe();
        }
        // Serial.println(listSize);
    }
    // clearList();
    // breathecountreceive = 0;
    // receive_or_not = false;
}

void Receivepinksignal(){
    for(i=0; i< listSize; i++) {
        c = strip.Color(225, 0, 255);
        strip.setPixelColor(i, c );
		strip.show();
		delay( 150 );
    }
    for(i=0; i< listSize; i++) {
        c = strip.Color(0, 0, 0);
        strip.setPixelColor(i, c );
		strip.show();
		delay( 150 );
    }
    
//     for(i=0; i< breathecountreceive; i++) {
//         c = strip.Color(225, 0, 255);
//         strip.setPixelColor(i, c );
// 		strip.show();
// 		delay( 20 );
//     }
//     for(i=0; i< breathecountreceive; i++) {
//         c = strip.Color(0, 0, 0);
//         strip.setPixelColor(i, c );
// 		strip.show();
// 		delay( 20 );
//     }
}

void Secondstagebreathe1(){

    for (int color = 0; color < 255; color++) {
        for (i = 0; i < strip.numPixels(); i++) {
            c = strip.Color(color, color, color);
            strip.setPixelColor(i, c);
        }
        strip.show();
        delay(breathreceiveList[j]);
    }
                
    for (int color = 255; color > 0; color--) {
        for (i = 0; i < strip.numPixels(); i++) {
            c = strip.Color(color, color, color);
            strip.setPixelColor(i, c);
        }
        strip.show();
        delay(breathreceiveList[j]);
    }
}

// void endbreathe(){
//     for(i=0; i< strip.numPixels(); i++) {
//         c = strip.Color(225, 0, 255);
//         strip.setPixelColor(i, c );
// 		strip.show();
// 		delay( 150 );
//     }
//     for(i=0; i< strip.numPixels(); i++) {
//         c = strip.Color(0, 0, 0);
//         strip.setPixelColor(i, c );
// 		strip.show();
// 		delay( 150 );
//     }
// }

void clearList() {
    listSize = 0;
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0