Back to Parent

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


int tapSensorPin = A0;
int PressureValue;


int solPin = D3;


Servo myServo;
//Servo myServo2;
int servoPos = 0;


String eventNumber;

long lastPublishedAt = 0;
int publishAfter = 10000;

#define PIXEL_PIN D2
#define PIXEL_COUNT 8
#define PIXEL_TYPE WS2812B

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

uint32_t green = strip.Color(0, 255, 0);
uint32_t red = strip.Color(255, 0, 0);
uint32_t blue = strip.Color(0, 0, 255);
uint32_t off = strip.Color(0, 0, 0);
//uint16_t stablity = 0;

void setup() {
    Serial.begin(9600);
    
    myServo.attach( A3 );
    //myServo2.attach( A5 );
    
    myServo.write(0);
    //myServo2.write(0);
    
    pinMode(tapSensorPin, INPUT);
    pinMode(solPin, OUTPUT);
    
    strip.begin();
    strip.show(); 
    
     for(int i=0; i< strip.numPixels(); i++){
            strip.setPixelColor(i, green);
            strip.show();
            delay( 100 );
    } 
    
    Particle.subscribe(  "GRP5_SZL" , handleSharedEvent );

}


void loop() 
{ 
    
    PressureValue = analogRead(tapSensorPin); 
   
   
    
	
    // if(PressureValue <= 1000 && PressureValue >= 400) // pressure reading compare
    //     stablity++;
    // else
    //     stablity=0;
  
    if (PressureValue <= 1000 && PressureValue >= 400) {
        
        eventNumber="1";
        publishMyEvent();
        //Serial.print
        
        }
    
    if (PressureValue > 1000) {
        
        eventNumber="2";
        publishMyEvent();
        
    } 
}

void publishMyEvent()
{
  
  if( lastPublishedAt + publishAfter < millis() )
  {
      
      String eventName = "GRP5_SZL" + System.deviceID();

      Particle.publish( eventName, eventNumber );

      lastPublishedAt = millis();
  }

}


void handleSharedEvent(const char *event, const char *data)
{
    Particle.publish("result","it is working");
    String eventName = String( event ); 
    String getEventNum = String(data);
    String deviceID = System.deviceID();
    
  
    
    if( eventName.indexOf( deviceID ) != -1 ) {
        return;
    } else  {
                if (getEventNum=="1") {
                    
                    //servo move
                    //servoPos = map(PressureValue, 0, 1000, 0, 180);//the highest value of children's pressure is 2700
                    myServo.write(90);
                    //myServo2.write(90);
                    delay(100);
                    
                     //green light
                    for(int g=0; g< strip.numPixels(); g++){
                            strip.setPixelColor(g, green );
                            strip.show();
                            delay( 100 );
                    }
                    
                    
        
                } else if ( getEventNum=="2") {
                        
                        //red light
                        for(int r=0; r< strip.numPixels(); r++){
                            strip.setPixelColor(r, red );
                            strip.show();
                            delay( 100 );
                        }
       
                        //Solenoid will jump to indicate tickman's body is falling apart
                        for( int s = 0; s < 5; s++ ){
                            digitalWrite(solPin, HIGH);
                            delay( 100 ) ;
                            digitalWrite(solPin, LOW);
                            delay( 100 );
                        }
                    }
            }
        
    
}
Click to Expand

Content Rating

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

0