Last active
May 21, 2023 09:21
-
-
Save srmo/a45d9e8c00e50d09876bfef50aea61d9 to your computer and use it in GitHub Desktop.
Copypastasketch to combine UART Data mode and FastLED Demoreel Rainbow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <SPI.h> | |
#include "Adafruit_BLE.h" | |
#include "Adafruit_BluefruitLE_SPI.h" | |
#include "Adafruit_BluefruitLE_UART.h" | |
#include <FastLED.h> | |
#include "BluefruitConfig.h" | |
#if SOFTWARE_SERIAL_AVAILABLE | |
#include <SoftwareSerial.h> | |
#endif | |
#define FACTORYRESET_ENABLE 1 | |
#define MINIMUM_FIRMWARE_VERSION "0.6.6" | |
#define MODE_LED_BEHAVIOUR "MODE" | |
FASTLED_USING_NAMESPACE | |
#if defined(FASTLED_VERSION) && (FASTLED_VERSION < 3001000) | |
#warning "Requires FastLED 3.1 or later; check github for latest code." | |
#endif | |
#define DATA_PIN 6 | |
//#define CLK_PIN 4 | |
#define LED_TYPE WS2811 | |
#define COLOR_ORDER GRB | |
#define NUM_LEDS 218 | |
CRGB leds[NUM_LEDS]; | |
#define BRIGHTNESS 96 | |
#define FRAMES_PER_SECOND 120 | |
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0])) | |
uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current | |
uint8_t gHue = 0; // rotating "base color" used by many of the patterns | |
void rainbow() | |
{ | |
// FastLED's built-in rainbow generator | |
fill_rainbow( leds, NUM_LEDS, gHue, 7); | |
} | |
/**** THIS CAN BE REMOVED/BUT NEEDS CAREFUL CLEANUP */ | |
// List of patterns to cycle through. Each is defined as a separate function below. | |
typedef void (*SimplePatternList[])(); | |
//SimplePatternList gPatterns = { rainbow, rainbowWithGlitter, confetti, sinelon, juggle, bpm }; | |
SimplePatternList gPatterns = { rainbow }; | |
Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); | |
void error(const __FlashStringHelper*err) { | |
Serial.println(err); | |
while (1); | |
} | |
void setup_bluetooth() { | |
// while (!Serial); // required for Flora & Micro | |
// delay(500); | |
// | |
Serial.begin(115200); | |
Serial.println(F("Adafruit Bluefruit Command <-> Data Mode Example")); | |
Serial.println(F("------------------------------------------------")); | |
/* Initialise the module */ | |
Serial.print(F("Initialising the Bluefruit LE module: ")); | |
if ( !ble.begin(VERBOSE_MODE) ) | |
{ | |
error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?")); | |
} | |
Serial.println( F("OK!") ); | |
if ( FACTORYRESET_ENABLE ) | |
{ | |
/* Perform a factory reset to make sure everything is in a known state */ | |
Serial.println(F("Performing a factory reset: ")); | |
if ( ! ble.factoryReset() ){ | |
error(F("Couldn't factory reset")); | |
} | |
} | |
/* Disable command echo from Bluefruit */ | |
ble.echo(false); | |
Serial.println("Requesting Bluefruit info:"); | |
/* Print Bluefruit information */ | |
ble.info(); | |
Serial.println(F("Please use Adafruit Bluefruit LE app to connect in UART mode")); | |
Serial.println(F("Then Enter characters to send to Bluefruit")); | |
Serial.println(); | |
ble.verbose(false); // debug info is a little annoying after this point! | |
/* Wait for connection */ | |
while (! ble.isConnected()) { | |
Serial.println("Waiting for ble connect"); | |
delay(500); | |
} | |
Serial.println(F("******************************")); | |
ble.sendCommandCheckOK("AT+HWModeLED=" MODE_LED_BEHAVIOUR); | |
// Set module to DATA mode | |
Serial.println( F("Switching to DATA mode!") ); | |
ble.setMode(BLUEFRUIT_MODE_DATA); | |
Serial.println(F("******************************")); | |
} | |
void setup_leds() { | |
delay(3000); // 3 second delay for recovery | |
// tell FastLED about the LED strip configuration | |
FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); | |
//FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); | |
// set master brightness control | |
FastLED.setBrightness(BRIGHTNESS); | |
} | |
void setup() { | |
setup_bluetooth(); | |
setup_leds(); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
// Call the current pattern function once, updating the 'leds' array | |
//gPatterns[gCurrentPatternNumber](); | |
rainbow(); | |
// send the 'leds' array out to the actual LED strip | |
FastLED.show(); | |
// insert a delay to keep the framerate modest | |
FastLED.delay(1000/FRAMES_PER_SECOND); | |
// do some periodic updates | |
EVERY_N_MILLISECONDS( 10 ) { gHue++; } // slowly cycle the "base color" through the rainbow | |
//EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically | |
// /**** HERE STARTS THE BLUETOOTH CODE ****/ | |
// // Check for user input | |
// char n, inputs[BUFSIZE+1]; | |
// | |
// // Echo received data | |
// while ( ble.available() ) | |
// { | |
// int c = ble.read(); | |
// | |
// Serial.print((char)c); | |
// | |
// // Hex output too, helps w/debugging! | |
// Serial.print(" [0x"); | |
// if (c <= 0xF) Serial.print(F("0")); | |
// Serial.print(c, HEX); | |
// Serial.print("] "); | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment