Not-so final Final Submission?

Posted by flashstick - December 2, 2015

So the bluetooth-driven code works for the part of the skirt I have done, but the physical connections on the skirt are so finicky that I haven’t been able to test the accelerometer.  I need to find a less finicky way of connecting the lights, because conductive thread (even now that it’s insulated with tape) just doesn’t seem to want to work.

I jerry rigged the flora, bluetooth, and accelerometer onto the skirt with safety pins so it’s one unit that doesn’t need computer connection to work and they are pretty much where I plan to actually sew them on. when I have time.

In theory my code should work, and it does before you add the accelerometer.  I broke the code into three parts: The final code with the bluetooth and accelerometer, one code for bluetooth by itself, and one code for accelerometer by itself (to test without dependence on bluetooth).

The final code has the brightness driven by the accelerometer and the color driven by bluetooth.  Unfortunately if you move the skirt the lights freak out and generally don’t work, so this code is not testable currently.

Final code with accelerometer and bluetooth control, must be accompanied by packerParser.cpp and BluefruitConfig.h

/*********************************************************************
Code edit for Light-Up Skirt
*********************************************************************/
/*********************************************************************
This is an example for our nRF51822 based Bluefruit LE modules

Pick one up today in the adafruit shop!

Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

MIT license, check LICENSE for more information
All text above, and the splash screen below must be included in
any redistribution
*********************************************************************/
//bluefruit code//
#include <string.h>
#include <Arduino.h>
#include <SPI.h>
#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_)
#include <SoftwareSerial.h>
#endif

#include “Adafruit_BLE.h”
#include “Adafruit_BluefruitLE_SPI.h”
#include “Adafruit_BluefruitLE_UART.h”

#include “BluefruitConfig.h”

#include <Adafruit_NeoPixel.h>

//accelSensor code//
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303_U.h>

Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(54321);
//color variables//
uint8_t red = 20;
uint8_t green = 20;
uint8_t blue = 20;
uint8_t varY = 0;
/*=========================================================================
APPLICATION SETTINGS

FACTORYRESET_ENABLE    Perform a factory reset when running this sketch

Enabling this will put your Bluefruit LE module
in a ‘known good’ state and clear any config
data set in previous sketches or projects, so
running this at least once is a good idea.

When deploying your project, however, you will
want to disable factory reset by setting this
value to 0.  If you are making changes to your
Bluefruit LE device via AT commands, and those
changes aren’t persisting across resets, this
is the reason why.  Factory reset will erase
the non-volatile memory where config data is
stored, setting it back to factory default
values.

Some sketches that require you to bond to a
central device (HID mouse, keyboard, etc.)
won’t work at all with this feature enabled
since the factory reset will clear all of the
bonding data stored on the chip, meaning the
central device won’t be able to reconnect.
PIN Which pin on the Arduino is connected to the NeoPixels?
NUMPIXELS How many NeoPixels are attached to the Arduino?
———————————————————————–*/
#define FACTORYRESET_ENABLE 1

#define PIN 6
#define NUMPIXELS 11
/*=========================================================================*/

Adafruit_NeoPixel pixel = Adafruit_NeoPixel(NUMPIXELS, PIN);

// Create the bluefruit object as a hardware serial connection, which does not need the RTS/CTS pins.
Adafruit_BluefruitLE_UART ble(Serial1, BLUEFRUIT_UART_MODE_PIN);

// A small helper
void error(const __FlashStringHelper*err) {
Serial.println(err);
while (1);
}

// function prototypes over in packetparser.cpp
uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout);
float parsefloat(uint8_t *buffer);
void printHex(const uint8_t * data, const uint32_t numBytes);

// the packet buffer
extern uint8_t packetbuffer[];
/**************************************************************************/
/*!
@brief Sets up the HW an the BLE module (this function is called
automatically on startup)
*/
/**************************************************************************/
void setup(void)
{
/*
while (!Serial); // required for Flora & Micro
delay(500);
*/

// turn off neopixel
pixel.begin(); // This initializes the NeoPixel library.
for(uint8_t i=0; i<NUMPIXELS; i++) {
pixel.setPixelColor(i, pixel.Color(0,0,0)); // off
}
pixel.show();

Serial.begin(115200);
Serial.println(F(“Adafruit Bluefruit Neopixel Color Picker 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 Controller mode”));
Serial.println(F(“Then activate/use the sensors, color picker, game controller, etc!”));
Serial.println();

ble.verbose(false); // debug info is a little annoying after this point!

/* Wait for connection */
while (! ble.isConnected()) {
delay(500);
}

Serial.println(F(“*****************”));

// Set module to DATA mode
Serial.println( F(“Switching to DATA mode using +++!”) );
ble.println(“+++”);
delay(10);

Serial.println(F(“*****************”));

}

/**************************************************************************/
/*!
@brief Constantly poll for new command or response data
*/
/**************************************************************************/
void loop(void)
{
sensors_event_t event;
accel.getEvent(&event);
Serial.println(“accel”);

float accelX = event.acceleration.x;
float accelY = event.acceleration.y;
Serial.println(accelY);
float accelZ = event.acceleration.z;
if (abs(accelY) > 5){
varY = map(abs(accelY), 0, 60, 0, 1023);
Serial.println(varY);
}

/* Wait for new data to arrive */
uint8_t len = readPacket(&ble, BLE_READPACKET_TIMEOUT);
if (len == 0) return;

/* Got a packet! */
// printHex(packetbuffer, len);

// Color
if (packetbuffer[1] == ‘C’) {
red = packetbuffer[2];
green = packetbuffer[3];
blue = packetbuffer[4];
Serial.print (“RGB #”);
if (red < 0x10) Serial.print(“0”);
Serial.print(red, HEX);
if (green < 0x10) Serial.print(“0”);
Serial.print(green, HEX);
if (blue < 0x10) Serial.print(“0”);
Serial.println(blue, HEX);

uint8_t R = map(red, 0, 255, 0, varY);
uint8_t G = map(green, 0, 255, 0, varY);
uint8_t B = map(blue, 0, 255, 0, varY);

for(uint8_t i=0; i<NUMPIXELS; i++) {
pixel.setPixelColor(i, pixel.Color(R,G,B));
}
pixel.show(); // This sends the updated pixel color to the hardware.
}

}

Code with only bluetooth control, must be accompanied by packerParser.cpp and BluefruitConfig.h

/*********************************************************************

Code edit for Light-Up Skirt, bluetooth only
*********************************************************************/
/*********************************************************************
This is an example for our nRF51822 based Bluefruit LE modules

Pick one up today in the adafruit shop!

Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

MIT license, check LICENSE for more information
All text above, and the splash screen below must be included in
any redistribution
*********************************************************************/

#include <string.h>
#include <Arduino.h>
#include <SPI.h>
#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_)
#include <SoftwareSerial.h>
#endif

#include “Adafruit_BLE.h”
#include “Adafruit_BluefruitLE_SPI.h”
#include “Adafruit_BluefruitLE_UART.h”

#include “BluefruitConfig.h”

#include <Adafruit_NeoPixel.h>

/*=========================================================================
APPLICATION SETTINGS

FACTORYRESET_ENABLE    Perform a factory reset when running this sketch

Enabling this will put your Bluefruit LE module
in a ‘known good’ state and clear any config
data set in previous sketches or projects, so
running this at least once is a good idea.

When deploying your project, however, you will
want to disable factory reset by setting this
value to 0.  If you are making changes to your
Bluefruit LE device via AT commands, and those
changes aren’t persisting across resets, this
is the reason why.  Factory reset will erase
the non-volatile memory where config data is
stored, setting it back to factory default
values.

Some sketches that require you to bond to a
central device (HID mouse, keyboard, etc.)
won’t work at all with this feature enabled
since the factory reset will clear all of the
bonding data stored on the chip, meaning the
central device won’t be able to reconnect.
PIN Which pin on the Arduino is connected to the NeoPixels?
NUMPIXELS How many NeoPixels are attached to the Arduino?
———————————————————————–*/
#define FACTORYRESET_ENABLE 1

#define PIN 6
#define NUMPIXELS 11
/*=========================================================================*/

Adafruit_NeoPixel pixel = Adafruit_NeoPixel(NUMPIXELS, PIN);

// Create the bluefruit object as a hardware serial connection, which does not need the RTS/CTS pins.
Adafruit_BluefruitLE_UART ble(Serial1, BLUEFRUIT_UART_MODE_PIN);

// A small helper
void error(const __FlashStringHelper*err) {
Serial.println(err);
while (1);
}

// function prototypes over in packetparser.cpp
uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout);
float parsefloat(uint8_t *buffer);
void printHex(const uint8_t * data, const uint32_t numBytes);

// the packet buffer
extern uint8_t packetbuffer[];
/**************************************************************************/
/*!
@brief Sets up the HW an the BLE module (this function is called
automatically on startup)
*/
/**************************************************************************/
void setup(void)
{
/*
while (!Serial); // required for Flora & Micro
delay(500);
*/

// turn off neopixel
pixel.begin(); // This initializes the NeoPixel library.
for(uint8_t i=0; i<NUMPIXELS; i++) {
pixel.setPixelColor(i, pixel.Color(0,0,0)); // off
}
pixel.show();

Serial.begin(115200);
Serial.println(F(“Adafruit Bluefruit Neopixel Color Picker 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 Controller mode”));
Serial.println(F(“Then activate/use the sensors, color picker, game controller, etc!”));
Serial.println();

ble.verbose(false); // debug info is a little annoying after this point!

/* Wait for connection */
while (! ble.isConnected()) {
delay(500);
}

Serial.println(F(“*****************”));

// Set module to DATA mode
Serial.println( F(“Switching to DATA mode using +++!”) );
ble.println(“+++”);
delay(10);

Serial.println(F(“*****************”));

}

/**************************************************************************/
/*!
@brief Constantly poll for new command or response data
*/
/**************************************************************************/
void loop(void)
{
/* Wait for new data to arrive */
uint8_t len = readPacket(&ble, BLE_READPACKET_TIMEOUT);
if (len == 0) return;

/* Got a packet! */
// printHex(packetbuffer, len);

// Color
if (packetbuffer[1] == ‘C’) {
uint8_t red = packetbuffer[2];
uint8_t green = packetbuffer[3];
uint8_t blue = packetbuffer[4];
Serial.print (“RGB #”);
if (red < 0x10) Serial.print(“0”);
Serial.print(red, HEX);
if (green < 0x10) Serial.print(“0”);
Serial.print(green, HEX);
if (blue < 0x10) Serial.print(“0”);
Serial.println(blue, HEX);

for(uint8_t i=0; i<NUMPIXELS; i++) {
pixel.setPixelColor(i, pixel.Color(red,green,blue));
}
pixel.show(); // This sends the updated pixel color to the hardware.
}

}

 

Code with only accelerometer control, may or may not work.

/*********************************************************************
Code edit for Light-Up Skirt, accel only
*********************************************************************/
/*********************************************************************
This is an example for our nRF51822 based Bluefruit LE modules

Pick one up today in the adafruit shop!

Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

MIT license, check LICENSE for more information
All text above, and the splash screen below must be included in
any redistribution
*********************************************************************/
//bluefruit code//

#include <Adafruit_NeoPixel.h>

//accelSensor code//
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303.h>

Adafruit_LSM303 lsm;
//color variables//
uint8_t red = 20;
uint8_t green = 20;
uint8_t blue = 20;
uint8_t varY;
/*=========================================================================
APPLICATION SETTINGS

FACTORYRESET_ENABLE    Perform a factory reset when running this sketch

Enabling this will put your Bluefruit LE module
in a ‘known good’ state and clear any config
data set in previous sketches or projects, so
running this at least once is a good idea.

When deploying your project, however, you will
want to disable factory reset by setting this
value to 0.  If you are making changes to your
Bluefruit LE device via AT commands, and those
changes aren’t persisting across resets, this
is the reason why.  Factory reset will erase
the non-volatile memory where config data is
stored, setting it back to factory default
values.

Some sketches that require you to bond to a
central device (HID mouse, keyboard, etc.)
won’t work at all with this feature enabled
since the factory reset will clear all of the
bonding data stored on the chip, meaning the
central device won’t be able to reconnect.
PIN Which pin on the Arduino is connected to the NeoPixels?
NUMPIXELS How many NeoPixels are attached to the Arduino?
———————————————————————–*/
//#define FACTORYRESET_ENABLE 1

#define PIN 6
#define NUMPIXELS 11
/*=========================================================================*/

Adafruit_NeoPixel pixel = Adafruit_NeoPixel(NUMPIXELS, PIN);
/*
// Create the bluefruit object as a hardware serial connection, which does not need the RTS/CTS pins.
Adafruit_BluefruitLE_UART ble(Serial1, BLUEFRUIT_UART_MODE_PIN);

// A small helper
void error(const __FlashStringHelper*err) {
Serial.println(err);
while (1);
}

// function prototypes over in packetparser.cpp
uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout);
float parsefloat(uint8_t *buffer);
void printHex(const uint8_t * data, const uint32_t numBytes);

// the packet buffer
extern uint8_t packetbuffer[];
*/

/**************************************************************************/
/*!
@brief Sets up the HW an the BLE module (this function is called
automatically on startup)
*/
/**************************************************************************/
void setup(void)
{
/*
while (!Serial); // required for Flora & Micro
delay(500);
*/

// turn off neopixel
pixel.begin(); // This initializes the NeoPixel library.
for(uint8_t i=0; i<NUMPIXELS; i++) {
pixel.setPixelColor(i, pixel.Color(0,0,0)); // off
}
pixel.show();
}
/**************************************************************************/
/*!
@brief Constantly poll for new command or response data
*/
/**************************************************************************/
void loop(void)
{
lsm.read();
Serial.println(“accel”);

//get the accelerometer data

//float accelX = lsm.accelData.x;
float accelY = lsm.accelData.y;
Serial.println(accelY);
//float accelZ = lsm.accelData.z;

//map the accelerometer to a brightness for the lights
varY = map(abs(accelY), 0, 100, 200, 1023);
Serial.println(varY);
delay(500);

//map the RGB brightness to the desired color
uint8_t R = map(red, 0, 105, 0, varY);
uint8_t G = map(green, 0, 32, 0, varY);
uint8_t B = map(blue, 0, 117, 0, varY);
Serial.print (“R”); Serial.println(R);
Serial.print (“G”); Serial.println(G);
Serial.print (“B”); Serial.println(B);

for(uint8_t i=0; i<NUMPIXELS; i++) {
pixel.setPixelColor(i, pixel.Color(R,G,B));
}
pixel.show(); // This sends the updated pixel color to the hardware.
}

In:
No Comments yet, be the first to reply

Leave a Reply

Your email address will not be published. Required fields are marked *