Description
Here is a project that can be relatively easily made using two Spark Cores, a handful of wires, push-buttons, LEDs, and the Spark web IDE. The project relies on two Cores, "Core1" and "Core2". Each Core controls two LEDs. One's state ("SendLED") is dependent on a local push-button and is published to the Spark Cloud via Spark.publish(). The other LED's state ("ReceiveLED") mirrors the state of the "SendLED" on the other Core's board, pulling the published data using Spark.subscribe(). See the Fritzing diagram to reconstruct the hardware on a breadboard. The Core1 and Core2 code only varies slightly, but the system does need one copy of each flashed to one of the two Cores. It's amazing how fast the internet is at publishing and subscribing the state of the LEDs. Enjoy.
Core1 Code
Core1 Code
Warning: Embedding code files within the project story has been deprecated. To edit this file or add more files, go to the "Software" tab. To remove this file from the story, click on it to trigger the context menu, then click the trash can button (this won't delete it from the "Software" tab).
/*
This a program utilizing Spark.publish() and Spark.subscribe() to have pushbutton toggle LEDs communicate between two Spark Cores,
Core1 and Core2. When the local pushbutton is pressed on Core1, "sendLed" on Core1 will toggle state, while "receiveLed" on Core2 will follow
this state, and vice-versa. This is the code for Core1.
*/
int sendLedPin = D2; //choose the pin for the send (local) LED
int inputPin = D0; //choose the input pin (for a pushbutton)
int receiveLedPin = D1; //choose the pin for the receive (Core2-dependent LED)
int val = 0; //variable for reading the pushbutton pin status
int sendLedVal = 0; //variable for keeping the state of the local pushbutton dependent LED (D2)
void ledTwoToggle(const char *toggle, const char *onOff); //handler function for Spark.subscribe()
void setup() {
pinMode(receiveLedPin, OUTPUT); //Set the pin controlling the Core2-dependent LED as an OUTPUT
pinMode(sendLedPin, OUTPUT); //Set the pin controlling the local pushbutton-dependent LED as an OUTPUT
pinMode(inputPin, INPUT); //Set the pin connected to the pushbutton as an INPUT
digitalWrite(sendLedPin, LOW); //Ensure the local LED is set to off to begin with
Spark.publish("Core1Toggle", "State", 0, PRIVATE); //Set up Spark.publish() so that the state of the local LED is published to the Spark Cloud PRIVATELY
Spark.subscribe("Core2Toggle", ledTwoToggle, MY_DEVICES); //Set up Spark.subscribe() so that the state of Core1's Led is recorded and handled by ledTwoToggle
}
void loop() {
val = digitalRead(inputPin); //read value of pushbutton
if (val == LOW) { //if clause activated if pushbutton pressed and thus inputPin reads LOW
sendLedVal = !sendLedVal; //if clause activated if pushbutton pressed and thus inputPin reads LOW
digitalWrite(sendLedPin, sendLedVal ? HIGH : LOW); //write the appropriate HIGH/LOW to the sendLed pin to turn it ON/OFF
Spark.publish("Core1Toggle", sendLedVal ? "ON" : "OFF"); //publish the state to the Spark Cloud as ON/OFF
delay(250); //primitive button debouncing
}
}
void ledTwoToggle(const char *toggle, const char *onOff){ //handler function for Spark.subscribe()
if (strcmp(onOff, "ON") == 0){ //if sendLed on Core2 is ON according to Spark.publish()
digitalWrite(receiveLedPin, HIGH); //then turn on receiveLed
} else if (strcmp(onOff, "OFF") == 0){ //if sendLed on Core2 is OFF according to Spark.publish()
digitalWrite(receiveLedPin, LOW); //then turn off receiveLed
}
}
Core2 Code
Core2 Code
Warning: Embedding code files within the project story has been deprecated. To edit this file or add more files, go to the "Software" tab. To remove this file from the story, click on it to trigger the context menu, then click the trash can button (this won't delete it from the "Software" tab).
/*
This a program utilizing Spark.publish() and Spark.subscribe() to have pushbutton toggle LEDs communicate between two Spark Cores,
Core1 and Core2. When the local pushbutton is pressed on Core1, "sendLed" on Core1 will toggle state, while "receiveLed" on Core2 will follow
this state, and vice-versa. This is the code for Core2.
*/
int sendLedPin = D2; //choose the pin for the send (local) LED
int inputPin = D0; //choose the input pin (for a pushbutton)
int receiveLedPin = D1; //choose the pin for the receive (Core1-dependent LED)
int val = 0; //variable for reading the pushbutton pin status
int sendLedVal = 0; //variable for keeping the state of the local pushbutton dependent LED (D2)
void ledTwoToggle(const char *toggle, const char *onOff); //handler function for Spark.subscribe()
void setup() {
pinMode(receiveLedPin, OUTPUT); //Set the pin controlling the Core1-dependent LED as an OUTPUT
pinMode(sendLedPin, OUTPUT); //Set the pin controlling the local pushbutton-dependent LED as an OUTPUT
pinMode(inputPin, INPUT); //Set the pin connected to the pushbutton as an INPUT
digitalWrite(sendLedPin, LOW); //Ensure the local LED is set to off to begin with
Spark.publish("Core2Toggle", "State", 0, PRIVATE); //Set up Spark.publish() so that the state of the local LED is published to the Spark Cloud PRIVATELY
Spark.subscribe("Core1Toggle", ledTwoToggle, MY_DEVICES); //Set up Spark.subscribe() so that the state of Core1's Led is recorded and handled by ledTwoToggle
}
void loop() {
val = digitalRead(inputPin); //read value of pushbutton
if (val == LOW) { //if clause activated if pushbutton pressed and thus inputPin reads LOW
sendLedVal = !sendLedVal; //reverse state of sendLed variable value
digitalWrite(sendLedPin, sendLedVal ? HIGH : LOW); //write the appropriate HIGH/LOW to the sendLed pin to turn it ON/OFF
Spark.publish("Core2Toggle", sendLedVal ? "ON" : "OFF"); //publish the state to the Spark Cloud as ON/OFF
delay(250); //primitive button debouncing
}
}
void ledTwoToggle(const char *toggle, const char *onOff){ //handler function for Spark.subscribe()
if (strcmp(onOff, "ON") == 0){ //if sendLed on Core1 is ON according to Spark.publish()
digitalWrite(receiveLedPin, HIGH); //then turn on receiveLed
} else if (strcmp(onOff, "OFF") == 0){ //if sendLed on Core1 is OFF according to Spark.publish()
digitalWrite(receiveLedPin, LOW); //then turn off receiveLed
}
}
Git Repo
Video
Entanglement in action
Images
/*
This a program utilizing Spark.publish() and Spark.subscribe() to have pushbutton toggle LEDs communicate between two Spark Cores,
Core1 and Core2. When the local pushbutton is pressed on Core1, "sendLed" on Core1 will toggle state, while "receiveLed" on Core2 will follow
this state, and vice-versa. This is the code for Core1.
*/
int sendLedPin = D2; //choose the pin for the send (local) LED
int inputPin = D0; //choose the input pin (for a pushbutton)
int receiveLedPin = D1; //choose the pin for the receive (Core2-dependent LED)
int val = 0; //variable for reading the pushbutton pin status
int sendLedVal = 0; //variable for keeping the state of the local pushbutton dependent LED (D2)
void ledTwoToggle(const char *toggle, const char *onOff); //handler function for Spark.subscribe()
void setup() {
pinMode(receiveLedPin, OUTPUT); //Set the pin controlling the Core2-dependent LED as an OUTPUT
pinMode(sendLedPin, OUTPUT); //Set the pin controlling the local pushbutton-dependent LED as an OUTPUT
pinMode(inputPin, INPUT); //Set the pin connected to the pushbutton as an INPUT
digitalWrite(sendLedPin, LOW); //Ensure the local LED is set to off to begin with
Spark.publish("Core1Toggle", "State", 0, PRIVATE); //Set up Spark.publish() so that the state of the local LED is published to the Spark Cloud PRIVATELY
Spark.subscribe("Core2Toggle", ledTwoToggle, MY_DEVICES); //Set up Spark.subscribe() so that the state of Core1's Led is recorded and handled by ledTwoToggle
}
void loop() {
val = digitalRead(inputPin); //read value of pushbutton
if (val == LOW) { //if clause activated if pushbutton pressed and thus inputPin reads LOW
sendLedVal = !sendLedVal; //if clause activated if pushbutton pressed and thus inputPin reads LOW
digitalWrite(sendLedPin, sendLedVal ? HIGH : LOW); //write the appropriate HIGH/LOW to the sendLed pin to turn it ON/OFF
Spark.publish("Core1Toggle", sendLedVal ? "ON" : "OFF"); //publish the state to the Spark Cloud as ON/OFF
delay(250); //primitive button debouncing
}
}
void ledTwoToggle(const char *toggle, const char *onOff){ //handler function for Spark.subscribe()
if (strcmp(onOff, "ON") == 0){ //if sendLed on Core2 is ON according to Spark.publish()
digitalWrite(receiveLedPin, HIGH); //then turn on receiveLed
} else if (strcmp(onOff, "OFF") == 0){ //if sendLed on Core2 is OFF according to Spark.publish()
digitalWrite(receiveLedPin, LOW); //then turn off receiveLed
}
}
/*
This a program utilizing Spark.publish() and Spark.subscribe() to have pushbutton toggle LEDs communicate between two Spark Cores,
Core1 and Core2. When the local pushbutton is pressed on Core1, "sendLed" on Core1 will toggle state, while "receiveLed" on Core2 will follow
this state, and vice-versa. This is the code for Core2.
*/
int sendLedPin = D2; //choose the pin for the send (local) LED
int inputPin = D0; //choose the input pin (for a pushbutton)
int receiveLedPin = D1; //choose the pin for the receive (Core1-dependent LED)
int val = 0; //variable for reading the pushbutton pin status
int sendLedVal = 0; //variable for keeping the state of the local pushbutton dependent LED (D2)
void ledTwoToggle(const char *toggle, const char *onOff); //handler function for Spark.subscribe()
void setup() {
pinMode(receiveLedPin, OUTPUT); //Set the pin controlling the Core1-dependent LED as an OUTPUT
pinMode(sendLedPin, OUTPUT); //Set the pin controlling the local pushbutton-dependent LED as an OUTPUT
pinMode(inputPin, INPUT); //Set the pin connected to the pushbutton as an INPUT
digitalWrite(sendLedPin, LOW); //Ensure the local LED is set to off to begin with
Spark.publish("Core2Toggle", "State", 0, PRIVATE); //Set up Spark.publish() so that the state of the local LED is published to the Spark Cloud PRIVATELY
Spark.subscribe("Core1Toggle", ledTwoToggle, MY_DEVICES); //Set up Spark.subscribe() so that the state of Core1's Led is recorded and handled by ledTwoToggle
}
void loop() {
val = digitalRead(inputPin); //read value of pushbutton
if (val == LOW) { //if clause activated if pushbutton pressed and thus inputPin reads LOW
sendLedVal = !sendLedVal; //reverse state of sendLed variable value
digitalWrite(sendLedPin, sendLedVal ? HIGH : LOW); //write the appropriate HIGH/LOW to the sendLed pin to turn it ON/OFF
Spark.publish("Core2Toggle", sendLedVal ? "ON" : "OFF"); //publish the state to the Spark Cloud as ON/OFF
delay(250); //primitive button debouncing
}
}
void ledTwoToggle(const char *toggle, const char *onOff){ //handler function for Spark.subscribe()
if (strcmp(onOff, "ON") == 0){ //if sendLed on Core1 is ON according to Spark.publish()
digitalWrite(receiveLedPin, HIGH); //then turn on receiveLed
} else if (strcmp(onOff, "OFF") == 0){ //if sendLed on Core1 is OFF according to Spark.publish()
digitalWrite(receiveLedPin, LOW); //then turn off receiveLed
}
}
Comments