Back to Parent

#include "neopixel.h"
#define PIXEL_COUNT 24  //use a nexpixel with 24 leds
#define PIXEL_PIN D2
#define PIXEL_TYPE WS2812

Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

int i;
int yellowPin = D5;  // use a single-color led
int flexPin = A1;  //use a flex sensor
int state = 0;
int num = 0;
int flex;
int a =1;

void setup(){
  pinMode(yellowPin, OUTPUT);
  pinMode(flexPin, INPUT);
  strip.begin();
  strip.show();
  Particle.function("sendColor", sendColor); //This function is used for testing.
  Particle.subscribe("pairA/has/newMessage", handler);
  Serial.begin(9600);
}


void loop(){
  int flex = analogRead(flexPin);
  //Serial.println(flex);

  //The variable state functions as a trigger to make sure the event will
  //be only be published once when the flex sensor is bent.
  if( flex <= 70 and state == 0){ // When the flex sensor is bent to one direction.
    color = "love";
    Particle.publish("pairA/has/newMessage", color);
    //This event contains the data "love" or rage. Depending on the data, the other person will recive a message from IFTTT.
    Serial.println("love");
    state = 1;
    delay(500);
  }

  if( flex >= 180 and state == 0){ // When the flex sensor is bent to another direction.
    color = "rage";
    Particle.publish("pairA/has/newMessage", color);
    Serial.println("rage");
    state = 1;
    delay(500);
  }

  if( flex > 50 and flex < 180 and state == 1){ // When the flex sensor is bent to another direction.
    state = 0;
    delay(500);
  }

  // int flex = analogRead(flexPin);
  // Serial.println(flex);
  // delay(1000);

}


//When receive a event, comparing the data with "love" or "rage".
void handler( const char * event, const char *data ){
  color = String(data);
  if(color == "love"){ //If data is "love", trigger the nexpixel breathing effect.
    digitalWrite(yellowPin, LOW);
    for(int n=0;n<=200;n+=8){
      for(int i=0;i<PIXEL_COUNT;i++){
        strip.setColorDimmed(i,20*i,10*i,100-i,n);
      }
      strip.show();
      delay(20);
    }

    for(int n=202;n>=2;n-=8){
      for(int i=0;i<PIXEL_COUNT;i++){
        strip.setColorDimmed(i,20*i,10*i,100-i,n);
      }
      strip.show();
      delay(20);
    }
  }

  else if(color == "rage"){  //If data is "rage", turn the led light on.
    for(int i=0;i<PIXEL_COUNT;i++){
      strip.setColorDimmed(i,0,0,0,0);
    }
    strip.show();
    digitalWrite(yellowPin, HIGH);
  }
}
Click to Expand

Content Rating

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

0