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 31SpacebrewYun sb = SpacebrewYun("spacebrewYun Range", "Range sender and receiver"); 32 33// variable that holds the last potentiometer value 34int last_value = 0; 35 36// create variables to manage interval between each time we send a string 37void setup() { 38 39 // start the serial port 40 SerialUSB.begin(57600); 41 42 // for debugging, wait until a serial console is connected 43 delay(4000); 44 while (!SerialUSB) { 45 ; 46 } 47 48 // start-up the bridge 49 Bridge.begin(); 50 51 // configure the spacebrew object to print status messages to serial 52 sb.verbose(true); 53 54 // configure the spacebrew publisher and subscriber 55 sb.addPublish("physical pot", "range"); 56 sb.addSubscribe("virtual pot", "range"); 57 58 // register the string message handler method 59 sb.onRangeMessage(handleRange); 60 61 // connect to cloud spacebrew server at "sandbox.spacebrew.cc" 62 sb.connect("sandbox.spacebrew.cc"); 63} 64 65 66void loop() { 67 // monitor spacebrew connection for new data 68 sb.monitor(); 69 70 // connected to spacebrew then send a new value whenever the pot value changes 71 if (sb.connected()) { 72 int cur_value = analogRead(A0); 73 if (last_value != cur_value) { 74 sb.send("physical pot", cur_value); 75 last_value = cur_value; 76 } 77 } 78} 79 80// handler method that is called whenever a new string message is received 81void handleRange(String route, int value) { 82 // print the message that was received 83 SerialUSB.print("From "); 84 SerialUSB.print(route); 85 SerialUSB.print(", received msg: "); 86 SerialUSB.println(value); 87} 88