Arduino networked lamp test

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.

Space, rock and Arduino
Rock, space and Arduino

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:

https://mayorquinmachines.weebly.com/blog/arduino-project-arduino-networked-lamp
https://mayorquinmachines.weebly.com/blog/arduino-project-arduino-networked-lamp

And the two versions of it that I built:

Arduino networked lamp circuit v1
Arduino networked lamp circuit v1, with RGB LEDs
Arduino networked lamp circuit v2
Arduino networked lamp circuit v2, with one LED

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…

 

Research and inspiration 27/03/2018

Discovered the artist Amulets this week. He works with cassettes, players, tape loops and effects , creating woozy soundscapes and atmospheres from these simple sources. Of particular interest is the physical aspect of what he does, manipulating the sounds and machines in real time.

Also reading The Oxford Handbook of Interactive Audio, with a view to developing a more theoretical approach to the sound that will be part of my final installation.

Currently experimenting with combining simple tones to make chords / walls of sound, using this for reference: Frequencies for equal-tempered scale, A4 = 440 Hz

Panned version.

Mono version.

Also read this piece recently on FACT – The Sound of Fear, which mentioned the Ghost Tape Number 10, which was unpacked in this podcast a while ago. An interesting example of using sound to play on people’s cultural preconceptions.

During the Vietnam conflict, US troops played a soundtrack known as Ghost Tape Number 10 against the soldiers of the National Liberation Front. Used as part of Operation Wandering Soul, the unsettling tape collage tapped into Vietnamese beliefs that ancestors not buried in their homeland roam without rest in the afterlife. This spooky mix of voice, sound and music was intended to haunt Vietnamese soldiers and encourage them to abandon their cause.

Quite intrigued by this visualisation of sound in space via AR. Discovered this while reading about the Weird Type AR app.

I have also been investigating a few options for networking and interacting with the objects that will be the 3D models in the installation.

One is X BEE – apparently “the Digi XBee3 Series offers design freedom with easy-to-add functionality and flexible wireless connectivity.”

Another is the Google’s Project SOLI, a sensor which recognises hand gestures.

And finally, a MIDI controller ring, The Wave, as featured on TechCrunch.

Sadly the last two will not be available for a few months yet.

Updated Arduino random play MP3s

With excellent assistance from the Arduino forum, I now have a working version of the circuit to play a random MP3 file each time the distance sensor is triggered. It doesn’t play a few of the files when they are triggered, but this has been tested elsewhere and the code works, so I need to investigate the files themselves, it seems.

And here’s the code used.

#include <SoftwareSerial.h>

#define ARDUINO_RX 5  //should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX 6  //connect to RX of the module

#define trigPin 13  //for the distance module
#define echoPin 12

SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX);//init the serial protocol, tell to myserial wich pins are TX and RX

////////////////////////////////////////////////////////////////////////////////////
//all the commands needed in the datasheet(http://geekmatic.in.ua/pdf/Catalex_MP3_board.pdf)
uint8_t Send_buf[8] = {0} ;//The MP3 player undestands orders in a 8 int string
//0X7E FF 06 command 00 00 00 EF;(if command =01 next song order)
#define NEXT_SONG 0X01
#define PREV_SONG 0X02

#define CMD_PLAY_W_INDEX 0X03 //DATA IS REQUIRED (number of song)

#define VOLUME_UP_ONE 0X04
#define VOLUME_DOWN_ONE 0X05
#define CMD_SET_VOLUME 0X06//DATA IS REQUIRED (number of volume from 0 up to 30(0x1E))
#define SET_DAC 0X17
#define CMD_PLAY_WITHVOLUME 0X22 //data is needed  0x7E 06 22 00 xx yy EF;(xx volume)(yy number of song)

#define CMD_SEL_DEV 0X09 //SELECT STORAGE DEVICE, DATA IS REQUIRED
#define DEV_TF 0X02 //HELLO,IM THE DATA REQUIRED

#define SLEEP_MODE_START 0X0A
#define SLEEP_MODE_WAKEUP 0X0B

#define CMD_RESET 0X0C//CHIP RESET
#define CMD_PLAY 0X0D //RESUME PLAYBACK
#define CMD_PAUSE 0X0E //PLAYBACK IS PAUSED

#define CMD_PLAY_WITHFOLDER 0X0F//DATA IS NEEDED, 0x7E 06 0F 00 01 02 EF;(play the song with the directory \01\002xxxxxx.mp3

#define STOP_PLAY 0X16

#define PLAY_FOLDER 0X17// data is needed 0x7E 06 17 00 01 XX EF;(play the 01 folder)(value xx we dont care)

#define SET_CYCLEPLAY 0X19//data is needed 00 start; 01 close

#define SET_DAC 0X17//data is needed 00 start DAC OUTPUT;01 DAC no output
////////////////////////////////////////////////////////////////////////////////////

void setup() {
  Serial.begin(9600);  //Start our Serial coms for serial monitor in our pc
  mySerial.begin(9600);  //Start our Serial coms for THE MP3
  delay(500);  //Wait chip initialization is complete
  sendCommand(CMD_SEL_DEV, DEV_TF);  //select the TF card
  delay(200);  //wait for 200ms
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  randomSeed(analogRead(A0));
}

void loop() {
  if (measureDistance(trigPin, echoPin) < 10) {
    int randTrackNo;
    randTrackNo = random(1, 9);
    sendCommand(0x03, randTrackNo);
    Serial.print("Playing track:");
    Serial.println(randTrackNo);
    delay(1000);  //wait to avoid errors
  }
  delay(300);
}

void sendCommand(int8_t command, int16_t dat) {
  delay(20);
  Send_buf[0] = 0x7e; //starting byte
  Send_buf[1] = 0xff; //version
  Send_buf[2] = 0x06; //the number of bytes of the command without starting byte and ending byte
  Send_buf[3] = command; //
  Send_buf[4] = 0x00;  //0x00 = no feedback, 0x01 = feedback
  Send_buf[5] = (int8_t)(dat >> 8);  //datah
  Send_buf[6] = (int8_t)(dat);  //datal
  Send_buf[7] = 0xef;  //ending byte
  for (uint8_t i = 0; i < 8; i++) {
    mySerial.write(Send_buf[i]) ;  //send bit to serial mp3
    Serial.print(Send_buf[i], HEX);  //send bit to serial monitor in pc
    Serial.print(" ");
  }
  Serial.println();
}

long measureDistance(int trigger, int echo) {
  long duration, distance;

  digitalWrite(trigger, LOW);  //PULSE ___|---|___
  delayMicroseconds(2);
  digitalWrite(trigger, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger, LOW);

  duration = pulseIn(echo, HIGH);
  distance = (duration / 2) / 29.1;
  Serial.print("distance: ");
  Serial.println(distance);
  return distance;
}

Arduino adventures: triggering MP3 files with distance sensor

Finally got the circuit built to be able to read the distance sensor and the MP3 board, so now I can trigger a selected MP3 to play when something passes the sensor. What I built previously was overly-complicated and was most likely shorting out.

The videos show the sensor working from 1ocm distance, but this is easy to vary. They also show the sound looping if the object sensed stays in range, and also two separate pieces of audio being triggered. This was changed in the code before being uploaded. The code used is below.

Next to make it play a random one of the MP3 files each time the sensor is triggered!

//code rearranged by Javier Muñoz 10/11/2016 ask me at javimusama@hotmail.com
#include <SoftwareSerial.h>

#define ARDUINO_RX 5//should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX 6//connect to RX of the module

#define trigPin 13//for the distance module
#define echoPin 12


SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX);//init the serial protocol, tell to myserial wich pins are TX and RX

////////////////////////////////////////////////////////////////////////////////////
//all the commands needed in the datasheet(http://geekmatic.in.ua/pdf/Catalex_MP3_board.pdf)
static int8_t Send_buf[8] = {0} ;//The MP3 player undestands orders in a 8 int string
                                 //0X7E FF 06 command 00 00 00 EF;(if command =01 next song order) 
#define NEXT_SONG 0X01 
#define PREV_SONG 0X02 

#define CMD_PLAY_W_INDEX 0X03 //DATA IS REQUIRED (number of song)

#define VOLUME_UP_ONE 0X04
#define VOLUME_DOWN_ONE 0X05
#define CMD_SET_VOLUME 0X06//DATA IS REQUIRED (number of volume from 0 up to 30(0x1E))
#define SET_DAC 0X17
#define CMD_PLAY_WITHVOLUME 0X22 //data is needed  0x7E 06 22 00 xx yy EF;(xx volume)(yy number of song)

#define CMD_SEL_DEV 0X09 //SELECT STORAGE DEVICE, DATA IS REQUIRED
                #define DEV_TF 0X02 //HELLO,IM THE DATA REQUIRED
                
#define SLEEP_MODE_START 0X0A
#define SLEEP_MODE_WAKEUP 0X0B

#define CMD_RESET 0X0C//CHIP RESET
#define CMD_PLAY 0X0D //RESUME PLAYBACK
#define CMD_PAUSE 0X0E //PLAYBACK IS PAUSED

#define CMD_PLAY_WITHFOLDER 0X0F//DATA IS NEEDED, 0x7E 06 0F 00 01 02 EF;(play the song with the directory \01\002xxxxxx.mp3

#define STOP_PLAY 0X16

#define PLAY_FOLDER 0X17// data is needed 0x7E 06 17 00 01 XX EF;(play the 01 folder)(value xx we dont care)

#define SET_CYCLEPLAY 0X19//data is needed 00 start; 01 close

#define SET_DAC 0X17//data is needed 00 start DAC OUTPUT;01 DAC no output
////////////////////////////////////////////////////////////////////////////////////


void setup()
{
  Serial.begin(9600);//Start our Serial coms for serial monitor in our pc
mySerial.begin(9600);//Start our Serial coms for THE MP3
delay(500);//Wait chip initialization is complete
   sendCommand(CMD_SEL_DEV, DEV_TF);//select the TF card  
delay(200);//wait for 200ms
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

}

void loop()
{
  if(measureDistance(trigPin,echoPin)<50){
sendCommand(CMD_PLAY_WITHFOLDER, 0X0203);//play the third song of the second folder
delay(1000);//wait to avoid errors
}
delay(300);
}

void sendCommand(int8_t command, int16_t dat)
{
 delay(20);
 Send_buf[0] = 0x7e; //starting byte
 Send_buf[1] = 0xff; //version
 Send_buf[2] = 0x06; //the number of bytes of the command without starting byte and ending byte
 Send_buf[3] = command; //
 Send_buf[4] = 0x00;//0x00 = no feedback, 0x01 = feedback
 Send_buf[5] = (int8_t)(dat >> 8);//datah
 Send_buf[6] = (int8_t)(dat); //datal
 Send_buf[7] = 0xef; //ending byte
 for(uint8_t i=0; i<8; i++)//
 {
   mySerial.write(Send_buf[i]) ;//send bit to serial mp3
   Serial.print(Send_buf[i],HEX);//send bit to serial monitor in pc
 }
 Serial.println();
}

long measureDistance(int trigger,int echo){
   long duration, distance;
  
  digitalWrite(trigger, LOW);  //PULSE ___|---|___
  delayMicroseconds(2); 
  digitalWrite(trigger, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigger, LOW);
  
  duration = pulseIn(echo, HIGH);
  distance = (duration/2) / 29.1;
   Serial.println("distance:");
   Serial.println(distance);
  return distance;

}

I Am Sensor – Godzuino

A track created using the audio signals from Arduino, via four over-sized sensors: Twitch, Flexilla, Bütton and Anomomehelmet.

I Am Sensor artwork (Photo: Alexa Pollmann, typography & logo: Kat Hornstein)
I Am Sensor artwork (Photo: Alexa Pollmann, typography & logo: Kat Hornstein)