#include <neopixel.h> //includes the header file
#include <neopixel.h>
#define PIXEL_PIN A5 //define the input pin for neopixel
#define PIXEL_COUNT 1 //defines the number of pixels
#define PIXEL_TYPE WS2812 //defines the type of pixel used
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE); //creates a neopixel object called strip
//input pin for the flex sensor and piezo is defined
//integer flex is defined to store values from the flex sensor
int flexPin = A0;
int flex = 0;
int ledBrightness = 0;
int bright = 0;
int speakerPin = D4;
int melody[] = {1908,2551,2551,2273,2551,0,2024,1908};// defining an array for sound values of the piezo
int noteDurations[] = {4,8,8,4,4,4,4,4 }; //note durations e.g. 4= quarter note, 8=eighth note etc.
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()
{
pinMode(flexPin, INPUT);
pinMode(speakerPin, OUTPUT);
strip.begin(); //prepares data pin for output
strip.show(); // initialzes all neopixel to 'OFF'
delay( 100 );
Particle.variable("force", &flex, INT);
Particle.subscribe("plantr/team10/2019/bright/",handleSharedEventBright); //event is created to brighten the neopixels
//handleSharedEventBright is a function to read ths event when it will be published
Particle.subscribe("plantr/team10/2019/dark/",handleSharedEventDark); //event is created to darken the neopixels
}
void loop()
{
flex = analogRead(flexPin); //reads values from the flex sensor and stores in the integer flex
//if the flex sensor is pressed, brighten function is called to make the neopixels bright
if (flex > 500 && bright != 1) {
brighten();
} else if (flex < 300 && bright !=-1) { //when the flex sensor is released, darken function is called to dim the neopixels
darken();
}
}
void darken()
{
uint16_t i, j;
//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 (j = 127; j > 25; j--)
{
for (i = 0; i < strip.numPixels(); i++)
{
strip.setPixelColor(i, 0, 0, j); //sets the colour of each led
// argument i is the number of pixel, 0 is for red colour, second 0 is for green, j is blue colour value
//with every iteration the leds are dimmed
}
strip.show(); //updates the whole neopixel
delay(10);
}
delay(1500);
bright = -1; // after the neopixel is dimmed, an event is published
publishMyEventDark();
}
void brighten()
{ //same as above, only the leds will brighten
uint16_t i, j;
for (j = 127; j < 255; j++)
{
for (i = 0; i < strip.numPixels(); i++)
{
strip.setPixelColor(i, 0, 0, j);
}
strip.show();
}
delay(1500);
bright=1;
publishMyEventBright();//after neopixel is bright, an event is published
}
void publishMyEventDark()
{ //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/dark/"+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 publishMyEventBright()
{ //same as above, only the event here is for the bright function
if(lastPublished+publishAfter<millis())
{
String eventname="plantr/team10/2019/bright/"+System.deviceID();
Particle.publish(eventname,"data goes here");
lastPublished=millis();
}
}
void handleSharedEventBright(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
brighten(); //calls brighten function to make the neopixels bright
playNotes(); //calls this function to play the piezo
}
}
void handleSharedEventDark(const char *event, const char *data)
{ //this is same as above for the dark function
String eventname=String(event);
String deviceID=System.deviceID();
if(eventname.indexOf(deviceID)!=-1)
{
return;
}
else
{
darken();
playNotes();
}
}
void playNotes()
{
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(speakerPin, melody[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note’s duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(speakerPin);
}
}
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. .