libraries / Bridge / examples / SpacebrewYun / spacebrewBoolean / spacebrewBoolean.inoon commit Added link to project report (97a3ba0)
   1/*
   2  Spacebrew Boolean
   3
   4 Demonstrates how to create a sketch that sends and receives a
   5 boolean value to and from Spacebrew. Every time the buttton is
   6 pressed (or other digital input component) a spacebrew message
   7 is sent. The sketch also accepts analog range messages from
   8 other Spacebrew apps.
   9
  10 Make sure that your Yún is connected to the internet for this example
  11 to function properly.
  12
  13 The circuit:
  14 - Button connected to Yún, using the Arduino's internal pullup resistor.
  15
  16 created 2013
  17 by Julio Terra
  18
  19 This example code is in the public domain.
  20
  21 More information about Spacebrew is available at:
  22 http://spacebrew.cc/
  23
  24 */
  25
  26#include <Bridge.h>
  27#include <SpacebrewYun.h>
  28
  29// create a variable of type SpacebrewYun and initialize it with the constructor
  30SpacebrewYun sb = SpacebrewYun("spacebrewYun Boolean", "Boolean sender and receiver");
  31
  32// variable that holds the last potentiometer value
  33int last_value = 0;
  34
  35// create variables to manage interval between each time we send a string
  36void setup() {
  37
  38  // start the serial port
  39  SerialUSB.begin(57600);
  40
  41  // for debugging, wait until a serial console is connected
  42  delay(4000);
  43  while (!SerialUSB) {
  44    ;
  45  }
  46
  47  // start-up the bridge
  48  Bridge.begin();
  49
  50  // configure the spacebrew object to print status messages to serial
  51  sb.verbose(true);
  52
  53  // configure the spacebrew publisher and subscriber
  54  sb.addPublish("physical button", "boolean");
  55  sb.addSubscribe("virtual button", "boolean");
  56
  57  // register the string message handler method
  58  sb.onBooleanMessage(handleBoolean);
  59
  60  // connect to cloud spacebrew server at "sandbox.spacebrew.cc"
  61  sb.connect("sandbox.spacebrew.cc");
  62
  63  pinMode(3, INPUT);
  64  digitalWrite(3, HIGH);
  65}
  66
  67
  68void loop() {
  69  // monitor spacebrew connection for new data
  70  sb.monitor();
  71
  72  // connected to spacebrew then send a new value whenever the pot value changes
  73  if (sb.connected()) {
  74    int cur_value = digitalRead(3);
  75    if (last_value != cur_value) {
  76      if (cur_value == HIGH) {
  77        sb.send("physical button", false);
  78      } else {
  79        sb.send("physical button", true);
  80      }
  81      last_value = cur_value;
  82    }
  83  }
  84}
  85
  86// handler method that is called whenever a new string message is received
  87void handleBoolean(String route, boolean value) {
  88  // print the message that was received
  89  SerialUSB.print("From ");
  90  SerialUSB.print(route);
  91  SerialUSB.print(", received msg: ");
  92  SerialUSB.println(value ? "true" : "false");
  93}
  94