Julian Rogers Home HAL interface electronics

Under Construction

HAL interface software

// 10.3.20

// halcoms3

// adapted from Adafruit MusicMaker example


// include SPI, MP3 and SD libraries

#include <SPI.h>

#include <SD.h>

#include <Adafruit_VS1053.h>


// These are the pins used

#define VS1053_RESET   -1     // VS1053 reset pin (not used!)

#define VS1053_CS       6     // VS1053 chip select pin (output)

#define VS1053_DCS     10     // VS1053 Data/command select pin (output)

#define CARDCS          5     // Card chip select pin

#define VS1053_DREQ     9     // VS1053 Data request, ideally an Interrupt pin


byte t;


Adafruit_VS1053_FilePlayer musicPlayer =

  Adafruit_VS1053_FilePlayer(VS1053_RESET, VS1053_CS, VS1053_DCS, VS1053_DREQ, CARDCS);


void setup() {

  pinMode(12, OUTPUT);    // pin controls LED eye

 

  Serial.begin(9600);

  delay(500);

  

  musicPlayer.begin();

  SD.begin(CARDCS);


  // Set volume for left, right channels. lower numbers == louder volume!

  musicPlayer.setVolume(0,0);

  

  // If DREQ is on an interrupt pin we can do background audio playing


  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);  // DREQ int


}


//------------------------------------------------------------


void loop() {

  // file names of mp3 clips are /1.mp3, /2.mp3 etc.

  t = int(random(1,100));   // number > number of clips adding a random gap

  //wait for player to stop playing then select a random clip

  if(!musicPlayer.playingMusic){

    if(t==1){

    musicPlayer.startPlayingFile("/1.mp3");

    }

    if(t==2){

    musicPlayer.startPlayingFile("/2.mp3");

    }

    if(t==3){

    musicPlayer.startPlayingFile("/3.mp3");

    }

    if(t==4){

    musicPlayer.startPlayingFile("/4.mp3");

    }


    // plus similar conditionals for clips /5.mp3 to /45.mp3


    if(t==36){

    musicPlayer.startPlayingFile("/36.mp3");

    }

    if(t > 36){

      // cabin noise plays between clips

      musicPlayer.startPlayingFile("/noise.mp3");

    }    

  }

  //---------------------------------------------

  //brightness of led changes for extra "sentience"

  for(int i = 50; i <= 255; i++){

  analogWrite(12, i);

  delay(10);

  }

  for(int i = 255; i >= 50; i--){

    analogWrite(12, i);

    delay(10);

  }

  

}

//--------------------------------------------------


The software is adapted from one of the Adafruit examples to play mp3 clips using the Adafruit Musicmaker. The clips are played in interrupt mode which means that anything else can be going on in the main loop of the program and not be stopped while the clips play.

The program detects when the current clip finishes and then generates a random number which is used to select the next clip. There is a greater range of random numbers than there are clips so there is a random interval between clips. You could make the random numbers “more random” by using noise from an unused analogue pin to seed the random function as described in the Arduino documentation. When HAL’s voice is not playing, a clip of “cabin ambient sounds” plays continuously.

At present all that happens apart from playing the clips is that the red LED’s brightness waxes and wanes slightly to convey more “sentience”. However, sensors to detect a person in front of the interface, control over WiFi or input via serial on the usb could be easily incorporated (I think!)

Recording the clips

I’m sure this is ok from a copyright point of view for personal use only but don’t quote me!

Anyway, I recorded 2001 on my digital box whose brand shall be nameless and the recorded the clips by connecting the audio out on the box/tv with the mic/in on our old XP laptop running an audio recorder called Polderbits (now defunct owing to the sad death of the author of the software). It had the advantage of being able to save in mp3 format whereas Audacity had to make do with ogg. Now Audacity can export in mp3, that’s the best free software to use for this sort of job. When I got the raw audio ont my PC, I used Audacity to cut it up into bits for the HAL interface’s clips.