Advertisement
Guest User

code 1

a guest
Feb 12th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. /*
  2. Core1.
  3. */
  4.  
  5.  
  6. int sendLedPin = D7; //choose the pin for the send (local) LED
  7. int inputPin = D4; //choose the input pin (for a pushbutton)
  8. int receiveLedPin = D0; //choose the pin for the receive (Core2-dependent LED)
  9. int val = 0; //variable for reading the pushbutton pin status
  10. int sendLedVal = 0; //variable for keeping the state of the local pushbutton dependent LED (D2)
  11. void ledTwoToggle(const char *toggle, const char *onOff); //handler function for Spark.subscribe()
  12.  
  13. void setup() {
  14.     pinMode(receiveLedPin, OUTPUT); //Set the pin controlling the Core2-dependent LED as an OUTPUT
  15.     pinMode(sendLedPin, OUTPUT); //Set the pin controlling the local pushbutton-dependent LED as an OUTPUT
  16.     pinMode(inputPin, INPUT); //Set the pin connected to the pushbutton as an INPUT
  17.     digitalWrite(sendLedPin, LOW); //Ensure the local LED is set to off to begin with
  18.     digitalWrite(receiveLedPin, HIGH); //Ensure the local LED is set to off to begin with
  19.     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
  20.     Spark.subscribe("Core2Toggle", ledTwoToggle, MY_DEVICES); //Set up Spark.subscribe() so that the state of Core1's Led is recorded and handled by ledTwoToggle
  21. }
  22.  
  23. void loop() {
  24.     val = digitalRead(inputPin); //read value of pushbutton
  25.    
  26.     if (val == LOW) { //if clause activated if pushbutton pressed and thus inputPin reads LOW
  27.         sendLedVal = !sendLedVal; //if clause activated if pushbutton pressed and thus inputPin reads LOW
  28.         digitalWrite(sendLedPin, sendLedVal ? HIGH : LOW); //write the appropriate HIGH/LOW to the sendLed pin to turn it ON/OFF
  29.         Spark.publish("Core1Toggle", sendLedVal ? "ON" : "OFF"); //publish the state to the Spark Cloud as ON/OFF
  30.         delay(250); //primitive button debouncing
  31.     }
  32. }
  33.  
  34. void ledTwoToggle(const char *toggle, const char *onOff){ //handler function for Spark.subscribe()
  35.     if (strcmp(onOff, "ON") == 0){ //if sendLed on Core2 is ON according to Spark.publish()
  36.         digitalWrite(receiveLedPin, HIGH); //then turn on receiveLed
  37.     } else if (strcmp(onOff, "OFF") == 0){ //if sendLed on Core2 is OFF according to Spark.publish()
  38.         digitalWrite(receiveLedPin, LOW); //then turn off receiveLed
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement