A short video where I talk about serial debugging.
In my last post I talked generally about connecting the Gemma and the Flora Accelerometer. Here, I tried to put that into action.
Thank You, Becky Stern
A few weeks ago, Adafruit Wearables Pro Becky Stern posted this tutorial for serial debugging with the Gemma. I decided to try it out. But then I missed the bit about leaving off the “receive” cable and had to spend some time figuring out how to connect the accelerometer’s data line to the Gemma.
Assistance
I also had to call in a favor with my Hunky Assistant to get his help working with the software serial and figuring out how to get the code to do what I wanted it to do. In the video I mention connecting the hardware and then working with the code to get it to work but I was really working with both of them on and off at the same time.
// GEMMA serial debugging with Flora Accelerometer #include <SoftwareSerial.h> #include <TinyWireM.h> #define ACCEL_ADDR (0x32 >> 1) #define OUT_X_L_A 0x28 #define CTRL_REG1_A 0x20 #define OTHER_REG_COMING_UP 0x80 // Establish software serial on digital pin 1 SoftwareSerial mySerial = SoftwareSerial(0, 1); int sensorValue; // Current void init_accelerometer() { TinyWireM.beginTransmission(ACCEL_ADDR); TinyWireM.send(CTRL_REG1_A); TinyWireM.send(0x37); TinyWireM.endTransmission(); } void setup() { mySerial.begin(9600); TinyWireM.begin(); init_accelerometer(); } int16_t recv_2bytes() { uint16_t v; uint8_t l = TinyWireM.receive(); uint8_t h = TinyWireM.receive(); v = h; v <<= 8; v |= l; return v; } void get_accelerometer_values(int16_t *x, int16_t *y, int16_t *z) { TinyWireM.beginTransmission(ACCEL_ADDR); TinyWireM.send(OUT_X_L_A | 0x80); TinyWireM.endTransmission(); TinyWireM.requestFrom(ACCEL_ADDR, 6); *x = recv_2bytes(); *y = recv_2bytes(); *z = recv_2bytes(); } void loop() { delay(500); int16_t x,y,z; get_accelerometer_values(&x, &y, &z); mySerial.println(x); mySerial.println(y); mySerial.println(z); mySerial.println(""); }
Importance of Snacks
My big takeaway from this part of the process is to eat while working. I started getting listless on the home stretch due to some low blood sugar. My thought processes weren’t as snappy. Won’t make this mistake again.
It Worked!
In the end, we were able to read the three axis values created by the accelerometer. I'll whip up another post just about trying to come up with an algorithm to produce the LED behaviors this should trigger.