Finally have some code that changes the synths on the Space Rock sound modules, based on the distance between Rocks.


The blog
Arduino audio tests using the Mozzi library, a Nano board, a light dependent resistor and a potentiometer (volume control).
http://sensorium.github.io/Mozzi/learn/introductory-tutorial/
Working through this tutorial currently, trying to understand how Processing can be used to network an Arduino and power the colour of a lamp from words featured in an XML feed (in this case my blog feed – replacing the word ‘love’ with ‘space’ and the word ‘peace’ with ‘rock’). This generates the colour #3C4C2C.
And after adding this post to the feed…
Note the slight colour change.
This is the circuit I used, from this website. The LED is a 4 pin one, which can generate any combination of RGB colour as light:
And the two versions of it that I built:
Here is the code used in Processing:
//Arduino Code for the Arduino Networked Lamp - Processing
#define SENSOR 0
#define R_LED 9
#define G_LED 10
#define B_LED 11
#define BUTTON 12
int val =0; //variable to store the value coming from the sensor
int btn = LOW;
int old_btn = LOW;
int state = 0;
char buffer[7];
int pointer = 0;
byte inByte = 0;
byte r = 0;
byte g = 0;
byte b = 0;
void setup() {
Serial.begin(9600); //open up serial port
pinMode(BUTTON, INPUT);
}
void loop() {
val = analogRead(SENSOR);
Serial.println(val);
if (Serial.available() >0) {
//read incoming byte
inByte = Serial.read();
if (inByte == '#') {
while (pointer < 6) {
buffer[pointer] = Serial.read();
pointer++;
}
//now need to decode 3 numbers of colors stored as hex numbers into 3 bytes
r = hex2dec(buffer[1]) +hex2dec(buffer[0])*16;
g = hex2dec(buffer[3]) +hex2dec(buffer[2])*16;
b = hex2dec(buffer[5]) +hex2dec(buffer[4])*16;
pointer = 0; //reset pointer
}
}
btn = digitalRead(BUTTON);
//check if there was a transition
if ((btn == HIGH) && (old_btn == LOW)) {
state = 1-state;
}
old_btn = btn; //val is now old,lets store it
if (state == 1) {
analogWrite(R_LED, r);
analogWrite(G_LED, g);
analogWrite(B_LED, b);
}
else {
analogWrite(R_LED, 0);
analogWrite(G_LED, 0);
analogWrite(B_LED, 0);
}
delay(100);
}
int hex2dec(byte c) {
if (c >= '0' && c<= '9') {
return c- '0';
} else if (c >='A' && c <= 'F') {
return c - 'A' + 10;
}
}
And the code in Arduino:
//Arduino Code for the Arduino Networked Lamp - Arduino
#define SENSOR 0
#define R_LED 9
#define G_LED 10
#define B_LED 11
#define BUTTON 12
int val =0; //variable to store the value coming from the sensor
int btn = LOW;
int old_btn = LOW;
int state = 0;
char buffer[7];
int pointer = 0;
byte inByte = 0;
byte r = 0;
byte g = 0;
byte b = 0;
void setup() {
Serial.begin(9600); //open up serial port
pinMode(BUTTON, INPUT);
}
void loop() {
val = analogRead(SENSOR);
Serial.println(val);
if (Serial.available() >0) {
//read incoming byte
inByte = Serial.read();
if (inByte == '#') {
while (pointer < 6) {
buffer[pointer] = Serial.read();
pointer++;
}
//now need to decode 3 numbers of colors stored as hex numbers into 3 bytes
r = hex2dec(buffer[1]) +hex2dec(buffer[0])*16;
g = hex2dec(buffer[3]) +hex2dec(buffer[2])*16;
b = hex2dec(buffer[5]) +hex2dec(buffer[4])*16;
pointer = 0; //reset pointer
}
}
btn = digitalRead(BUTTON);
//check if there was a transition
if ((btn == HIGH) && (old_btn == LOW)) {
state = 1-state;
}
old_btn = btn; //val is now old,lets store it
if (state == 1) {
analogWrite(R_LED, r);
analogWrite(G_LED, g);
analogWrite(B_LED, b);
}
else {
analogWrite(R_LED, 0);
analogWrite(G_LED, 0);
analogWrite(B_LED, 0);
}
delay(100);
}
int hex2dec(byte c) {
if (c >= '0' && c<= '9') {
return c- '0';
} else if (c >='A' && c <= 'F') {
return c - 'A' + 10;
}
}
This didn’t work the first time I ran it, so I had to specify the Arduino port that Processing should use and then…
Today I’ve been looking at how to build the various Arduino circuits for the Space Rocks, and at some examples of relevant projects.
I made some basic sensor experiments with a Light-dependent resistor, ultimately using this code from the Make: Getting Started with Arduino book.
// Example 06b: Blink LED at a rate specified by the // value of the analogue input # define LED 9 // the pin for the LED int val = 0; // variable used to store the value // coming from the sensor void setup() { // put your setup code here, to run once: pinMode(LED, OUTPUT); // LED is as an output // note Analogue pins are // automatically set as inputs } void loop() { // put your main code here, to run repeatedly: val = analogRead(0); // read the value from the sensor analogWrite(LED, val/4); // turn the LED on at // the brightness set // by the sensor delay(10); // stop the program for some time }
And this circuit:
Also stumbled upon How to Build an Arduino synthesizer with Mozzi library
The Mozzi library looks super-useful for sound generation:
Currently your Arduino can only beep like a microwave oven. Mozzi brings your Arduino to life by allowing it to produce much more complex and interesting growls, sweeps and chorusing atmospherics. These sounds can be quickly and easily constructed from familiar synthesis units like oscillators, delays, filters and envelopes.
You can use Mozzi to generate algorithmic music for an installation or performance, or make interactive sonifications of sensors, on a small, modular and super cheap Arduino, without the need for additional shields, message passing or external synths.
Note to self to also check out the Mozzi examples gallery.
Have had lots of fun and games trying to get the Arduino element of the player to work, so far.
Firstly, getting the newly-purchased SD card to read was relatively straightforward. Once I worked out that I could transfer the files through my camera (as I don’t have an SD card reader other than the one I had just bought to attach to the Arduino), testing to see if it was connected and reading the files on there worked okay. Once I had reformatted the card for FAT32, at least. There is still an issue with some of the files having names too long for the DOS 8.3 naming convention, but I will rename these later.
Next, I bought the additional components (amplifier chip and capacitors) for, and built, this circuit:
Unfortunately, not realising there was a difference between the Arduino DUE board, which this circuit was designed for, and the Arduino (Elegoo) UNO I have, took me off on the wrong track somewhat and cost me a fair bit of time. Essentially, the DUE uses the Audio.h library, which needs the DAC port. A port my UNO sadly doesn’t have.
So, next, lots of Googling, trying to find a simpler circuit and code that uses the TMRpcm library instead. For a while, every bit of code I verified was giving me errors similar to this one (I tried a few!):
So then I decided to try the other computer, and the same lines of code all worked fine.
Next, to build the circuit. I tried to build this:
But the wiring around the capacitors and amp chip lost me completely.
Then I started working with this code – https://maxoffsky.com/maxoffsky-blog/how-to-play-wav-audio-files-with-arduino-uno-and-microsd-card/ – and finally managed to play various of the .wav files on the card
(although the audio was so distorted it was sometimes hard to tell!).
But how to make the circuit, so that it wasn’t just plugged straight into the Arduino board? After a couple of false starts, I found and built this:
This meant I now had very distorted, but louder audio from the speaker.
So…next steps..?
Watch this space.
The code and circuit for the Arduino-powered giant tilt switch was adapted from this page – http://starter-kit.nettigo.eu/2010/tilt-sensor/
/// Defining a pin for the tilt sensor
#define SENSOR_PIN 2
// Defining a pin for the LED diode
#define LED_PIN 13
// Defining a pin for the piezo
#define PIEZO_PIN 3
// Defining sensitivity time [ms]
#define SENSOR_TIME 100
// Defining alarm duration time [ms] (5000 ms = 5 s)
#define ALARM_TIME 10000
// variable storing sensor's switching time
unsigned long sensor_time;
// variable storing alarm duration
unsigned long alarm_time;
// variable storing pitch change time
unsigned long signal_time = 0;
// variable storing the pitch value
// 0 - no sound
// 1 - first tone
// > 1 - second tone
byte sound_tone = 0;
// alarm siren function
void alarm_signal()
{
if (sound_tone != 0) // checking if the siren should be turned on
{
if (millis() - signal_time > 750) // checking tone duration
{
signal_time = millis(); // storing the time when the tone was turned on
sound_tone = ~sound_tone; // turning on the second tone
}
if (sound_tone == 1) // first tone
{
tone(PIEZO_PIN, 300);
}
else // second tone
{
tone(PIEZO_PIN, 200, 100);
delay(1000);
tone(PIEZO_PIN, 400, 100);
delay(1000);
}
}
else
{
noTone(PIEZO_PIN); // Sound off
}
}
// Function invoked when the alarm is off
void alarm_on()
{
alarm_time = millis(); // storing the time when the tone was turned on
digitalWrite(LED_PIN, HIGH); // LED on
sound_tone = 1; // Turning alarm siren on
}
// Function invoked when the alarm is off
void alarm_off()
{
if (millis() - alarm_time > ALARM_TIME) // checking alarm duration
{
digitalWrite(LED_PIN, LOW); // LED off
sound_tone = 0; // Turning alarm siren off
}
}
// function checking sensor's state
void check_sensor()
{
// checking sensor's state
if (digitalRead(SENSOR_PIN) == HIGH)
{
// Action if off
if (sensor_time == 0) // checking if the state was changed
{
sensor_time = millis(); // storing sensor's activation time
}
// Condition, if sensor's state lasts for a specified time
else if (millis() - sensor_time > SENSOR_TIME)
{
alarm_on();
}
}
else
{
// Action if off
sensor_time = 0;
alarm_off();
}
}
void setup()
{
// Setting the sensor's pin as input
pinMode(SENSOR_PIN, INPUT);
// Turning internal Pull Up resistor on
digitalWrite(SENSOR_PIN, HIGH);
pinMode(LED_PIN, OUTPUT);
}
void loop()
{
check_sensor();
alarm_signal();
}
Some initial sketches for an over-sized tilt switch that will generate audio through the Arduino board when activated.
Arduino with heartbeat sensor – audio test