Punch Through
Created December 7, 2015 © MIT

LightBlue Sandbox: Trackpad Control

Control your Bean's LED with the Bean Sandbox in LightBlue Explorer for iOS!

BeginnerProtip304
LightBlue Sandbox: Trackpad Control

Things used in this project

Hardware components

LightBlue Bean
Punch Through LightBlue Bean
×1

Hand tools and fabrication machines

Punch Through LightBlue Explorer for iOS

Story

Read more

Code

TrackpadControl.ino

Arduino
#define BUF_SIZE 64

// Sandbox control IDs
const char MSG_XYPAD_X = 8;
const char MSG_XYPAD_Y = 9;

// State variables and default values
char rate = 40;
char intensity = 127;
char buffer[BUF_SIZE] = {0};

void setup() {
  // Bean starts serial automatically; no need to set baud rate
  // Prevent Serial.readBytes from blocking when the user isn't sending us data
  Serial.setTimeout(5);
}

void loop() {
  length = Serial.readBytes(buffer, BUF_SIZE);

  if (length > 0) {
    // Iterate over sandbox messages by processing groups of two bytes.
    for (char i = 0; i < length - 1; i += 2) {
      if (buffer[i] == MSG_XYPAD_X) {
        rate = buffer[i + 1];
        // Cap the blink rate at a minimum period of 10 ms
        if (rate < 5) {
          rate = 5;
        }

      } else if (buffer[i] == MSG_XYPAD_Y) {
        intensity = buffer[i + 1];
      }
    }
  }

  // Blink the LED at the configured intensity and rate
  Bean.setLed(0, intensity, intensity);
  delay(rate);
  Bean.setLed(0, 0, 0);
  delay(rate);
}

Credits

Punch Through
16 projects • 41 followers
We’ve been building connected products since 2009. Our diverse team has expertise in every layer from hardware to software to web.
Contact

Comments

Please log in or sign up to comment.