Skip to content

Commit 1e2f6d1

Browse files
committed
add servo example
1 parent 29a25c0 commit 1e2f6d1

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Adafruit IO Servo Example
2+
// Tutorial Link: https://learn.adafruit.com/adafruit-io-basics-servo
3+
//
4+
// Adafruit invests time and resources providing this open source code.
5+
// Please support Adafruit and open source hardware by purchasing
6+
// products from Adafruit!
7+
//
8+
// Written by Todd Treece for Adafruit Industries
9+
// Copyright (c) 2016-2017 Adafruit Industries
10+
// Licensed under the MIT license.
11+
//
12+
// All text above must be included in any redistribution.
13+
14+
/************************** Configuration ***********************************/
15+
16+
// edit the config.h tab and enter your Adafruit IO credentials
17+
// and any additional configuration needed for WiFi, cellular,
18+
// or ethernet clients.
19+
#include "config.h"
20+
21+
/************************ Example Starts Here *******************************/
22+
23+
#include <Servo.h>
24+
25+
// pin used to control the servo
26+
#define SERVO_PIN 2
27+
28+
// create an instance of the servo class
29+
Servo servo;
30+
31+
// set up the 'servo' feed
32+
AdafruitIO_Feed *servo_feed = io.feed("servo");
33+
34+
void setup() {
35+
36+
// start the serial connection
37+
Serial.begin(115200);
38+
39+
// wait for serial monitor to open
40+
while(! Serial);
41+
42+
// tell the servo class which pin we are using
43+
servo.attach(SERVO_PIN);
44+
45+
// connect to io.adafruit.com
46+
Serial.print("Connecting to Adafruit IO");
47+
io.connect();
48+
49+
// set up a message handler for the 'servo' feed.
50+
// the handleMessage function (defined below)
51+
// will be called whenever a message is
52+
// received from adafruit io.
53+
servo_feed->onMessage(handleMessage);
54+
55+
// wait for a connection
56+
while(io.status() < AIO_CONNECTED) {
57+
Serial.print(".");
58+
delay(500);
59+
}
60+
61+
// we are connected
62+
Serial.println();
63+
Serial.println(io.statusText());
64+
65+
}
66+
67+
void loop() {
68+
69+
// io.run(); is required for all sketches.
70+
// it should always be present at the top of your loop
71+
// function. it keeps the client connected to
72+
// io.adafruit.com, and processes any incoming data.
73+
io.run();
74+
75+
}
76+
77+
// this function is called whenever a 'servo' message
78+
// is received from Adafruit IO. it was attached to
79+
// the servo feed in the setup() function above.
80+
void handleMessage(AdafruitIO_Data *data) {
81+
82+
// convert the data to integer
83+
int reading = data->toInt();
84+
85+
// make sure we don't exceed the limit
86+
// of the servo. the range is from 0
87+
// to 180.
88+
if(reading < 0)
89+
reading = 0;
90+
else if(reading > 180)
91+
reading = 180;
92+
93+
servo.write(reading);
94+
95+
}

examples/adafruitio_16_servo/config.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/************************ Adafruit IO Config *******************************/
2+
3+
// visit io.adafruit.com if you need to create an account,
4+
// or if you need your Adafruit IO key.
5+
#define IO_USERNAME "your_username"
6+
#define IO_KEY "your_key"
7+
8+
/******************************* WIFI **************************************/
9+
10+
// the AdafruitIO_WiFi client will work with the following boards:
11+
// - HUZZAH ESP8266 Breakout -> https://www.adafruit.com/products/2471
12+
// - Feather HUZZAH ESP8266 -> https://www.adafruit.com/products/2821
13+
// - Feather M0 WiFi -> https://www.adafruit.com/products/3010
14+
// - Feather WICED -> https://www.adafruit.com/products/3056
15+
16+
#define WIFI_SSID "your_ssid"
17+
#define WIFI_PASS "your_pass"
18+
19+
// comment out the following two lines if you are using fona or ethernet
20+
#include "AdafruitIO_WiFi.h"
21+
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
22+
23+
24+
/******************************* FONA **************************************/
25+
26+
// the AdafruitIO_FONA client will work with the following boards:
27+
// - Feather 32u4 FONA -> https://www.adafruit.com/product/3027
28+
29+
// uncomment the following two lines for 32u4 FONA,
30+
// and comment out the AdafruitIO_WiFi client in the WIFI section
31+
// #include "AdafruitIO_FONA.h"
32+
// AdafruitIO_FONA io(IO_USERNAME, IO_KEY);
33+
34+
35+
/**************************** ETHERNET ************************************/
36+
37+
// the AdafruitIO_Ethernet client will work with the following boards:
38+
// - Ethernet FeatherWing -> https://www.adafruit.com/products/3201
39+
40+
// uncomment the following two lines for ethernet,
41+
// and comment out the AdafruitIO_WiFi client in the WIFI section
42+
// #include "AdafruitIO_Ethernet.h"
43+
// AdafruitIO_Ethernet io(IO_USERNAME, IO_KEY);

0 commit comments

Comments
 (0)