Back to Parent

#include "application.h"
#include "neopixel/neopixel.h"
//#include "Adafruit_GFX/Adafruit_GFX.h"
//#include "adafruit-led-backpack.h"

/*#ifndef _BV
  #define _BV(bit) (1<<(bit))
#endif*/


 SYSTEM_MODE(AUTOMATIC);

 // IMPORTANT: Set pixel COUNT, PIN and TYPE
 #define PIXEL_PIN D6
 #define PIXEL_COUNT 16
 #define PIXEL_TYPE WS2812B



//Adafruit_8x8matrix matrix = Adafruit_8x8matrix();
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

int bus(String command);
int buildin= D7;
int command_int;
int pushButton = D2;
int pirSensor = D4;
bool flag = false;
int i = 0;

 // Prototypes for local build, ok to leave in for Build IDE
 void rainbow(uint8_t wait);
 uint32_t Wheel(byte WheelPos);
/*
 static const uint8_t  smile_bmp[] =
   { 0B00111100,
     0B01000010,
     0B10100101,
     0B10000001,
     0B10100101,
     0B10011001,
     0B01000010,
     0B00111100 };
 static const uint8_t  neutral_bmp[] =
   { 0B00111100,
     0B01000010,
     0B10100101,
     0B10000001,
     0B10111101,
     0B10000001,
     0B01000010,
     0B00111100 };
 static const uint8_t  frown_bmp[] =
   { 0B00111100,
     0B01000010,
     0B10100101,
     0B10000001,
     0B10011001,
     0B10100101,
     0B01000010,
     0B00111100 };
 static const uint8_t  cloud_bmp[] =
   { 0B00000000,
     0B00000000,
     0B00110100,
     0B01111110,
     0B11111111,
     0B11111111,
     0B00000000,
     0B00000000 };

 uint8_t counter = 0;

 int issPassingOver(String command);
 int passing = 0;

 // This function is executed when the IFTTT receipe calls it
 int issPassingOver(String command)
 {
     if(command == "iss")
     {
      passing = 1;
      return 1;
     }
     else return -1;
 }
*/

 void setup()
 {
   Serial.begin(9600);
   Particle.function("brew", bus);
   Particle.function("brew2", bus2);
   pinMode(buildin, OUTPUT);
   pinMode(pushButton, INPUT_PULLUP);
   pinMode(pirSensor, INPUT);
   strip.begin();
   strip.show(); // Initialize all pixels to 'off'
   //matrix.begin(0x70);  // pass in the address
   //Spark.function("iss",issPassingOver);
 }



 void loop()
 {
   //matrix.clear();
   //matrix.setRotation(0);

   // Play the scrolling cloud animation
   /*for (int8_t x=20; x>=-20; x--) {
           matrix.clear();
           matrix.drawBitmap(x, 0, cloud_bmp, 8, 8, LED_ON);
           matrix.writeDisplay();
           delay(100);
       }*/

 }

 // this function automagically gets called upon a matching POST request
 int bus(String command)
 {
   int pushButtonState;

   pushButtonState = digitalRead(pushButton);

   int pirSensorState;

   pirSensorState = digitalRead(pirSensor);

   if(pushButtonState == LOW){ // If we push down on the push button
     flag = true;
   }
   if (flag == true){
     if(pirSensorState == LOW){
       i++;
       if (i>1000){
         flag = false;
         return -1;
       }
     }
     else{
       Serial.println("motion detected!");
     }
   }
   Serial.println(flag);


   if (flag == true){
     command_int = atoi(command);

     Serial.println(command_int);

     if (command_int < 2)  {//RED -- DON'T GO
     colorTwoWipe1(strip.Color(1, 0, 0), 50);
     strip.show();
       if (command_int > 0 ){
         return 1;
       }
       else{
         return -10;
       }
     }
     else if (command_int < 3){//Orange
       colorTwoWipe1(strip.Color(4, 1, 0), 50);
       strip.show();
       return 2;
       Particle.publish("test", "Get ready.");

     }
     else if (command_int < 4) {//yellow
       colorTwoWipe1(strip.Color(2, 1, 0), 50);
       strip.show();
       return 3;
     }
     else if (command_int < 5) {//Green
       colorTwoWipe1(strip.Color(0, 1, 0), 50);
       strip.show();
       Particle.publish("alert", "2");
       return 4;

     }
     if (command_int >= 5){//Blue
       colorTwoWipe1(strip.Color(0, 1, 1),50);
       strip.show();
       return 5;
       
     }
     else return -10;
   }
   else
   {
     return -20;
   }
   delay(500);
 }

 // this function automagically gets called upon a matching POST request
 int bus2(String command)
 {
   int pushButtonState;

   pushButtonState = digitalRead(pushButton);

   int pirSensorState;

   pirSensorState = digitalRead(pirSensor);

   if(pushButtonState == LOW){ // If we push down on the push button
     flag = true;
   }
   if (flag == true){
     if(pirSensorState == LOW){
       i++;
       if (i>1000){
         flag = false;
         return -1;
       }
     }
     else{
       Serial.println("motion detected!");
     }
   }
   Serial.println(flag);


   if (flag == true){
     command_int = atoi(command);

     Serial.println(command_int);

     if (command_int < 2)  {//RED -- DON'T GO
     colorTwoWipe2(strip.Color(1, 0, 0), 50);
     strip.show();
       if (command_int > 0 ){
         return 1;
       }
       else{
         return -10;
       }
     }
     else if (command_int < 3){//Orange
       colorTwoWipe2(strip.Color(4, 1, 0), 50);
       strip.show();
       return 2;
     }
     else if (command_int < 4) {//yellow
       colorTwoWipe2(strip.Color(2, 1, 0), 50);
       strip.show();
       return 3;
     }
     else if (command_int < 5) {//Green
       colorTwoWipe2(strip.Color(0, 1, 0), 50);
       strip.show();
       return 4;
     }
     if (command_int < 10){//Blue
       colorTwoWipe2(strip.Color(0, 1, 1),50);
       strip.show();
       return 5;
       Particle.publish("test", "this is a test message from particle");
     }
     if (command_int >= 10){//Blue
       colorTwoWipe2(strip.Color(0, 0, 1),50);
       strip.show();
       return 6;
       Particle.publish("test", "this is a test message from particle");
     }
     else return -10;
   }
   else
   {
     return -20;
   }
   delay(500);
 }



 // Fill the dots one after the other with a color, wait (ms) after each one
 void colorWipe(uint32_t c, uint8_t wait) {
   for(uint16_t i=0; i<strip.numPixels(); i++) {
     strip.setPixelColor(i, c);
     strip.show();
     delay(wait);
   }
 }

 void colorTwoWipe (uint32_t c, uint32_t d, uint8_t wait){
   for(uint16_t i=0; i<8; i++) {
     strip.setPixelColor(i, c);
     strip.show();
     delay(wait);
   }
   for(uint16_t i=8; i<strip.numPixels(); i++) {
     strip.setPixelColor(i, d);
     strip.show();
     delay(wait);
   }
 }


 void colorTwoWipe1 (uint32_t c, uint8_t wait){
   for(uint16_t i=0; i<11; i++) {
     strip.setPixelColor(i, c);
     strip.show();
     delay(wait);
   }
 }

 void colorTwoWipe2 (uint32_t d, uint8_t wait){
   for(uint16_t i=11; i<strip.numPixels(); i++) {
     strip.setPixelColor(i, d);
     strip.show();
     delay(wait);
   }

 }

 void rainbow(uint8_t wait) {
   uint16_t i, j;

   for(j=0; j<256; j++) {
     for(i=0; i<strip.numPixels(); i++) {
       strip.setPixelColor(i, Wheel((i+j) & 255));
     }
     strip.show();
     delay(wait);
   }
 }

 // Input a value 0 to 255 to get a color value.
 // The colours are a transition r - g - b - back to r.
 uint32_t Wheel(byte WheelPos) {
   if(WheelPos < 85) {
    return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
   } else if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
   } else {
    WheelPos -= 170;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
   }
 }
Click to Expand

Content Rating

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

0