Giovanni Gentile
Published © GPL3+

O-Zone: DIY Bluetooth Battery Lamp

O-Zone is a DIY Bluetooth lamp. You can modify the color of the lamp and also the brightness of leds.

IntermediateFull instructions provided2 hours384
O-Zone: DIY Bluetooth Battery Lamp

Things used in this project

Story

Read more

Code

Code snippet #1

Plain text
/* *******************************************  O-zone Bluethooth Lamp*  31 March 2017*  Giovanni Gentile*  for Punch Through Light BlueBean**********************************************/#include "Adafruit_NeoPixel.h"// The pin that is connected to the NeoPixels
#define PIN 5// The amount of LEDs in the NeoPixels
#define NUMPIXELS 16
int bright = 20;
boolean lumi = 1;
boolean previousLumi = 1;String command;
boolean commandStarted = false;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
LedReading ledColor;
LedReading previousLedColor;
void setup() {
  Serial.begin();
    // Initialize the NeoPixels 
  pixels.begin();
}void loop() {
  getCommand();
}/* 
This function reads the serial port and checks for the start character '#'
if the start character if found it will add all received characters to 
the command buffer until it receives the end command ';' When the end 
command is received the commandCompleted() function is called.
if a second start character is found before an end character then the buffer
is cleared and the process starts over. 
*/
void getCommand() {
   while (Serial.available()) {
    char newChar = (char)Serial.read();
    if (newChar == '#') {
      commandStarted = true;
      command = "\0";
    } else if (newChar == ';') {
      commandStarted = false;
      commandCompleted();
      command = "\0";
    } else if (commandStarted == true) {
      command += newChar;
    }
  }
}/*
This function takes the completed command and checks it against a list
of available commands and executes the appropriate code.  Add extra 'if' 
statements to add commands with the code you want to execute when that 
command is received. It is recommended to create a function for a command
if there are more than a few lines of code for as in the 'off' example.
*/
void commandCompleted() {
  if (command == "low") {
    bright = bright - 10;
    lumi = -lumi;
    Serial.print(bright);
  }
  if (command == "high") {
    bright = bright + 10;
    lumi = -lumi;
    Serial.print(bright);
  }
  if (command == "red") {
    Bean.setLed( 255, 0, 0 );
    Serial.print("LED turned red");
  }
  if (command == "green") {
    Bean.setLed( 0, 255, 0 ); 
    Serial.print("LED turned green");
  }
  if (command == "blue") {
    Bean.setLed( 0, 0, 255 );
    Serial.print("LED turned blue");
  }
  if (command == "yellow") {
    Bean.setLed( 255, 255, 0 );
    Serial.print("LED turned yellow"); 
  }
  if (command == "orange") {
    Bean.setLed( 255, 60, 0 ); 
    Serial.print("LED turned orange");
  }
  if (command == "purple") {
    Bean.setLed( 128, 0, 128 );
    Serial.print("LED turned purple");
  }
  if (command == "white") {
    Bean.setLed( 255, 255, 255 );
    Serial.print("LED turned white");
  }
  if (command == "off") {
    off();
  }
  if(Bean.getConnectionState()){
    // Get the values from the Bean's onboard LED
    ledColor = Bean.getLed();
  }
  if(lumi != previousLumi || ledColor.red != previousLedColor.red || ledColor.green != previousLedColor.green || ledColor.blue != previousLedColor.blue){
    for(int i=0;i/*
Use a separate function like this when there are more than just a few
lines of code.  This will help maintain clean easy to read code.
*/
void off() {
  Bean.setLed( 0, 0, 0 );
  Serial.print("LED turned off");
}

Credits

Giovanni Gentile

Giovanni Gentile

31 projects • 63 followers
Graduated in Psychology expert in new technologies and communication, with expertise in electronics, automation and home automation.

Comments

Add projectSign up / Login