Music making with neo trellis
Tags: hardware, electronics
Published: 2022-09-05 09:41:07

---
title: Music making with neo trellis
published: true
description: I used arduino neo trellis to play recorded tunes.
tags: hardware, electronics
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b1q5zxjdbnpz4pgc7q8n.jpg
# Use a ratio of 100:42 for best results.
---

I've been playing around with neo trellis for a while. I had to pack it up in a box and froget about it for a few months due to life changes and priorities, and then I came back to it. I lost the sketchy code I made for it months back, so I decided to start over and re-read the [documentation](https://learn.adafruit.com/adafruit-neotrellis/arduino-code) in Adafruit. I finally remembered everything and started working. First of all I already enjoy one of the examples from the [seesaw](https://www.arduino.cc/reference/en/libraries/adafruit-seesaw-library/) NeoTrellis library: "interrupt". 

![A 4x4 keyboard, a hand is pressing a button which lights in red](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v1sfhz01lw9i0slcg90j.jpg) 

![A 4x4 keyboard, a hand is pressing a button which lights in blue](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1vt69emiffe2afjphnsi.jpg)

Basically what it does is light a colorful rainbow on the keyboard and then when you press one of them it lights with the assigned color. After studying that code I decided a fast dirty way of hacking the keys was getting the code of them by printing them on serial monitor. The identification code of the keys was actually part of the funtion that creates and returns a color for each key. I basically picked that unique [code+color] data and programmed it to print it in the serial monitor whenever I pressed a key. That way I would open the serial monitor and press each key to get the code. Anyway since my NeoTrellis is a 4x4 matrix, I mapped the keys and made a conditional in a way that they would print an unique id letter in the monitor when they are pressed, instead of the weird code mentioned before. IN the end, the keys were set in this way:


| A | B | C | D |
|---|---|---|---|
| E | F | G | H |
| I | J | K | L |
| M | N | O | P |

The final result was that if I pressed the key in the left corner above it would print "A" in the serial (`tty/ACM0` in my case) monitor, if I pressed the key in the right bottom corner it would print "P" and so on. Once this was done, I changed to python. I used pyserial to read the `tty/ACM0` contents.

```
device='/dev/ttyACM0'
arduino = serial.Serial(device, 9600,timeout=1)

```

My original idea was playing simple "beep" sounds and similar. I searched for the notifications sounds in my system and used `mplayer` command to play the `wav` file of some sample sounds, such bells or guitars. 

```
     valor = arduino.readline()
     valor = valor.decode('utf-8')
     if "A" in valor:
        print("playing sample music")
        subprocess.run(["mplayer", "/<path_to_my_music>/a.wav"])

```

Since this worked I decided to go a litle further. I opened Audacity, took my mic and my ukelele. I started recording some basic chords and saving them in a music folder in the [project](https://sr.ht/~alienagain/neotrellis_music_player/) main folder. I also did some funny sounds with my mouth and recorded them. I changed the python code so it would read the path from that folder instead of the default system's. 

```
     valor = arduino.readline()
     valor = valor.decode('utf-8')
     if "A" in valor:
        print("playing ukelele chord F")
        subprocess.run(["mplayer", "/<new_path_to_my_music>/ukelele1.wav"])
     ...

```

I wrapped up this in a while true loop that only escaped with one of the key signals (in my case I used "M" since I decided to use the last row of keys as option buttons).

And that's it! now I can play my own recorded things on my computer with the neotrellis. This is just an example, though. Once I have this map in the serial, I can actually program them to open folders, open firefox tabs or whatever I need. I'm thinking of creating a "git management" helper. It has a few seconds of delay, though!

![inside of the device, some wires, the microcontroller and the pcb can be seen](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b1q5zxjdbnpz4pgc7q8n.jpg)

[Here's a short video](https://mastodon.green/@alien/108929861049298164) showing the recording and the neotrellis.

{% embed https://mastodon.green/@alien/108929861049298164 %}.