From very annoying thing to slightly less annoying thing (and serial, and temperature sensors, and...)

You might recall the annoying thing I built with the Duemilanove before I left it in an unusable state. It was essentially the usual blinking example but with a buzzer connected instead of an LED.

But that wasn't very musical. So I investigated a bit further. First I connected this huge chunky potentiometer to the board and learnt how to read its values. My idea was to change the voltage of what we wrote to the pin, depending on what the potentiometer said. A cheap way to experiment with how would different values sound in the buzzer, without having to recompile each time—interactive inputs are always so much better!

Analog reads give values in a 0..1023 range, and analog writes can only be in the 0..255 range. So to scale it I divided it by 4.

Investigating a bit more I found the tone() function which is supposed to generate nicer sounding sounds, so I played a bit more replacing my previous analogWrite calls with calls to tone(). At that point I was a bit in the dark regarding what was going on between what I was hearing and the actual frequency that was sent to the buzzer, so I refreshed my memory about the Serial. functions. I was slightly amused at the lack of a printf function in the Arduino programming language, but it didn't take me much to realise that maybe bringing the entire C stdlib to the chip was a bit overkill, so I continued decomposing my print statements as if I had never heard about 'printf' before. It's fine.

if(abs(oldValue - value) >= tolerance) {
    Serial.print("old - "); Serial.println(oldValue);
    Serial.print("new - "); Serial.println(value);
    Serial.print("freq "); Serial.println(frequency);

There's no recording because I went down a black hole of playing with different serial port speeds and seeing the different effects this would cause: the slowest values were like accessing an early BBS where characters would slowly show up on your terminal. Mismatches between values were also funny and glitchy (i.e. where the initialised value on the Arduino would not match the expected value on the computer). Maybe I should make a recording after all—as I write this I would like to see this again.

Anyway, I wanted to learn more about making more sophisticated sounds with the Arduino (ah, not surprising). Perhaps I could use actual components to create curves, envelopes, etc, instead of computing them on the Arduino? But I don't really remember how this was supposed to work—the last time I studied this was like... I don't even remember when!

So I went via a different route, which was looking at the various components in the two hardware kits in my possession, and trying to a) identify what was each thing and b) what did each thing do and how to use it.

Some of the components are obvious, some are not. I still haven't figured out all of them! One was specially interesting as I thought it was a transistor, but turns out it was a temperature sensor, the TMP36.

Right now I don't have any thermometer in my place, and I'm intrigued as to what the indoors temperature is. This was a happy finding! Of course I set myself to use immediately. It's not hard to do it. You connect one pin of the sensor to GND (Ground), the other to +5V and read the analog value of the middle pin: quite like reading the potentiometer. The sensor range is already described on its datasheet so you know the function for calculating the temperature depending on the reading-you don't need to calibrate it. It's explained in this Adafruit tutorial, but the crux is this:

temperatureReading = analogRead(temperaturePin);
  voltage = temperatureReading * 5000.0 / 1024.0;
  tempCelsius = (voltage - 500) / 10;

Then I realised that individual readings don't really cut it. I wanted a history of readings! But I didn't know how to read values from the serial without the Arduino IDE and storing them to disk or "somewhere". And I was also getting mildly irritated with the Arduino IDE and its lack of autocompletion, etc, etc. Luckily, I remembered that I was using an Arduino Uno now so I could use it with Johnny Five, and write all this in JavaScript and use my favourite editor.

Lo and behold, I flashed StandardFirmatta on the board. It worked! Then I made a new folder, initialised the npm package and installed johnny-five and started with the simple Hello world. It worked, what next?

Analog readings! They worked too! I could read the sensor!

What next? Wait! There is a Thermometer object, I saw in the documentation. Wait, what? Well, turns out that Johnny Five already knows how to deal with the sensor I was using, because it's very popular. I can simply make an instance of the thermometer object, tell it which sensor I'm using, and it will emit events with the reading and I don't even need to care about converting voltage to degrees! Wow!

I ended up writing a very simple temperature logging script:

var fs = require('fs');
var moment = require('moment');
var five = require('johnny-five');
var board = new five.Board();
var outputFile = fs.openSync('out.txt', 'w');

board.on('ready', function() {
    var led = new five.Led(13);

    var thermometer = new five.Thermometer({
        controller: "TMP36",
        pin: "A0",
        freq: 60000

    thermometer.on('data', function() {
        var celsius = this.C;
        var timestamp = moment().format();
        var message = timestamp + ',' + celsius;
        fs.writeSync(outputFile, message + '\n');

This is 24 lines of code only and I think it's totally great. This is an incredible platform to experiment with!

I left it running overnight for the sake of curiosity, and went on to have dreams about hardware designs and components that all worked together nicely, which was a welcome change from the usual obsessive nightmares about faulty algorithms.

Next up? not sure if I want to keep learning more about the analog sound generation, or look more into efficient value logging—at the moment I'm writing a value per minute but often the previous value is the same as the previous and so on. Perhaps I just want to store deltas, or a new value if it's different from the old one. I don't know! Or maybe I'll want to do something else. We'll see.