Back to Parent

Code for the Childs set
// This #include statement was automatically added by the Particle IDE.
//This code is for the child
#include <neopixel.h>
#define PIXEL_PIN D5
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812
 
Adafruit_NeoPixel stripchild = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE); //neopixel initialisation
//initialising photocell
int photopinchild = A1; 
int photoreadingchild =0;
int photopinmom = A2;
int photoreadingmom = 0;
int momLed = D7;
int momnormal =0;
int childnormal=0;
int momblock=0;
int childblock=0;


//initializing RGB
int redPin = D4;    
int greenPin = D3;  
int bluePin = D2;  
String wanttocookmom = "0";
String wanttocookchild = "0";
#define COMMON_ANODE

//initializing FSR
int fsrchild = A0;
int fsrreadingchild =0;
uint32_t n = stripchild.Color(0, 0, 0);
uint32_t w = stripchild.Color(255, 255, 255);

long lastPublishedAt = 0;
// this is the time delay before we should publish a new event
// from this device
int publishAfter = 5000;
int rainbow =0;
int rainbowchildPosition = 0;

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(momLed,OUTPUT);
  pinMode(photopinchild,INPUT);
  pinMode(photopinmom,INPUT);
  Particle.variable("fsrchild", &fsrreadingchild, INT);
  Particle.variable("photochild", &photoreadingchild, INT);
  Particle.variable("photomom", &photoreadingmom, INT);
  Particle.subscribe(  "iot/timetocookmom" , handleTimeToCook);//subscribing to function from mom
     momnormal = analogRead(photopinmom); //establishing baseline reading for photocell
     childnormal = analogRead(photopinchild);
     
//  Particle.subscribe(  "iot/stopmom" , handleStopChild);

}

void loop() {
    
    //stripchild.setBrightness(60);
    
     fsrreadingchild = analogRead(fsrchild);
     photoreadingchild = analogRead(photopinchild);
     photoreadingmom= analogRead(photopinmom);
    
    //setting limits for the photocell
  
     int mn = momnormal;
     int mh = momnormal + 300;
     int ml = momnormal - 1000;
     
       
     int cn = childnormal;
     int ch = childnormal + 300;
     int cl = childnormal - 1000;
     
     if(photoreadingmom>mh)
     {
         momblock=3;
     }
     else if(photoreadingmom<ml)
     {
         momblock =1;
     }
     else
     {
         momblock=2;
     }
     
     
       if(photoreadingchild>ch)
     {
         childblock=3;
     }
     else if(photoreadingchild<cl)
     {
         childblock =1;
     }
     else
     {
         childblock=2;
     }
 if(fsrchild<2500) //checking for equal measurements of spice if the spoons are lifted
 {
     if(childblock == momblock) //just right
     {
        analogWrite(redPin,255);
        analogWrite(bluePin,255);
        analogWrite(greenPin,0);
        digitalWrite(momLed,HIGH);
     }
     
     else if(childblock<momblock) 
     {
          analogWrite(redPin,255);
        analogWrite(bluePin,0);
         analogWrite(greenPin,255);
          digitalWrite(momLed,LOW);
     }
     
     else if(momblock<childblock)
     {
          analogWrite(redPin,0);
        analogWrite(bluePin,255);
         analogWrite(greenPin,255);
          digitalWrite(momLed,LOW);
     }
 }
     
     if(fsrreadingchild<2500) //checking if child wants to cook
     { //rainbowmom(30);
         wanttocookchild = "1";
         publishWantToChild();
        // analogWrite(redPin,255);
        //  analogWrite(bluePin,255);
        //  analogWrite(greenPin,0);
     }
     
     else if(fsrreadingchild>2500)
     {
         wanttocookchild = "0";
         publishStopChild();
        //  analogWrite(redPin,0);
        //  analogWrite(bluePin,255);
        //  analogWrite(greenPin,255);
     }
     
 
     if(rainbow==1)
     {
         rainbowchild(30);
         delay(10);
     }

}

void publishWantToChild() //publishing to mom if child wants to cook
{
     if( lastPublishedAt + publishAfter < millis() )
  {
      String eventName = "iot/timetocookchild" + System.deviceID();
      Particle.publish( eventName, "wanttocook" );
      lastPublishedAt = millis();
  }
}

void publishStopChild()
{
    if( lastPublishedAt + publishAfter < millis() )
  {
      String eventName = "iot/stopchild" + System.deviceID();
      Particle.publish( eventName, "wanttocook" );
      lastPublishedAt = millis();
  }
}

void handleTimeToCook(const char *event, const char *data) //if daughter is signalling that its time to cook
{ String eventName = String( event ); // convert to a string object
  // int value = int(data);

    String deviceID = System.deviceID();
        if(fsrreadingchild>2500)
        { rainbow=1;
          wanttocookchild="0";
        }
        else
        {rainbow=0;
            for(int i=0; i<stripchild.numPixels(); i++) {
      stripchild.setPixelColor(i,w);
        stripchild.show();
     }
     wanttocookchild="1";
        }
     if( eventName.indexOf( deviceID ) != -1 ){
      return;
    }
}

void rainbowchild(uint8_t wait) { //rainbow wheel cycles
  uint16_t i;
    for(i=0; i<stripchild.numPixels(); i++) {
      stripchild.setPixelColor(i, Wheel((i*1+rainbowchildPosition) & 255));
    }
    stripchild.show();
 

  rainbowchildPosition++;
  if( rainbowchildPosition > 255 ){
      rainbowchildPosition = 0;
  }
}

uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
    return stripchild.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } 
  else if(WheelPos < 170) {
    WheelPos -= 85;
    return stripchild.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } 
  else {
    WheelPos -= 170;
    return stripchild.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