Skip to content

Instantly share code, notes, and snippets.

@jkeefe
Created February 27, 2014 04:01
Show Gist options
  • Save jkeefe/9244135 to your computer and use it in GitHub Desktop.
Save jkeefe/9244135 to your computer and use it in GitHub Desktop.
/*
* The Chartbeat LED Bar
* (over Bluetooth)
*
* This is the Arduino code
*
* For information about the hardware and other
* code for this project
* see: http://johnkeefe.net/the-charbeat-led-bar
*
* Modified from the example docs for the
* BlueTooth Serial PhoneGap Plugin
* at https://github.com/don/BluetoothSerial
*
* John Keefe
* john@johnkeefe.net
* February 2014
*/
// load in the serial module for the bluetooth functions
#include <SoftwareSerial.h>
// set up a few constants
int neoPixelPin = 6; // Neopixel's comman pin
int bluetoothTx = 2; // TX-O pin of bluetooth mate
int bluetoothRx = 3; // RX-I pin of bluetooth mate
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
// load in the neopixel module for the LED strip functions
// more on neopixels at
// http://learn.adafruit.com/adafruit-neopixel-uberguide/arduino-library
#include <Adafruit_NeoPixel.h>
// Set up the LED strip info
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(60, neoPixelPin, NEO_GRB + NEO_KHZ800);
uint16_t color;
void setup() {
Serial.begin(9600);
pixels.begin();
pixels.show(); // Initialize all pixels to 'off'
setupBluetooth(); // Run the Bluetooth function below
Serial.println("Hello.");
}
void loop() {
int red = 0;
int green = 0;
int blue = 0;
int pixels_to_light = 0;
// Watches for a string in from the serial port like so:
// c red, green, blue \n
// for example: "c255,0,0\n" shows red
// for example: "c0,0,255\n" shows blue
if (bluetooth.find("c")) {
red = bluetooth.parseInt(); // parses numeric characters before the comma
green = bluetooth.parseInt();// parses numeric characters after the comma
blue = bluetooth.parseInt(); // parses numeric characters after the comma
Serial.print("Setting color to: " ); // print to console for debugging
Serial.print(red);Serial.print(", ");
Serial.print(green);Serial.print(", ");
Serial.println(blue);
showColor(red, green, blue);
}
// Watches for a string in from the serial port like so:
// p pixels_to_light \n
// for example: "p20\n" shows 20 pixels
if (bluetooth.find("p")) {
pixels_to_light = bluetooth.parseInt(); // parses numeric characters
Serial.print("Setting pixels to light to: " ); // print to console for debugging
Serial.println(pixels_to_light);
showPixels(pixels_to_light);
}
}
void showColor(int red, int green, int blue) {
uint32_t c = pixels.Color(red, green, blue);
for(uint16_t i=0; i<pixels.numPixels(); i++) {
pixels.setPixelColor(i, c);
}
pixels.show();
}
void showPixels(int pixels_to_light) {
// kill all the colors
showColor(0,0,0);
// check to make sure we're not trying to light more pixels than we have
if (pixels_to_light > pixels.numPixels()) {
pixels_to_light = pixels.numPixels();
}
// hard code the colors to use (for now)
uint32_t c;
uint32_t color1 = pixels.Color(255, 34, 9 ); // deep orange
uint32_t color2 = pixels.Color(192, 76, 0); // pretty yellow
uint32_t color3 = pixels.Color(0, 170, 0); // excellent green
// set them up
for(uint16_t i=0; i < pixels_to_light; i++) {
// this is setting each third of the strip one of the colors above
if (i < 60) {
c = color3;
}
if (i < 40) {
c = color2;
}
if (i < 20) {
c = color1;
}
pixels.setPixelColor(i, c);
}
// light them up
pixels.show();
}
// this initializes the Sparkfun Bluetooth radio (works for BLEmini too)
void setupBluetooth() {
bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
bluetooth.print("$$$"); // Enter command mode
delay(100); // Short delay, wait for the Mate to send back CMD
bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
// 115200 can be too fast at times for NewSoftSerial to relay the data reliably
bluetooth.begin(9600); // Start bluetooth serial at 9600
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment