Advertisement
Guest User

Core II

a guest
Nov 8th, 2015
31
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int sendLedPin = D7;
  2. int inputPin = D4;
  3. int inputPin2 = D3;
  4. int receiveLedPin = D0;
  5. int lastVal = 0;
  6. int lastVal2 = 0;
  7. int sendLedVal = 0;
  8. int sendLedVal2 = 0;
  9. void ledTwoToggle(const char *toggle, const char *onOff); //handler function for Spark.subscribe()
  10.  
  11.  
  12. void setup()
  13. {
  14.   pinMode(receiveLedPin, OUTPUT);
  15.   pinMode(sendLedPin, OUTPUT);
  16.   pinMode(inputPin2, INPUT_PULLUP);
  17.   pinMode(inputPin, INPUT_PULLUP);
  18.   digitalWrite(sendLedPin, LOW);
  19.   digitalWrite(receiveLedPin, LOW);
  20.   Spark.publish("Core2Toggle", "State", 0);
  21.   Spark.publish("Core3Toggle", "State", 0);
  22.   Spark.subscribe("Core1Toggle", ledTwoToggle);
  23. }
  24.  
  25. void loop()
  26. {
  27.   static unsigned long lastPressTime =0;
  28.   int val = digitalRead(inputPin);
  29.   if (val == LOW && lastVal == HIGH)
  30.   {
  31.     if (millis() - lastPressTime > 25UL) //less primitive (non-blocking) debounce time
  32.     {
  33.       sendLedVal = !sendLedVal;
  34.       digitalWrite(sendLedPin, sendLedVal ? HIGH : LOW);
  35.       Spark.publish("Core2Toggle", sendLedVal ? "ON" : "OFF");
  36.     }
  37.     lastPressTime = millis();
  38.   }
  39.   lastVal = val;
  40.  
  41.   static unsigned long lastPressTime2 =0;
  42.   int val2 = digitalRead(inputPin2);
  43.   if (val2 == LOW && lastVal2 == HIGH)
  44.   {
  45.     if (millis() - lastPressTime2 > 25UL) //less primitive (non-blocking) debounce time
  46.     {
  47.       sendLedVal2 = !sendLedVal2;
  48.       digitalWrite(sendLedPin, sendLedVal2 ? HIGH : LOW);
  49.       Spark.publish("Core3Toggle", sendLedVal2 ? "ON" : "OFF");
  50.     }
  51.     lastPressTime2 = millis();
  52.   }
  53.   lastVal2 = val2;
  54.  
  55. }
  56.  
  57. void ledTwoToggle(const char *toggle, const char *onOff)
  58. {
  59.   if (strcmp(onOff, "OFF") == 0)
  60.   {
  61.     digitalWrite(receiveLedPin, LOW);
  62.   }
  63.   else if (strcmp(onOff, "ON") == 0)
  64.   {
  65.     digitalWrite(receiveLedPin, HIGH);
  66.   }
  67. }
Advertisement
RAW Paste Data Copied
Advertisement