Back to Parent

Code for the Mothers set
//this is code for the Mom.
#include <neopixel.h>
#define PIXEL_PIN D5
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812
 
Adafruit_NeoPixel stripmom = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE); //neopixel initialisation
//initialising photocell
int photopinmom = A1;
int photoreadingmom =0;

//initializing RGB

int ledPin = D3;  
String wanttocookmom = "0";
String wanttocookchild = "0";


//initializing FSR
int fsrmom = A0;
int fsrreadingmom =0;
uint32_t n = stripmom.Color(0, 0, 0);
uint32_t w = stripmom.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 rainbowmomPosition = 0;

void setup() {

  pinMode(ledPin, OUTPUT);
  Particle.variable("fsrmom", &fsrreadingmom, INT);
  Particle.variable("photomom", &photoreadingmom, INT);
  Particle.subscribe(  "iot/timetocookchild" , handleTimeToCook);
  //Particle.subscribe(  "iot/stopchild" , handleStopChild);

}

void loop() {
  //  stripmom.setBrightness(60);
     fsrreadingmom = analogRead(fsrmom); //checking whether moms spoon is on the stand or not
     photoreadingmom = analogRead(photopinmom);
     
     if(fsrreadingmom<2000) //mom is ready to cook
     { //rainbowmom(30);
         wanttocookchild = "1";
         publishWantToMom();
     digitalWrite(ledPin,HIGH);
     }
     
     else if(fsrreadingmom>2000) //mom is not ready to cook
     {
         wanttocookchild = "0";
         publishStopMom();
         digitalWrite(ledPin,LOW);
       
     }
     
 
     if(rainbow==1)
     {
         rainbowmom(30);
         delay(10);
     }

}

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

void publishStopMom() //publishing to child if mom doesnt want to cook
{
    if( lastPublishedAt + publishAfter < millis() )
  {
      String eventName = "iot/stopmom" + 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(fsrreadingmom>1000)
        { rainbow=1;
          wanttocookmom="0";
        }
        else
        {rainbow=0;
            for(int i=0; i<stripmom.numPixels(); i++) {
      stripmom.setPixelColor(i,w);
        stripmom.show();
     }
     wanttocookmom="1";
        }
     if( eventName.indexOf( deviceID ) != -1 ){
      return;
    }
}

void rainbowmom(uint8_t wait) { //rainbow wheel cycle
  uint16_t i;
    for(i=0; i<stripmom.numPixels(); i++) {
      stripmom.setPixelColor(i, Wheel((i*1+rainbowmomPosition) & 255));
    }
    stripmom.show();
 

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

uint32_t Wheel(byte WheelPos) { //picking colours for the rainbow wheel
  if(WheelPos < 85) {
    return stripmom.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } 
  else if(WheelPos < 170) {
    WheelPos -= 85;
    return stripmom.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } 
  else {
    WheelPos -= 170;
    return stripmom.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