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…

 

Arduino research 17/05/2018

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:

LDR - LED circuit diagram
LDR – LED circuit diagram
LDR - LED circuit
LDR – LED 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.

Processed stories: 2. Made Little Idea

Second text created using Markov chain text generation. Again, the text is in the order it was generated, although some slight edits have been made to give the text a bit more flow and ‘sense’.

Made little idea where he was
connect the bothersome sounds of the Earth
only family
had aged in the Desert,
a location on,
announcing the lens,
much less of the horse,
scans the space of whom,
announcing around the music,
waiting like electricity,
and intense, despite simple,

was being much like those back home.

 

Reported quite signals.
As he turned the cottage
whomever was great,

but maybe we’re not quiet.

 

To be honest, they too had seen the nature
wandered
not only more lush
and was unable to make the journey
back to the camera

shows not only family.

 

Looking back
their artists tried
the object details

tenderly.

 

These people live
significant mountains,
much like spider life,
that’s existence

quite done with wandering after all.

 

Being from and surprises the edges.

 

Even as a small life here,
as he looks closely,
careful to ignore
the leaf presents itself,
at first, as he thinks, as she named Kathy,

in honour of the sounds of the object.

 

Families gathered
spirit beautiful,
announcing she is assigned the airport,

sparsely, tendency, finally around the screen.

 

These people live a small listen
to take the creative commune
complex understanding

much less of the object.

 

Under closely, which finally that.

 

A web of careful
to connect is assigned

the artist’s ranch.

 

When they all listen.

 

A kindred spoil the communicating
back down is taken in a row behind

the council would change us all.

 

Or at the flickering moon shots,
the camera shows Beachtown USA,
situated near others

the moon landings.

 

Taken.

 

A kindred spirit beautiful,
and screen searching
signed the dial
view of decided
that is the pictures from memory

and the small life deciphering the stars.

 

As the lens near the Golden Gate Bridge

come riding horses.

 

Some before.

 

A celebration and spirit
behind the sound of the base

the contact with wandering travel.

 

The lens near other
means camera when the moon.

They travel.

 

Hopeful to think
she thinks,

object is so near as macro.

 

The live a similar kind,
was a pretty good idea while they knew it
was concert
have signal

would do the pictures from the lens.

 

Looking at this
was taken in by the concert

have begun.

 

Sat on his

heard the same from.

As he once again see scans them.

 

However, he had it.
They create as them.
However, he looks closely, careful to ignore they made.
With safety being near othered

occasionally the Earth.

 

The lens,
as only that there to find the lens.
Looking at thinks,

she notices that finally.

 

A web of carefully during its travels across space

mission for survival.

 

Even as we can tell,
static, waiting to pass.

Before static, waiting the home rest.

 

The city that’s existening
back down,
announcing the moon landing wasn’t enough,
because life wandered
was his headquarters, tunes,
looking for signal would come

time has passed.

 

More stop outside of the creative commune
inhabitants, the lab; an hour.
She glances up momentarily at the violins,

much; it was, but they weren’t enough the map.

 

An experience
up momentarily at a secret astronaut
analysed and scattered
mounted on the was

some time to listen is.

 

Looking at some sound so much;
it was unable to make the create

a sort of us.

 

Hopefully measured veins branch through others
and he decided the collects
antique photos of they weren’t enough,

because life.

 

Maybe was unable to take in the moon.
The sounds they knew the parlour.

They played from memory and the edges.

 

An easier time.
Before will be a fanfare.
A kind, was off to ride
collects antique photograph shows
not so much like electricity,

and the bother means came naturally, tenderly.

 

The sound of the same from the subject.
Unfortunately, tendency to be honest, they trail.
Where he wasn’t enough
other life, general
leave this cue to ride towards home,

and sounding like spider legs.

 

An expedition is.
Out in the Sun
a little idea
where to follow the map and in had it.
The grainy black and white screen.
Another life.
She had food and was being much like the journey
back to the photos of the city

the rider closely.

 

Families gather place.
Gazing in
stopped, tuned, rode again.
It was near to reach it,

communicating against the stars.

 

Lens near other side of time
to connect the screen.
They knew it took some sound.
Occasionally too
had seen this horse,
she tunes,
once again see

scanning their artists travel.

 

Being and expedition is sentimentarily
at a newly acquired piece mounted.
They all smile into her colleagues.
She adjusts the home
side of the commune
and takes it to his her –
she notices sound made little,

he came named.

 

Many have since suggested the decided that reminds they made.
 Without the craft the newly-acquired family

 

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;

}