[136] | 1 | /*
|
---|
| 2 | Spacebrew Range
|
---|
| 3 |
|
---|
| 4 | Demonstrates how to create a sketch that sends and receives analog
|
---|
| 5 | range value to and from Spacebrew. Every time the state of the
|
---|
| 6 | potentiometer (or other analog input component) change a spacebrew
|
---|
| 7 | message 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 | - Potentiometer connected to Y炭n. Middle pin connected to analog pin A0,
|
---|
| 15 | other pins connected to 5v and GND pins.
|
---|
| 16 |
|
---|
| 17 | created 2013
|
---|
| 18 | by Julio Terra
|
---|
| 19 |
|
---|
| 20 | This example code is in the public domain.
|
---|
| 21 |
|
---|
| 22 | More information about Spacebrew is available at:
|
---|
| 23 | http://spacebrew.cc/
|
---|
| 24 |
|
---|
| 25 | */
|
---|
| 26 |
|
---|
| 27 | #include <Bridge.h>
|
---|
| 28 | #include <SpacebrewYun.h>
|
---|
| 29 |
|
---|
| 30 | // create a variable of type SpacebrewYun and initialize it with the constructor
|
---|
| 31 | SpacebrewYun sb = SpacebrewYun("spacebrewYun Range", "Range sender and receiver");
|
---|
| 32 |
|
---|
| 33 | // variable that holds the last potentiometer value
|
---|
| 34 | int last_value = 0;
|
---|
| 35 |
|
---|
| 36 | // create variables to manage interval between each time we send a string
|
---|
| 37 | void setup() {
|
---|
| 38 |
|
---|
| 39 | // start the serial port
|
---|
| 40 | Serial.begin(57600);
|
---|
| 41 |
|
---|
| 42 | // for debugging, wait until a serial console is connected
|
---|
| 43 | delay(4000);
|
---|
| 44 | while (!Serial) { ; }
|
---|
| 45 |
|
---|
| 46 | // start-up the bridge
|
---|
| 47 | Bridge.begin();
|
---|
| 48 |
|
---|
| 49 | // configure the spacebrew object to print status messages to serial
|
---|
| 50 | sb.verbose(true);
|
---|
| 51 |
|
---|
| 52 | // configure the spacebrew publisher and subscriber
|
---|
| 53 | sb.addPublish("physical pot", "range");
|
---|
| 54 | sb.addSubscribe("virtual pot", "range");
|
---|
| 55 |
|
---|
| 56 | // register the string message handler method
|
---|
| 57 | sb.onRangeMessage(handleRange);
|
---|
| 58 |
|
---|
| 59 | // connect to cloud spacebrew server at "sandbox.spacebrew.cc"
|
---|
| 60 | sb.connect("sandbox.spacebrew.cc");
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 |
|
---|
| 64 | void loop() {
|
---|
| 65 | // monitor spacebrew connection for new data
|
---|
| 66 | sb.monitor();
|
---|
| 67 |
|
---|
| 68 | // connected to spacebrew then send a new value whenever the pot value changes
|
---|
| 69 | if ( sb.connected() ) {
|
---|
| 70 | int cur_value = analogRead(A0);
|
---|
| 71 | if ( last_value != cur_value ) {
|
---|
| 72 | sb.send("physical pot", cur_value);
|
---|
| 73 | last_value = cur_value;
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | // handler method that is called whenever a new string message is received
|
---|
| 79 | void handleRange (String route, int value) {
|
---|
| 80 | // print the message that was received
|
---|
| 81 | Serial.print("From ");
|
---|
| 82 | Serial.print(route);
|
---|
| 83 | Serial.print(", received msg: ");
|
---|
| 84 | Serial.println(value);
|
---|
| 85 | }
|
---|
| 86 |
|
---|