Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@BorisKourt
Created May 16, 2015 14:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BorisKourt/a13b3d6424aa4384ee36 to your computer and use it in GitHub Desktop.
Save BorisKourt/a13b3d6424aa4384ee36 to your computer and use it in GitHub Desktop.
Workshop arduino code.
const int axisCharOne = 1;
const int axisCharTwo = 2;
const int colourChar = 3;
const int scaleChar = 4;
String sanitizeSensor(long val) {
// Makes a simple 'data structure' to pass a single axis.
String ret, front, out;
long cur = val;
// Check if the value is negative or positive.
if (val >= 0) {
front = "1";
} else {
cur = abs(val);
front = "2";
}
// Cap value to three digits
if (cur < 10) {
out = String("00" + String(cur, DEC));
} else if (cur < 100) {
out = String("0" + String(cur, DEC));
} else if (cur > 999) {
out = String("999");
} else {
out = String(cur, DEC);
}
// Return a string that ends with 1 for positive or 2 for negative.
ret = String(front + out);
return ret;
}
void setScaleChar (long scale) {
setScratchWithOne(scaleChar, sanitizeSensor(scale));
}
void setColourChar (long grad) {
setScratchWithOne(colourChar, sanitizeSensor(grad));
}
void setAxisChar (long x, long y, long z) {
setScratchWithTwo(axisCharOne, sanitizeSensor(x), sanitizeSensor(y));
setScratchWithOne(axisCharTwo, sanitizeSensor(z));
}
void setScratchWithTwo (int scratchNumber, String one, String two) {
String outString = String(one + two);
long number = atol( outString.c_str() );
Bean.setScratchNumber( scratchNumber, number );
}
void setScratchWithOne (int scratchNumber, String one) {
String outString = String(one + "3333");
long number = atol( outString.c_str() );
Bean.setScratchNumber( scratchNumber, number );
}
void setup() {
// Sets X, Y, Z to 0
setAxisChar(0, 0, 0);
// Sets to highest value.
setColourChar(255);
// 10 pixel circle
setScaleChar(10);
}
void loop() {
bool connected = Bean.getConnectionState();
if ( connected ) {
Bean.setLed( 0, 20, 0 );
AccelerationReading accel = {0, 0, 0};
accel = Bean.getAcceleration();
long x = accel.xAxis;
long y = accel.yAxis;
long z = accel.zAxis;
setAxisChar(x, y, z);
} else {
Bean.setLed(0, 0, 0);
}
Bean.sleep(40);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment