// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
#define PIXEL_PIN_1 A5 //define input pin number for the first neopixel
#define PIXEL_PIN_2 A3 //define input pin number for the second neopixel
#define PIXEL_COUNT 7//define pixel number
#define PIXEL_TYPE SK6812RGBW //define pixel type
Adafruit_NeoPixel StripOne = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN_1, PIXEL_TYPE); //create an object StripOne for the first neopixel
Adafruit_NeoPixel StripTwo = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN_2, PIXEL_TYPE); //create an object StripTwo for the second neopixel
uint32_t green1colour; //initialze colour green for StripOne
uint32_t green2colour; //initialize colour green for StripTwo
int touchPin0=D2; //initilaze the touchpins for capacitor
int touchPin1=D3;
int touchPin2=D4;
int touchPin3=D5;
long lastPublished=0; //keeps tracks of time of the last published event
int publishAfter=10000; //duration after which another event should be published
void setup()
{
StripOne.begin();//prepares datapin for output
StripOne.show();//initializes all neopixel to 'OFF'
delay( 100 );
StripTwo.begin();
StripTwo.show();
delay(100);
green1colour= StripOne.Color(255,0,0,0); //define colour green for the first neopixel
green2colour= StripTwo.Color(255,0,0,0); //define colour green for the second neopixel
FirstLeafInitialSetup(); //call function for the initial setup of the first leaf
SecondLeafInitialSetup(); //call function for the initial setup of the second leaf
pinMode( touchPin0, INPUT_PULLUP); //set touchpins as input pins in pullup mode
pinMode( touchPin1, INPUT_PULLUP);
pinMode( touchPin2, INPUT_PULLUP);
pinMode( touchPin3, INPUT_PULLUP);
Particle.subscribe("plantr/team10/2019/BrightLeaf1/",handleSharedEventBrightLeaf1); //event is created to brighten the first leaf
//handleSharedEventBrightLeaf1 is a function to read ths event when it will be published
Particle.subscribe("plantr/team10/2019/DarkLeaf1/",handleSharedEventDarkLeaf1); //event is created to darken the first leaf
Particle.subscribe("plantr/team10/2019/BrightLeaf2/",handleSharedEventBrightLeaf2); //event is created to brighten the second leaf
Particle.subscribe("plantr/team10/2019/DarkLeaf2/",handleSharedEventDarkLeaf2); //event is created to darken the second leaf
}
void loop()
{
if( digitalRead( touchPin0 ) == LOW ){ //when the first capacitor on the first leaf detects a touch call the bright function
FirstLeafBright();
publishMyEventBrightLeaf1();
delay( 1000 ) ;
}
else if(digitalRead(touchPin1) == LOW){ //when the second capacitor on the first leaf detects a touch call the dark function
FirstLeafDark();
publishMyEventDarkLeaf1();
delay(1000);
}
else if(digitalRead(touchPin2) == LOW){ //when the first capacitor on the second leaf detects a touch call the bright function
SecondLeafBright();
publishMyEventBrightLeaf2();
delay(1000);
}
else if(digitalRead(touchPin3) == LOW){ //when the second capacitor on the second leaf detects a touch call the dark function
SecondLeafDark();
delay(1000);
}
}
void FirstLeafInitialSetup()
{
StripOne.setBrightness(128); //set initial brightness for the first neopixel
for (int k=0; k< StripOne.numPixels(); k++) //set all the pixels to green colour
{
StripOne.setPixelColor(k,green1colour);
delay(100);
}
StripOne.show();
}
void SecondLeafInitialSetup()
{
StripTwo.setBrightness(128); //set initial brightness for the second neopixel
for (int k=0; k< StripTwo.numPixels(); k++) //set all the pixels to green colour
{
StripTwo.setPixelColor(k,green2colour);
delay(100);
}
StripTwo.show();
}
void FirstLeafBright()
{ //create an integer j that keeps track of the brightness of the leds
//create an integer i that keeps track of the number of leds in neopixel
for (int j=128; j<256; j++)
{
for (int i=0; i< StripOne.numPixels() ; i++)
{
StripOne.setBrightness(j); //increases the brightness of leds with every iteration
StripOne.setPixelColor(i, green1colour); //sets the colour of the leds to green
delay(100);
}
StripOne.show(); //updates the whole neopixel
delay(100);
}
}
void SecondLeafBright()
{ //same as above, for the second leaf
for (int j=128; j<256; j++)
{
for (int i=0; i< StripTwo.numPixels() ; i++)
{
StripTwo.setBrightness(j); //increases the brightness of leds with every iteration
StripTwo.setPixelColor(i, green2colour); //sets the colour of the leds to green
delay(100);
}
StripTwo.show(); //updates the whole neopixel
delay(100);
}
}
void FirstLeafDark()
{ //same as above, only the leds will dim
for (int j=128; j>0; j--)
{
for (int i=0; i< StripOne.numPixels() ; i++)
{
StripOne.setBrightness(j);
StripOne.setPixelColor(i, green1colour);
delay(100);
}
StripOne.show();
delay(100);
}
}
void SecondLeafDark()
{
//same as above, only the leds will dim
for (int j=128; j>0; j--)
{
for (int i=0; i< StripTwo.numPixels() ; i++)
{
StripTwo.setBrightness(j);
StripTwo.setPixelColor(i, green2colour);
delay(100);
}
StripTwo.show();
delay(100);
}
}
void publishMyEventBrightLeaf1()
{ //millis function keeps track of the real time clock
if(lastPublished+publishAfter<millis()) //have 10s passed since the last event was published
{
String eventname="plantr/team10/2019/BrightLeaf1/"+System.deviceID(); //add your argon device id to the event
Particle.publish(eventname,"data goes here"); //publish the above event and send an attached message with it
lastPublished=millis(); //last published is changed to now
}
}
void handleSharedEventBrightLeaf1(const char *event, const char *data) //function to read the published bright event
{
String eventname=String(event); //converts the character event to a string
String deviceID=System.deviceID(); //creates a string to store the device id
if(eventname.indexOf(deviceID)!=-1) //checks if the published event is from another device
//by looking for it's own device id in the event name
{
return;
}
else
{ //since the event is from another device
FirstLeafBright(); //calls the function to make the first leaf bright
}
}
void publishMyEventDarkLeaf1()
{ //same as above, only this is for the dark function of the first leaf
if(lastPublished+publishAfter<millis())
{
String eventname="plantr/team10/2019/DarkLeaf1/"+System.deviceID();
Particle.publish(eventname,"data goes here");
lastPublished=millis();
}
}
void handleSharedEventDarkLeaf1(const char *event, const char *data) //function to read the published dark event
{
String eventname=String(event); //converts the character event to a string
String deviceID=System.deviceID(); //creates a string to store the device id
if(eventname.indexOf(deviceID)!=-1) //checks if the published event is from another device
//by looking for it's own device id in the event name
{
return;
}
else
{ //since the event is from another device
FirstLeafDark(); //calls the function to make the first leaf dark
}
}
void publishMyEventBrightLeaf2()
{ //millis function keeps track of the real time clock
if(lastPublished+publishAfter<millis()) //have 10s passed since the last event was published
{
String eventname="plantr/team10/2019/BrightLeaf2/"+System.deviceID(); //add your argon device id to the event
Particle.publish(eventname,"data goes here"); //publish the above event and send an attached message with it
lastPublished=millis(); //last published is changed to now
}
}
void handleSharedEventBrightLeaf2(const char *event, const char *data) //function to read the published bright event
{
String eventname=String(event); //converts the character event to a string
String deviceID=System.deviceID(); //creates a string to store the device id
if(eventname.indexOf(deviceID)!=-1) //checks if the published event is from another device
//by looking for it's own device id in the event name
{
return;
}
else
{ //since the event is from another device
SecondLeafBright(); //calls the function to make the first leaf bright
}
}
void publishMyEventDarkLeaf2()
{ //same as above, only this is for the dark function of the second leaf
if(lastPublished+publishAfter<millis())
{
String eventname="plantr/team10/2019/DarkLeaf2/"+System.deviceID();
Particle.publish(eventname,"data goes here");
lastPublished=millis();
}
}
void handleSharedEventDarkLeaf2(const char *event, const char *data) //function to read the published dark event
{
String eventname=String(event); //converts the character event to a string
String deviceID=System.deviceID(); //creates a string to store the device id
if(eventname.indexOf(deviceID)!=-1) //checks if the published event is from another device
//by looking for it's own device id in the event name
{
return;
}
else
{ //since the event is from another device
SecondLeafDark(); //calls the function to make the second leaf dark
}
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .