/ Munny_Lamp / Munny_Lamp.ino
Munny_Lamp.ino
1 // SPDX-FileCopyrightText: 2018 John Edgar Park for Adafruit Industries 2 // 3 // SPDX-License-Identifier: MIT 4 5 // MUNNY BLUEFRUIT LAMP 6 // Feather M0 Bluefruit + Prop-Maker Wing and 3W RDB LED 7 #include <string.h> 8 #include <Arduino.h> 9 #include <SPI.h> 10 #include <Adafruit_LIS3DH.h> 11 #include "Adafruit_BLE.h" 12 #include "Adafruit_BluefruitLE_SPI.h" 13 #include "Adafruit_BluefruitLE_UART.h" 14 #if SOFTWARE_SERIAL_AVAILABLE 15 #include <SoftwareSerial.h> 16 #endif 17 18 // pin definitions for using Prop-Maker FeatherWing 19 //#define NEOPIXEL_PIN 5 20 //#define SWITCH_PIN 9 21 #define POWER_PIN 10 22 #define RED_LED 11 23 #define GREEN_LED 12 24 #define BLUE_LED 13 25 int red = 0; 26 int green = 0; 27 int blue = 0; 28 #include "BluefruitConfig.h" 29 Adafruit_LIS3DH lis = Adafruit_LIS3DH(); 30 31 /*========================================================================= 32 APPLICATION SETTINGS 33 34 FACTORYRESET_ENABLE Perform a factory reset when running this sketch 35 36 Enabling this will put your Bluefruit LE module 37 in a 'known good' state and clear any config 38 data set in previous sketches or projects, so 39 running this at least once is a good idea. 40 41 When deploying your project, however, you will 42 want to disable factory reset by setting this 43 value to 0. If you are making changes to your 44 Bluefruit LE device via AT commands, and those 45 changes aren't persisting across resets, this 46 is the reason why. Factory reset will erase 47 the non-volatile memory where config data is 48 stored, setting it back to factory default 49 values. 50 51 Some sketches that require you to bond to a 52 central device (HID mouse, keyboard, etc.) 53 won't work at all with this feature enabled 54 since the factory reset will clear all of the 55 bonding data stored on the chip, meaning the 56 central device won't be able to reconnect. 57 PIN Which pin on the Arduino is connected to the NeoPixels? 58 NUMPIXELS How many NeoPixels are attached to the Arduino? 59 -----------------------------------------------------------------------*/ 60 #define FACTORYRESET_ENABLE 0 61 /*=========================================================================*/ 62 63 64 // Create the bluefruit object, either software serial...uncomment these lines 65 Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); 66 67 // A small helper 68 void error(const __FlashStringHelper*err) { 69 Serial.println(err); 70 while (1); 71 } 72 73 // function prototypes over in packetparser.cpp 74 uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout); 75 float parsefloat(uint8_t *buffer); 76 void printHex(const uint8_t * data, const uint32_t numBytes); 77 78 // the packet buffer 79 extern uint8_t packetbuffer[]; 80 81 82 /**************************************************************************/ 83 /*! 84 @brief Sets up the HW an the BLE module (this function is called 85 automatically on startup) 86 */ 87 /**************************************************************************/ 88 void setup(void) 89 { 90 delay(500); 91 pinMode(POWER_PIN, OUTPUT); 92 digitalWrite(POWER_PIN, HIGH); 93 pinMode(RED_LED, OUTPUT); 94 pinMode(GREEN_LED, OUTPUT); 95 pinMode(BLUE_LED, OUTPUT); 96 97 analogWrite(RED_LED, 0); 98 analogWrite(GREEN_LED, 0); 99 analogWrite(BLUE_LED, 255); // startup color, waiting for BLE connection 100 101 if (! lis.begin(0x18)) { // change this to 0x19 for alternative i2c address 102 Serial.println("Couldnt start LIS3DH"); 103 while (1); 104 } 105 106 Serial.begin(115200); 107 Serial.println(F("Adafruit Bluefruit MUNNY LED Color Picker")); 108 Serial.println(F("------------------------------------------------")); 109 110 /* Initialise the module */ 111 Serial.print(F("Initialising the Bluefruit LE module: ")); 112 113 if ( !ble.begin(VERBOSE_MODE) ) 114 { 115 error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?")); 116 } 117 Serial.println( F("OK!") ); 118 119 if ( FACTORYRESET_ENABLE ) 120 { 121 /* Perform a factory reset to make sure everything is in a known state */ 122 Serial.println(F("Performing a factory reset: ")); 123 if ( ! ble.factoryReset() ){ 124 error(F("Couldn't factory reset")); 125 } 126 } 127 128 /* Disable command echo from Bluefruit */ 129 ble.echo(false); 130 131 Serial.println("Requesting Bluefruit info:"); 132 /* Print Bluefruit information */ 133 ble.info(); 134 135 Serial.println(F("Please use Adafruit Bluefruit LE app to connect in Controller mode")); 136 Serial.println(F("Then activate/use the sensors, color picker, game controller, etc!")); 137 Serial.println(); 138 139 ble.verbose(false); // debug info is a little annoying after this point! 140 141 /* Wait for connection */ 142 while (! ble.isConnected()) { 143 delay(500); 144 } 145 146 Serial.println(F("***********************")); 147 148 // Set Bluefruit to DATA mode 149 Serial.println( F("Switching to DATA mode!") ); 150 ble.setMode(BLUEFRUIT_MODE_DATA); 151 152 Serial.println(F("***********************")); 153 154 } 155 156 /**************************************************************************/ 157 /*! 158 @brief Constantly poll for new command or response data 159 */ 160 /**************************************************************************/ 161 void loop(void) 162 { 163 digitalWrite(POWER_PIN, HIGH); 164 165 /* Wait for new data to arrive */ 166 uint8_t len = readPacket(&ble, BLE_READPACKET_TIMEOUT); 167 if (len == 0) { 168 accelerometer_check(); 169 return; 170 delay(10); 171 } 172 173 /* Got a packet! */ 174 // printHex(packetbuffer, len); 175 176 // Color 177 if (packetbuffer[1] == 'C') { 178 uint8_t red = packetbuffer[2]; 179 uint8_t green = packetbuffer[3]; 180 uint8_t blue = packetbuffer[4]; 181 Serial.print ("RGB #"); 182 if (red < 0x10) Serial.print("0"); 183 Serial.print(red, HEX); 184 if (green < 0x10) Serial.print("0"); 185 Serial.print(green, HEX); 186 if (blue < 0x10) Serial.print("0"); 187 Serial.println(blue, HEX); 188 189 analogWrite(RED_LED, red); 190 analogWrite(GREEN_LED, green); 191 analogWrite(BLUE_LED, blue); 192 } 193 194 } 195 196 void accelerometer_check() { 197 // Accelerometer 198 sensors_event_t event; 199 lis.getEvent(&event); 200 Serial.print("\t\tX: "); Serial.print(event.acceleration.x); 201 Serial.print(" \tY: "); Serial.print(event.acceleration.y); 202 Serial.print(" \tZ: "); Serial.print(event.acceleration.z); 203 Serial.println(" m/s^2 "); 204 205 if (event.acceleration.y < 0) { 206 analogWrite(RED_LED, random(0, 255)); 207 analogWrite(GREEN_LED, random(0, 255)); 208 analogWrite(BLUE_LED, random(0, 255)); 209 Serial.println("TILTED"); 210 delay(100); 211 } 212 }