Alex Glow
Published

LightBlue Bean: Arduino Basics

This little Bluetooth board is ideal for wearable tech and tiny robots. Get started with these projects!

BeginnerProtip1 hour3,157
LightBlue Bean: Arduino Basics

Things used in this project

Story

Read more

Schematics

Buzzer circuit

Connect it to digital pin 0 and ground.

Code

Example code: "BeanBlink"

C/C++
Arduino demo code from the Bean Loader app.
/* 
  This sketch uses the Bean library to blink the on-board RGB LED. 
  
  Notes:
    - This is not a low-power sketch 
    - A Bean with a low battery might show a faint blue and green LED color
  
  This example code is in the public domain.
*/

void setup() {
}

void loop() {
  // Turn the Bean's LED red
  Bean.setLed(255, 0, 0);
  Bean.sleep(1000);      
  // Turn the Bean's LED green  
  Bean.setLed(0, 255, 0);
  Bean.sleep(1000);
  // Turn the Bean's LED blue
  Bean.setLed(0, 0, 255);
  Bean.sleep(1000);
  // Turn off the Bean's LED
  Bean.setLed(0, 0, 0);
  Bean.sleep(1000);
}

Example code: "AccelerationLed"

C/C++
Arduino sample code from the Bean Loader app.
/* 
  This sketch reads the acceleration from the Bean's on-board 
  accelerometer and displays it on the Bean's LED.
    
  This example code is in the public domain.
*/

void setup() {
  // Optional: Use Bean.setAccelerationRange() to set the sensitivity 
  // to something other than the default of ±2g.
}

// the loop routine runs over and over again forever:
void loop() {
   // Get the current acceleration with range of ±2g, 
   // and a conversion of 3.91×10-3 g/unit or 0.03834(m/s^2)/units. 
   AccelerationReading accel = Bean.getAcceleration();

   // Update LED color
   uint16_t r = (abs(accel.xAxis)) / 4;
   uint16_t g = (abs(accel.yAxis)) / 4;
   uint16_t b = (abs(accel.zAxis)) / 4;
   Bean.setLed((uint8_t)r,(uint8_t)g,(uint8_t)b);

   Bean.sleep(50);
}

Speaker code

C/C++
Produces a buzzer tone at approximately 500 Hz, on digital pin 0.
int spk = 0;

void setup() {
  pinMode(spk, OUTPUT);
}

void loop() {
  analogWrite(spk, 255);
    delay(1);
    analogWrite(spk, 0);
    delay(1);
}

Handy BLE Colors example

C/C++
Example code by P.D. Shelley, for the Handy BLE app. See https://github.com/pdshelley/ColorsExample/blob/master/ColorsExample/ColorsExample.ino
/*
  ColorsExample
  For use with the "Handy BLE" iPhone app by Paul Shelley and 
  the LightBlue Bean by Punchthrough Design.
  This sketch receives Serial data with a simple packet format
  of '#' for a start character and ';' as an end character. Start
  and end characters can be changed easily. Simple error checking 
  is also included. 
  
  Handy BLE - https://pdshelley.com
  LightBlueBean - https://punchthrough.com
  This example code is in the public domain.
  Created 9 June 2015
  by Paul Shelley
*/

String command;
boolean commandStarted = false;

void setup() {
  Serial.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 == "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();
  }
}

/*
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

Alex Glow

Alex Glow

145 projects • 1570 followers
The Hackster team's resident Hardware Nerd. I love robots, music, EEG, wearables, and languages. FIRST Robotics kid.

Comments