Day 3: Getting and saving accelerometer data & Day 4: Arduino time
Tags: arduino, adafruit, maker
Published: 2020-07-15 23:29:59

*You are getting a double whammy this post, as Day 3 is quite short. In case you are wondering who Mick is, he's my other half and you can find him on [@micktwomey](https://twitter.com/micktwomey)* 😉

# Day 3 Getting and saving accelerometer data
Ok, today's task is to get value of accelerometer and save to filesystem on CPX. So I tried, and it bounced back

```
OSError: [Errno 30] Read-only filesystem
```

...hmm, ok-aay.

After a bit of searching I found the solution to write to the filesystem with CircuitPython from an Adafruit article. 🙌

##### References
* https://learn.adafruit.com/cpu-temperature-logging-with-circuit-python/writing-to-the-filesystem
* http://21stdigitalhome.blogspot.com/2018/03/circuitpython-program-for-writing.html

So I created the boot.py, restarted CPX, and yip, the csv was created when the programme started to run.

And as expected from reading the article, when I modified the code, it didn't let me save it.

Workaround is rename boot.py via the Python REPL because you can't edit boot.py. And that worked, I made a quick edit on the code (output 3 readings instead of 10) and saved it!

Renamed the boot file back to boot.py, and hit the reset button on CPX.

Checked the CSV file and it has 3 readings saved to it! Amazing!

Was showing Mick and suggested an alternative as this way may damage the filesystem, and it is pretty hacky. We tried to look at ways to transmit the data over USB serial... bar bringing a wire from one of the pins. Plus we tried switching off the terminal window which replaces the bitmap image on the TFT if we don't use a while loop, again, no easy way to not have the terminal replace the bitmap image.

🧐 I think we have hit the limit of what Circuit Python can do. So onwards to Arduino and see what fun things I can do with that.

# Day 4 Arduino time
Moved development onto my Mac from Raspberry Pi.

Step 1: Downloaded Arduino on my Mac.

Step 2: ...

Step 3: Profit! 😆

Note that the last time I used this was a number of years ago when I ran a wearable workshop and I was more or less following someone's lead.

Followed instructions from installing libraries right up to testing TFT Gizmo sample code.

##### ℹ️ https://learn.adafruit.com/adafruit-tft-gizmo/arduino-libraries-test

Tried to run example code, it failed saying it can't find SP1 library.

At the moment in time, Mick was just passing by and seeing how I got on, and he had more experience in Arduino. He asked if I installed everything. He thinks the board is missing, and suggested to look up setup for CPX for Arduino.

##### ℹ️ https://learn.adafruit.com/adafruit-circuit-playground-express/set-up-arduino-ide
Followed the instructions and ran the blink example. Still didn't work.

The board was still not found, checked the port and changed it, and the blink code works!

Tried the TFT Gizmo example and it didn't work. Re-checked libraries from docs several times, restarted CPX, and even commented out calls to functions to see what works and printing text to serial. Nothing written out to TFT. I do notice the TFT backlight coming on.

Dropped the TFT Gizmo UF2 onto CPX and the test does work.

# ☕️ BREAK ☕️

Still figuring out why the TFT library not working.

I also wanted to find out how to call the accelerometer via Arduino.

Mick popped up again to see how I was getting on, from his experience of using Arduino, he was asking if maybe when CPX was connecting was not ready yet, so looked up info about Serial on Arduino docs and found:

##### ℹ️ https://www.arduino.cc/reference/en/language/functions/communication/serial/ifserial

Added the while loop from the following snippet:

```
void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB
  }
}

void loop() {
  //proceed normally
}
```

And the calls in setup() seem to work now. So that's a good sign. Well, the printing to serial. I still haven't got the TFT libraries to work.

I was ignoring this part of the code:

```
// OPTION 1 (recommended) is to use the HARDWARE SPI pins, which are unique
// to each board and not reassignable.
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

// OPTION 2 lets you interface the display using ANY TWO or THREE PINS,
// tradeoff being that performance is not as fast as hardware SPI above.
//#define TFT_MOSI 29  // Data out
//#define TFT_SCLK 30  // Clock out
//Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
```

`Option 2` was recommended for Circuit Playground Classic, so I let it as is. Mick was also looking into it on his laptop and found that using Option 2 works (after we confirmed that our board is CPX). So I used the code from Option 2 and, it works. I don't mind if it's a little slower, I'm just pulling accelerometer data and displaying a generated image as the accelerometer numbers change. Let's pin generating image and displaying it on TFT for now.

So the tft library works at set up. Let's try, say, print X value from the accelerometer in the loop. It failed because of course you can't print a float as text. You have to convert it. And I found many answers following but less so on what library to include including the library to include:

```
dtostrf(FLOAT,WIDTH, PRECISION,BUFFER);
```

After a couple of false leads (no surprisingly), this was the library to include for `dtostrf` to work:

```
<avr/dtostrf.h>
```

And that worked. So I called it a night.