/*
* Project Kansha
* Description: A device to show gratitude over long distances.
* Author: Woo Kim, Anna Lawn, Olivia Roy, Corey Williams
* Date: 2/13/2018
*/
long lastPublishedAt = 0; // This value will store the last time we published an event
int publishAfter = 10000; // this is the time delay before we should publish a new event
//Assign global variables.
int redPin = D1;
int greenPin = D2;
int bluePin = D3;
int redValue = 255;
int greenValue = 255;
int blueValue = 255;
int capPin = D4;
int servoPin = A4;
int capReading = 0;
Servo servo;
bool shouldGesture = false;
void setup() {
Serial.begin(9600);
//Particle.function("servoControl", servoControl);
Particle.subscribe( "diot/2018/paired/kansha" , handleSharedEvent ); //Subscribe to event for Kansha project.
servo.attach(servoPin); //Attach servo motor.
servo.write(2); //Move servo to starting position.
//Assign output pins.
pinMode (redPin, OUTPUT);
pinMode (greenPin, OUTPUT);
pinMode (bluePin, OUTPUT);
//Assign input pins.
pinMode (capPin, INPUT);
setRGBColor(0,0,0); //Turn RGB LED on to start.
}
void loop() {
capReading = digitalRead(capPin); //Read if cap touch sensor is activated.
if (capReading == HIGH) { //Publish event when cap touch sensor is activated.
publishMyEvent();
}
if(shouldGesture == true ){
kanshaGesture(); //Do this if paired photon publishes event.
shouldGesture = false;
}
delay(100);
}
//Function for publishing event without spamming.
void publishMyEvent() {
if( lastPublishedAt + publishAfter < millis() ) {
String eventName = "diot/2018/paired/kansha" + System.deviceID();
Particle.publish( eventName );
lastPublishedAt = millis();
}
}
//Create a function to act when an event is published by paired photon, and not by this photon.
void handleSharedEvent(const char *event, const char *data)
{
String eventName = String( event ); // Convert to a string object.
String deviceID = System.deviceID();
if( eventName.indexOf( deviceID ) != -1 ){ //Don't do anything if the event was published by this device.
shouldGesture = true;
return;
}
kanshaGesture();
Serial.println("here");
}
//Set RGB color.
void setRGBColor( int r, int g, int b ){
redValue = r;
greenValue = g;
blueValue = b;
analogWrite(redPin, 255 - redValue);
analogWrite(greenPin, 255 - greenValue);
analogWrite(bluePin, 255 - blueValue);
}
//Create a function to turn on lights and move servo motor for Kansha gesture.
void kanshaGesture() {
setRGBColor(255,255,255);
delay(2000);
servo.write(52);
delay(5000);
servo.write(2);
delay(2000);
setRGBColor(0,0,0);
}
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. .