dashboard_control.ino
1 // SPDX-FileCopyrightText: 2018 Dave Astels for Adafruit Industries 2 // 3 // SPDX-License-Identifier: MIT 4 5 // Crickit + Adafruit IO Subscribe Example 6 // 7 // Adafruit invests time and resources providing this open source code. 8 // Please support Adafruit and open source hardware by purchasing 9 // products from Adafruit! 10 // 11 // Written by Dave Astels for Adafruit Industries 12 // Copyright (c) 2018 Adafruit Industries 13 // Licensed under the MIT license. 14 // 15 // All text above must be included in any redistribution. 16 17 /************************** Configuration ***********************************/ 18 19 // edit the config.h tab and enter your Adafruit IO credentials 20 // and any additional configuration needed for WiFi, cellular, 21 // or ethernet clients. 22 #include "config.h" 23 #include <Adafruit_Crickit.h> 24 #include <seesaw_servo.h> 25 #include <seesaw_neopixel.h> 26 27 #define NEOPIX_PIN (20) /* Neopixel pin */ 28 #define NEOPIX_NUMBER_OF_PIXELS (7) 29 30 31 // set up the feeds 32 AdafruitIO_Feed *servo1_control; 33 AdafruitIO_Feed *neopixel_control; 34 35 // set up the Crickit 36 37 Adafruit_Crickit crickit; 38 seesaw_Servo servo_1(&crickit); // create servo object to control a servo 39 40 // Parameter 1 = number of pixels in strip 41 // Parameter 2 = Arduino pin number (most are valid) 42 // Parameter 3 = pixel type flags, add together as needed: 43 // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) 44 // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) 45 // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) 46 // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) 47 // NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products) 48 seesaw_NeoPixel strip = seesaw_NeoPixel(NEOPIX_NUMBER_OF_PIXELS, NEOPIX_PIN, NEO_GRB + NEO_KHZ800); 49 50 void setup_feeds() 51 { 52 servo1_control = io.feed("crickit.servo1-control"); 53 neopixel_control = io.feed("crickit.neopixel-control"); 54 } 55 56 57 void setup() 58 { 59 // start the serial connection 60 Serial.begin(115200); 61 62 // wait for serial monitor to open 63 while(! Serial); 64 65 setup_feeds(); 66 Serial.println("Feeds set up"); 67 68 Serial.println("Connecting to Adafruit IO"); 69 70 // connect to io.adafruit.com 71 io.connect(); 72 73 // set up message handlers for the servo and neopixel feeds. 74 // the handle_*_message functions (defined below) 75 // will be called whenever a message is 76 // received from adafruit io. 77 78 servo1_control->onMessage(handle_servo_message); 79 neopixel_control->onMessage(handle_neopixel_message); 80 81 // wait for a connection 82 while(io.status() < AIO_CONNECTED) { 83 Serial.print("."); 84 // Serial.println(io.statusText()); 85 delay(500); 86 } 87 88 // we are connected 89 Serial.println(); 90 Serial.println(io.statusText()); 91 92 if (!crickit.begin()) { 93 Serial.println("Error starting Crickit!"); 94 while(1); 95 } else { 96 Serial.println("Crickit started"); 97 } 98 99 if(!strip.begin()){ 100 Serial.println("Error starting Neopixels!"); 101 while(1); 102 } else { 103 Serial.println("Neopixels started"); 104 } 105 106 servo1_control->get(); 107 108 servo_1.attach(CRICKIT_SERVO1); 109 110 Serial.println("setup complete"); 111 } 112 113 114 void loop() 115 { 116 117 // io.run(); is required for all sketches. 118 // it should always be present at the top of your loop 119 // function. it keeps the client connected to 120 // io.adafruit.com, and processes any incoming data. 121 io.run(); 122 } 123 124 125 void handle_servo_message(AdafruitIO_Data *data) 126 { 127 Serial.print("received servo control <- "); 128 Serial.println(data->value()); 129 int angle = data->toInt(); 130 if(angle < 0) { 131 angle = 0; 132 } else if(angle > 180) { 133 angle = 180; 134 } 135 servo_1.write(angle); 136 137 } 138 139 140 void handle_neopixel_message(AdafruitIO_Data *data) 141 { 142 Serial.print("received neopixel control <- "); 143 Serial.println(data->value()); 144 long color = data->toNeoPixel(); 145 for (int pixel = 0; pixel < NEOPIX_NUMBER_OF_PIXELS; pixel++) { 146 strip.setPixelColor(pixel, color); 147 } 148 strip.show(); 149 }