Skip to content

Instantly share code, notes, and snippets.

@capnmidnight
Last active November 4, 2016 13:37
Show Gist options
  • Save capnmidnight/07ef7298ef1012acf4c4c449aa552d0e to your computer and use it in GitHub Desktop.
Save capnmidnight/07ef7298ef1012acf4c4c449aa552d0e to your computer and use it in GitHub Desktop.
// I don't know why I did this as a define instead of a const int.
#define NUM_FINGERS 5
// This is a configuration for the pins on which the motors are hooked up.
// I do most of my projects this way so I can move pins around easily and
// not have to worry about rewriting a lot of code
const int FINGER_PINS[NUM_FINGERS] = {4, 5, 6, 11, 12};
// Holds the byte read in from the serial port. I statically allocate everything
// because there is no reason not to on such a simple application. This way,
// there is nothing being allocated or deallocated on the stack every
// iteration of the reading loop.
byte pinState = 0x00;
// Holds a bit that selects a finger. We binary-AND the pinState and the
// mask to be able to tell if any one particular finger is ON or OFF.
byte mask;
// Just a loop counter.
int i;
// Holds the number of bytes we'll have to read off of the serial port
// every iteration of the loop.
int numBytes;
void setup() {
// Turn on all of the finger pins for digital output, no pull up
// or pull down resistors engaged (because the motors have enough
// resistance on their own).
for (i = 0; i < NUM_FINGERS; ++i) {
pinMode(FINGER_PINS[i], OUTPUT);
}
// The exact baud rate to run a serial port on is a black art.
// I don't know why I chose this particular value. It's vital
// that the reading program and the writing program have the same
// value. Higher values generally mean faster communication.
// Lower values mean generally mean more reliable communication.
// But this is particularly low. Usually, I do 115200.
Serial.begin(9600);
}
void loop() {
// How much do we need to read? This value will most likely be either
// 0 or 1, but maybe sometimes as high as 2, or even 3!
numBytes = Serial.available();
// Post-decrement here means that we read the value of numBytes before
// decrementing it. So the while loop tests the value before 1 is
// subtracted. If the value is 0, then the while loop does not run.
// If the value is more than 0, then the value decrements and the
// while loop runs.
while (numBytes--) {
// Just to be sure we know what we're dealing with and never have
// extraneous bytes, I mask off only the lower 5 bits of the byte.
pinState = Serial.read() & 0x1f;
// For each finger:
for (i = 0; i < NUM_FINGERS; ++i) {
// Build the mask. Take the binary pattern 000000001 and shift that
// 1 over `i` bits.
mask = 0x1 << i;
// Now, do the work. It's easiest to see if we stack them up:
// 0011 0011 0011
// & 0001 & 0010 & 0100 etc.
// ====== ====== ======
// 0001 0010 0000
//
// And in C, 0 is treated as FALSE and everything else is treated as TRUE.
// The language is actually designed that way specifically so we can do this.
digitalWrite(FINGER_PINS[i], (pinState & mask) ? HIGH : LOW);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment