Nod Bang: Building Objects in C++

Posted on
nodbang intro to pcomp itpnyu

This week I’ve focused on implementing the functionality of the buttons and the LED strand.

I learned to make C++ class objects for Arduino this week and how to stick it all in an Arduino library. It’s been extremely helpful for cleaning up and debugging. It will also be valuable if I decide to ever add more buttons and LEDs to this project.

Button Class

The above video demonstrates the Button class objects running. The piano sound is coming from Ableton Live.

The Button object handles:

  • attaching Arduino pin
  • sending MIDI events over USB
  • stores color info

Each button has a class instance representation in the code. While there’s now ~150 lines of code in an Arduino library, the usage in the main sketch is pretty clean.

#include <Nodbang.h>

Button button_a (9, 60); // pin, MIDI note
Button button_b (10, 62);
Button button_c (11, 64);
Button button_d (12, 65);

void setup() {

  Serial.begin(115200);
  button_a.set_rgb(203, 75, 22);  //orange
  button_b.set_rgb(133, 153, 0);  //green
  button_c.set_rgb(211, 54, 130); //magenta
  button_d.set_rgb(38, 139, 210); //blue

}

void loop() {

  button_a.refresh();
  button_b.refresh();
  button_c.refresh();
  button_d.refresh();

}

Next steps

Reintroducing accelerometer functionality!