Punch Through
Published © MIT

iPhone controlled NeoPixel sketchpad

Curious how to light up a NeoPixel sketchpad through your iPhone? Find out how through this tutorial!

IntermediateFull instructions provided2,660

Things used in this project

Hardware components

LightBlue Bean
Punch Through LightBlue Bean
×1
8×8 Neopixel NeoMatrix
×1
AA batteries
×2
Battery holder
×1
large capacitor
1000 µF, 6.3V or higher
×1
300 to 500 Ohm resistor
×1
Jumper wires (generic)
Jumper wires (generic)
×1
iPhone
Apple iPhone
×1

Software apps and online services

Punch Through Bean Loader

Story

Read more

Schematics

Schematics

File missing, please reupload.

Code

Code

C/C++
#include <Adafruit_NeoPixel.h>
#define numOfLEDs 64
Adafruit_NeoPixel strip = Adafruit_NeoPixel(numOfLEDs, 4, NEO_GRB + NEO_KHZ800);
// The control # from the Sandbox X-Y trackpad screen
#define XYPAD_X 8
#define XYPAD_Y 9
#define SLIDER 11
#define CHECKBOX 12

// If drawingMode=true LEDs won't turn off
// when you change position on the X-Y trackpad
bool drawingMode = true;
int LEDsTurnedOn[numOfLEDs];
uint32_t color = 0;
byte pixel_X = 0;
byte pixel_Y = 0;
byte buffer[10];

void setup() {
  Serial.begin(57600);
  Serial.setTimeout(5);
  strip.begin();
  strip.show();  // Initialize all pixels to 'off'
}

void loop() {
  uint8_t* buf = reinterpret_cast(buffer)
  size_t length = Serial.readBytes(buf, 2);

  if ( length > 0 ) {
      // buffer[i] : Control #
      byte controlByte = buffer[0];
      // buffer[i+1] : Value
      byte valueByte = buffer[1];

      switch (controlByte) {
        case XYPAD_X:
          // valueByte goes from 0-255.
          // To match the NeoPixel we divide by 32
          pixel_X = valueByte/32;
          break;
        case XYPAD_Y:
          pixel_Y = valueByte/32;
          // Invert the Y coordinates to match the NeoPixel
          pixel_Y = 7 - pixel_Y;
          break;
        case SLIDER:
          // Set the color to the slider value
          color = valueByte;
          break;
        case CHECKBOX:
          // Toggle between drawing/not drawing with the checkbox
          drawingMode = valueByte;
          break;
    }

      // The NeoPixel LEDs are indexed from 0-63 which corresponds to X+8*Y
      byte cursorLED = pixel_X + (8*pixel_Y);
      strip.setPixelColor(cursorLED, Wheel(color));
      strip.show();
      LEDsTurnedOn[cursorLED] = 1;
      // Turn off all LEDs except for the cursorLED if not in drawingMode
      if (!drawingMode) {
      for (int i = 0; i < sizeof(LEDsTurnedOn); i++) {
        if (LEDsTurnedOn[i] == 1 && i != cursorLED) {
           strip.setPixelColor(i, strip.Color(0, 0, 0));
           strip.show();
           LEDsTurnedOn[i] = 0;
        }
      }
    }
  }
}

// Function to generate a color value from the slider
uint32_t Wheel(byte WheelPos) {
  if (WheelPos < 85) {
    return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if (WheelPos < 170) {
      WheelPos -= 85;
      return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
    } else {
        WheelPos -= 170;
        return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

Github

https://github.com/adafruit/Adafruit_NeoPixel

Credits

Punch Through

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.

Comments