Circuitry: Nano, XBee and RSSI

Working on the circuitry for the inside of the Space Rocks. With lots of help from Sitraka Rakotoniaina, we managed to get the code to work using a Nano I/O shield to connect the XBee and the Nano microcontroller. With a little help from a soldering iron and a wire from the RSSI pin (pin 6). Now looking forward to experimenting with this on all 4 circuits.

Here’s the current code, which maps the RSSI signal (essentially the distance between each Space Rock) from the XBees to a value that can effect the Mozzi synthesiser within the code.

/*   ~ Simple Arduino - xBee Receiver sketch ~

  Read an PWM value from Arduino Transmitter to fade an LED
  The receiving message starts with '<' and closes with '>' symbol.
  
  Dev: Michalis Vasilakis // Date:2/3/2016 // Info: www.ardumotive.com // Licence: CC BY-NC-SA                    */

#include <MozziGuts.h>
#include <Oscil.h> // oscillator 
#include <tables/cos2048_int8.h> // table for Oscils to play
#include <AutoMap.h> // maps unpredictable inputs to a range
 
// desired carrier frequency max and min, for AutoMap
const int MIN_CARRIER_FREQ = 22;
const int MAX_CARRIER_FREQ = 440;

// desired intensity max and min, for AutoMap, note they're inverted for reverse dynamics
const int MIN_INTENSITY = 700;
const int MAX_INTENSITY = 10;

AutoMap kMapCarrierFreq(0,1023,MIN_CARRIER_FREQ,MAX_CARRIER_FREQ);
AutoMap kMapIntensity(0,1023,MIN_INTENSITY,MAX_INTENSITY);

const int KNOB_PIN = 0; // set the input for the knob to analog pin 0
const int LDR_PIN = 1; // set the input for the LDR to analog pin 1

Oscil<COS2048_NUM_CELLS, AUDIO_RATE> aCarrier(COS2048_DATA);
Oscil<COS2048_NUM_CELLS, AUDIO_RATE> aModulator(COS2048_DATA);

int mod_ratio = 3; // harmonics
long fm_intensity; // carries control info from updateControl() to updateAudio()

//Constants
const int ledPin = 3; //Led to Arduino pin 3 (PWM)
//Variables:
int ff ; //Value from pot
int pbPin = 7;

//Variables
bool started= false;//True: Message is strated
bool ended  = false;//True: Message is finished 
char incomingByte ; //Variable to store the incoming byte
char msg[3];    //Message - array from 0 to 2 (3 values - PWM - e.g. 240)
byte index;     //Index of array

int rssiDur = 0;
int rssiMapped =0;
bool calib = false;
bool calib_top = false;
int base_value= 0;
int top_value=0;

void setup() {
  //Start the serial communication
  Serial.begin(9600); //Baud rate must be the same as is on xBee module
  pinMode(ledPin, OUTPUT);
  pinMode(6, OUTPUT);
  calib = true;
  calib_top = true;
  startMozzi(); // :))
 pinMode(pbPin, INPUT_PULLUP);
}
void updateControl(){
  // read the knob
  //int knob_value = mozziAnalogRead(KNOB_PIN); // value is 0-1023
  rssiDur = pulseIn(5, LOW, 200);
  rssiMapped = map(rssiDur, 10, 40, 0, 1023);
  
  
  Serial.print("raw RSSI : ");
  Serial.println(rssiDur);
  Serial.print("RSSI mapped : ");
  Serial.println(rssiMapped);
  
  delay(100);
  
  // map the knob to carrier frequency
  int carrier_freq = kMapCarrierFreq(rssiMapped);
  
  //calculate the modulation frequency to stay in ratio
  int mod_freq = carrier_freq * mod_ratio;
  
  // set the FM oscillator frequencies to the calculated values
  aCarrier.setFreq(carrier_freq); 
  aModulator.setFreq(mod_freq);
  
  // read the light dependent resistor on the Analog input pin
  int light_level= mozziAnalogRead(LDR_PIN); // value is 0-1023

  fm_intensity = kMapIntensity(light_level);

}

 int updateAudio(){
  long modulation = fm_intensity * aModulator.next(); 
  return aCarrier.phMod(modulation); // phMod does the FM
}


void loop() {
  
  
  //delay(100);
 

  if(rssiDur != 0){
      digitalWrite(6, HIGH);
    }else{
      digitalWrite(6,LOW);
    }

    audioHook();

    //Serial.println(digitalRead(pbPin));
  //Read the analog value from pot and store it to "value" variable
   ff = digitalRead(pbPin);//analogRead(A0);

  //Map the analog value to pwm value
  //value = map (value, 0, 1023, 0, 255);
  //Send the message:
 Serial.print('<');  //Starting symbol
  Serial.print(ff);//Value from 0 to 255
  Serial.println('>');//Ending symbol
  
    
  /*while (Serial.available()>0){
    //Read the incoming byte
    incomingByte = Serial.read();
    //Start the message when the '<' symbol is received
    if(incomingByte == '<')
    {
     started = true;
     index = 0;
     msg[index] = '\0'; // Throw away any incomplete packet
   }
   //End the message when the '>' symbol is received
   else if(incomingByte == '>')
   {
     ended = true;
     break; // Done reading - exit from while loop!
   }
   //Read the message!
   else
   {
     if(index < 4) // Make sure there is room
     {
       msg[index] = incomingByte; // Add char to array
       index++;
       msg[index] = '\0'; // Add NULL to end
     }
   }
 }
 
 if(started && ended)
 {
   int value = atoi(msg);
   
   //analogWrite(ledPin, value);
   Serial.println(value); //Only for debugging
   if(calib_top){
   if(value == 0){
    top_value = rssiMapped;
    Serial.println(top_value);
    calib_top = false;
   }
   }
   
   index = 0;
   msg[index] = '\0';
   started = false;
   ended = false;
 }*/
}


Some Mozzi audio experiments

Using my own samples with Mozzi

Have finally cracked getting my own samples into Mozzi. The sound clips need to be VERY short (around 0.5 seconds) or they are too big for the Nano. I also tried mono to see if that saved space, but then there was no sound at all. I really raised the volume of this sample and the clipping isn’t too noticeable compared to the previously-encoded version , as it clips because of the processing anyway.

Here’s the code:

/*  Example of playing a sampled sound,
    using Mozzi sonification library.
  
    Demonstrates one-shot samples scheduled
    with EventDelay.
  
    Circuit: Audio output on digital pin 9 on a Uno or similar, or
    DAC/A14 on Teensy 3.1, or 
    check the README or http://sensorium.github.com/Mozzi/
  
    Mozzi help/discussion/announcements:
    https://groups.google.com/forum/#!forum/mozzi-users
  
    Tim Barrass 2012, CC by-nc-sa.
*/

#include <MozziGuts.h>
#include <Sample.h> // Sample template
#include <samples/alienwave.h>
#include <EventDelay.h>

#define CONTROL_RATE 64

// use: Sample <table_size, update_rate> SampleName (wavetable)
Sample <alienwave_NUM_CELLS, AUDIO_RATE> aSample(alienwave_DATA);

// for scheduling sample start
EventDelay kTriggerDelay;

void setup(){
  startMozzi(CONTROL_RATE);
  aSample.setFreq((float) alienwave_SAMPLERATE / (float) alienwave_NUM_CELLS); // play at the speed it was recorded
  kTriggerDelay.set(1500); // 1500 msec countdown, within resolution of CONTROL_RATE
}


void updateControl(){
  if(kTriggerDelay.ready()){
    aSample.start();
    kTriggerDelay.start();
  }
}


int updateAudio(){
  return (int) aSample.next();
}


void loop(){
  audioHook();
}

With thanks to Tim Barass and his very helpful forum for help getting this working.