1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-13 23:48:46 +01:00
Arduino/libraries/Bridge/examples/SpacebrewYun/spacebrewBoolean/spacebrewBoolean.ino

90 lines
2.3 KiB
Arduino
Raw Normal View History

2013-08-07 17:16:13 +02:00
/*
Spacebrew Boolean
2014-04-01 09:35:56 +02:00
2013-08-07 17:16:13 +02:00
Demonstrates how to create a sketch that sends and receives a
2014-04-01 09:35:56 +02:00
boolean value to and from Spacebrew. Every time the buttton is
pressed (or other digital input component) a spacebrew message
is sent. The sketch also accepts analog range messages from
2013-08-07 17:16:13 +02:00
other Spacebrew apps.
2014-04-01 09:35:56 +02:00
Make sure that your Yun is connected to the internet for this example
2013-08-07 17:16:13 +02:00
to function properly.
2014-04-01 09:35:56 +02:00
2013-08-07 17:16:13 +02:00
The circuit:
- Button connected to Yun, using the Arduino's internal pullup resistor.
2014-04-01 09:35:56 +02:00
2013-08-07 17:16:13 +02:00
created 2013
by Julio Terra
2014-04-01 09:35:56 +02:00
2013-08-07 17:16:13 +02:00
This example code is in the public domain.
2014-04-01 09:35:56 +02:00
More information about Spacebrew is available at:
2013-08-07 17:16:13 +02:00
http://spacebrew.cc/
2014-04-01 09:35:56 +02:00
2013-08-07 17:16:13 +02:00
*/
#include <Bridge.h>
#include <SpacebrewYun.h>
// create a variable of type SpacebrewYun and initialize it with the constructor
SpacebrewYun sb = SpacebrewYun("spacebrewYun Boolean", "Boolean sender and receiver");
// variable that holds the last potentiometer value
int last_value = 0;
// create variables to manage interval between each time we send a string
2014-04-01 09:35:56 +02:00
void setup() {
2013-08-07 17:16:13 +02:00
2014-04-01 09:35:56 +02:00
// start the serial port
Serial.begin(57600);
2013-08-07 17:16:13 +02:00
2014-04-01 09:35:56 +02:00
// for debugging, wait until a serial console is connected
delay(4000);
while (!Serial) { ; }
2013-08-07 17:16:13 +02:00
2014-04-01 09:35:56 +02:00
// start-up the bridge
Bridge.begin();
2013-08-07 17:16:13 +02:00
2014-04-01 09:35:56 +02:00
// configure the spacebrew object to print status messages to serial
sb.verbose(true);
2013-08-07 17:16:13 +02:00
2014-04-01 09:35:56 +02:00
// configure the spacebrew publisher and subscriber
sb.addPublish("physical button", "boolean");
sb.addSubscribe("virtual button", "boolean");
2013-08-07 17:16:13 +02:00
2014-04-01 09:35:56 +02:00
// register the string message handler method
sb.onBooleanMessage(handleBoolean);
2013-08-07 17:16:13 +02:00
2014-04-01 09:35:56 +02:00
// connect to cloud spacebrew server at "sandbox.spacebrew.cc"
sb.connect("sandbox.spacebrew.cc");
2013-08-07 17:16:13 +02:00
2014-04-01 09:35:56 +02:00
pinMode(3, INPUT);
digitalWrite(3, HIGH);
}
2013-08-07 17:16:13 +02:00
2014-04-01 09:35:56 +02:00
void loop() {
// monitor spacebrew connection for new data
sb.monitor();
2013-08-07 17:16:13 +02:00
2014-04-01 09:35:56 +02:00
// connected to spacebrew then send a new value whenever the pot value changes
if ( sb.connected() ) {
int cur_value = digitalRead(3);
if ( last_value != cur_value ) {
if (cur_value == HIGH) sb.send("physical button", false);
else sb.send("physical button", true);
last_value = cur_value;
}
}
}
2013-08-07 17:16:13 +02:00
2014-04-01 09:35:56 +02:00
// handler method that is called whenever a new string message is received
2013-08-07 17:16:13 +02:00
void handleBoolean (String route, boolean value) {
2014-04-01 09:35:56 +02:00
// print the message that was received
Serial.print("From ");
Serial.print(route);
Serial.print(", received msg: ");
Serial.println(value ? "true" : "false");
2013-08-07 17:16:13 +02:00
}