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

89 lines
2.3 KiB
Arduino
Raw Normal View History

2013-08-07 17:16:13 +02:00
/*
Spacebrew Range
2013-08-07 17:16:13 +02:00
Demonstrates how to create a sketch that sends and receives analog
range value to and from Spacebrew. Every time the state of the
2013-08-07 17:16:13 +02:00
potentiometer (or other analog input component) change a spacebrew
message is sent. The sketch also accepts analog range messages from
2013-08-07 17:16:13 +02:00
other Spacebrew apps.
Make sure that your Yun is connected to the internet for this example
2013-08-07 17:16:13 +02:00
to function properly.
2013-08-07 17:16:13 +02:00
The circuit:
- Potentiometer connected to Yun. Middle pin connected to analog pin A0,
2013-08-07 17:16:13 +02:00
other pins connected to 5v and GND pins.
2013-08-07 17:16:13 +02:00
created 2013
by Julio Terra
2013-08-07 17:16:13 +02:00
This example code is in the public domain.
More information about Spacebrew is available at:
2013-08-07 17:16:13 +02:00
http://spacebrew.cc/
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 Range", "Range 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
void setup() {
2013-08-07 17:16:13 +02:00
// start the serial port
Serial.begin(57600);
2013-08-07 17:16:13 +02:00
// for debugging, wait until a serial console is connected
delay(4000);
while (!Serial) {
;
}
2013-08-07 17:16:13 +02:00
// start-up the bridge
Bridge.begin();
2013-08-07 17:16:13 +02:00
// configure the spacebrew object to print status messages to serial
sb.verbose(true);
2013-08-07 17:16:13 +02:00
// configure the spacebrew publisher and subscriber
sb.addPublish("physical pot", "range");
sb.addSubscribe("virtual pot", "range");
2013-08-07 17:16:13 +02:00
// register the string message handler method
sb.onRangeMessage(handleRange);
2013-08-07 17:16:13 +02:00
// connect to cloud spacebrew server at "sandbox.spacebrew.cc"
sb.connect("sandbox.spacebrew.cc");
}
2013-08-07 17:16:13 +02:00
void loop() {
// monitor spacebrew connection for new data
sb.monitor();
2013-08-07 17:16:13 +02:00
// connected to spacebrew then send a new value whenever the pot value changes
if ( sb.connected() ) {
int cur_value = analogRead(A0);
if ( last_value != cur_value ) {
sb.send("physical pot", cur_value);
last_value = cur_value;
}
}
}
2013-08-07 17:16:13 +02:00
// handler method that is called whenever a new string message is received
2013-08-07 17:16:13 +02:00
void handleRange (String route, int value) {
// print the message that was received
Serial.print("From ");
Serial.print(route);
Serial.print(", received msg: ");
Serial.println(value);
2013-08-07 17:16:13 +02:00
}