Back to Parent

//Device 1
// 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"
#include <iostream>
#include <string>
#include <stdio.h>

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

//particle serial monitor

int windSensorPin = A0;
int windSpeed = 0; //range:1700-2400
int threshold = 2000;
unsigned long lastFade = 0;
int Interval = 3000;
unsigned long servolastFade = 0;
int servoInterval = 20;
int buttonPin = D1;

//color
int redValue = 255;
int greenValue = 255; 
int blueValue = 255;
int color = 255;

//breathe
int breathecount_double = 0;
int breathecount = 0;
bool buttonsignal = true;
bool kissreceive = false;
bool currentwindbool = false;
bool firstsignalonce = true;
int largerthanthresholdcount = 0;
int mapbreathcount = 0;
int mapdelaytime = 0;
int j=0;

// int breathlength_max = ;
int delaytime = 2;
int windreadinterval = 50;
int breathelengthlist[MAX_SIZE]; 
int list_size = 0;
int listmax = 12;
//servo
// int servoPin = A5;
// int servoPosition = 0;
// Servo servo;

Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
uint16_t i;
uint32_t c = strip.Color(0, 0, 0);
int k = 0;

void setup() {
    Serial.begin( 9600 );
    strip.begin();
    strip.show(); 
    Particle.variable("speed",&windSpeed, INT);
    pinMode( buttonPin , INPUT_PULLUP);
    // servo.attach(servoPin);
    Particle.subscribe("kissReceivesuccess", handlekisssuccess);
}

void handlekisssuccess(const char *event, const char *data) {
    kissreceive = true;
}

void loop() {
    windSpeed = analogRead(windSensorPin); 
    int buttonState = digitalRead( buttonPin );
    
    while( buttonState == LOW ){
        windSpeed = analogRead(windSensorPin); 
        delay(windreadinterval);
        
        if(firstsignalonce == true){
            Firststagesignal();
        }
        
        while(windSpeed>threshold){
            delayvariation();
            currentwindbool = true;
            firstsignalonce = false;
            buttonsignal = false;
        }
        mapbreathcount = map(largerthanthresholdcount,0,100,1,10);
        // Serial.println(largerthanthresholdcount);
        // Serial.println(mapbreathcount);
        
        if (currentwindbool==true) {
            if(windSpeed<threshold){
                // breathecount_double++;
                delaytime = (windreadinterval*mapbreathcount*0.1)/5*2;
                mapdelaytime = map(delaytime,0,30,1,12);
                Serial.println(mapdelaytime);
                append();
                showList();
                
                currentwindbool = false;
                delaytime = 2;
            }
            // Serial.println(breathecount_double);
        }
        buttonState = digitalRead( buttonPin );
        largerthanthresholdcount = 0;
        // Serial.println(list_size);
        //blink one more light when count+1
        breathtimeslightrecord();
    }
    // breathecount = breathecount_double*0.5;
    // Serial.println(delaytime);
    // Serial.println(breathecount); 
    
    if(buttonsignal == false){
        j=0;
        while(j < list_size){
            // unsigned long now = millis();
            Secondstagebreathe();
            // servomotion();
            // lastFade = now;
            j++;
        }
        // for(k=0;k<breathecount;k++){
        //     Secondstagebreathe();
        // }
        delay(500);
        for(i=0;i<3;i++){
            Sendsuccessfully();
            delay(50);
        }
        buttonsignal = true;
        firstsignalonce = true;
        //send
        Serial.println(list_size);
        
        char breathe[10];
        itoa(list_size, breathe, 10);
        Particle.publish("kissPublish",breathe);
        // Serial.println(breathecount);
            
        StaticJsonDocument<256> doc;
        JsonArray array = doc.createNestedArray("breathecountlist");
        for (i = 0; i < list_size; i++) {
            array.add(breathelengthlist[i]);
        }
        char jsonBuffer[256];
        serializeJson(doc, jsonBuffer);
        Particle.publish("delayPublish",jsonBuffer,PRIVATE);
        
        breathecount = 0;
    }
    breathecount = 0;
    delaytime = 5;
    clearList();
    
    if(kissreceive == true){
        i=0;
        while(i<3){
            kissreceivesuccess();
            i++;
        }
        kissreceive = false;
    }
}

void Firststagesignal(){
//     for(i=0; i< strip.numPixels(); i++) {
//         c = strip.Color(255, 255, 0);
//         strip.setPixelColor(i, c );
// 		strip.show();
		
//     }
//     for(i=0; i< strip.numPixels(); i++) {
//         c = strip.Color(0, 0, 0);
//         strip.setPixelColor(i, c );
// 		strip.show();
// // 		delay( 5 );
//     }
//     delay( 100 );
    for (int color = 0; color < 255; color++) {
        for (i = 0; i < strip.numPixels(); i++) {
            c = strip.Color(color, color, 0);
            strip.setPixelColor(i, c);
        }
        strip.show();
        delay(0);
    }
                
    for (int color = 255; color > 0; color--) {
        for (i = 0; i < strip.numPixels(); i++) {
            c = strip.Color(color, color, 0);
            strip.setPixelColor(i, c);
        }
        strip.show();
        delay(0);
    }
}

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

void breathtimeslightrecord(){
    for (i = 0; i < list_size; i++){
        c = strip.Color(225, 0, 255);
        strip.setPixelColor(i, c);
        strip.show();
    }
}

void Secondstagebreathe(){
    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(breathelengthlist[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(breathelengthlist[j]);
    }
}

// void delayvariation(){
//     windSpeed = analogRead(windSensorPin);
//     delay(windreadinterval);
//     if(windSpeed>threshold){
//         largerthanthresholdcount++;
//         // Serial.println(largerthanthresholdcount);
//     }
//     // Serial.println(largerthanthresholdcount);
//     // delaytime = windreadinterval*largerthanthresholdcount;
// }

void delayvariation(){
    windSpeed = analogRead(windSensorPin);
    delay(windreadinterval);
    largerthanthresholdcount++;
    // Serial.println(largerthanthresholdcount);
}

void append() {
    if (list_size < listmax) {
        breathelengthlist[list_size] = mapdelaytime;
        list_size++;
    } 
}

void showList(){
    if (list_size == 0) {
        Serial.println("nothing");
        return;
    }
    for (int i = 0; i < list_size; i++) {
        Serial.print(breathelengthlist[i]);
        Serial.print(" ");
    }
    Serial.println();
}

void clearList() {
    list_size = 0;
}

void Sendsuccessfully(){
    //blink green lights
    c = strip.Color(0, 0, 0);
    for(i=0; i< strip.numPixels(); i++) {
        c = strip.Color(0, greenValue, 0);
        strip.setPixelColor(i, c );
		strip.show();
		delay( 50 );
        }
    for(i=0; i< strip.numPixels(); i++) {
        c = strip.Color(0, 0, 0);
        strip.setPixelColor(i, c );
		strip.show();
		delay( 50 );
    }
}
Click to Expand

Content Rating

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

0