int potPin = A1;
int ledPin1 = D0;
int ledPin2 = D1;
int ledPin3 = D2;
int potPosition;
int currentPosition = analogRead(A0);
int newPosition = analogRead(A0);
void setup() {
pinMode( ledPin1 , OUTPUT ); // sets pin as output
pinMode( ledPin2 , OUTPUT ); // sets pin as output
pinMode( ledPin3 , OUTPUT ); // sets pin as output
pinMode( potPin, INPUT ); //sets pin as input
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
Particle.subscribe( "jesse2016/paired/emoji-change" , handleEmojiChange );
//Particle.function( "lightEmoji" , lightEmojiFromCloud );
Serial.begin(9600);
}
void loop()
{ int newPosition = analogRead(A0);
Serial.print("NewPosition ");
Serial.println(newPosition);
delay( 1000 );
//announcePosition( newPosition );
if( newPosition != currentPosition )
{
// if they're not the same
// then announce it
announcePosition( newPosition );
// update the remote position variable
currentPosition = newPosition;
// wait 1 second before transmitting again
delay( 1000 );
}
}
void announcePosition( int newPosition )
{
// Combine the device ID and desired angle / servo position
// into a data string
String myID = System.deviceID();
String data = String(newPosition) + "," + myID;
// and share as a public event
Particle.publish( "jesse2016/paired/emoji-change", data );
}
// Cloud function to set the pot position
int lightEmojiFromCloud( String command )
{
int newPosition = command.toInt();
announcePosition( newPosition );
return 1;
}
// Event handler
// Triggered when a new event is recieved
// It will look like 106,ADF1347893274289374
// i.e. angle,device_id
void handleEmojiChange(const char *event, const char *data)
{
// Display what we got
Serial.println( "received=" );
Serial.println( data );
Serial.println( event );
// conver the data to a String type.
String dataStr = data;
// check for error stuff
// if there isn't data do nothing
if (!data) return;
// We only want to handle events
// From our paired device
// We're not interested in stuff that's come from
// this device ...
String myID = System.deviceID();
// if the device that sent is this device... ignore. return exits this function
if( dataStr.indexOf( myID ) > -1 ) return;
// If we get to here, it's an event we care about
Serial.println( "Not from another ... ");
// Extract the angle from the data string
// by chopping off the piece before the comma
int index = dataStr.indexOf(",");
String value = dataStr.substring( 0, index );
int newPosition = value.toInt();
// move the servo to the new position
lightEmoji( newPosition );
}
// Move the servo to where we want it to be...
void lightEmoji( int newPosition )
{
//happy
if (newPosition >= 0 && newPosition < 700 ){
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
}
//sleepy
if (newPosition >= 700 && newPosition < 2000 ){
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin3,LOW);
}
//love
if (newPosition >= 2000 && newPosition < 5000 ){
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,HIGH);
}
}
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. .