Finally back working on the Space Rocks prototype. A note to self regarding wiring of the new mini amps I am awaiting delivery of .
Also tried out the mini speakers I got a while ago, but are not loud enough.
*Just in case I don’t come back to this for a while again. Amazing how much you forget if you stop and start working on a project over a long period. Is all coming back to me now though.
The components for the second two Space Rocks needed to take up less space. In the case of Undarum, the amplifier was too large. Shown here is the circuit on a new PCB (version 3) using an Adafruit Mono 2.5W Class D Audio Amplifier (PAM8302), which has a much smaller footprint and height than the previous self-assembly amplifier kit I was using.
Adafruit Mono 2.5W Class D Audio Amplifier (PAM8302)Adafruit Mono 2.5W Class D Audio Amplifier (PAM8302), speaker and PCBAdafruit Mono 2.5W Class D Audio Amplifier (PAM8302), speaker and PCB in action
Here’s the current code for each Space Rock and the transmitter that they connect to. The code allows the synth programmed into each Rock to change based on the relative distance to the transmitter.
Vaporum (‘Small whale’) Uses the Control Echo Theremin (Mozzi) synth
// include all libraries#include<LoRaLib.h>#include<MozziGuts.h>#include<Oscil.h>#include<tables/sin2048_int8.h>#include<ControlDelay.h>#include<RollingAverage.h>// uncomment the following line for debug output// NOTE: debug output will slow down Arduino!//#define DEBUG#ifdefDEBUG#defineDEBUG_BEGIN(x)Serial.begin(x)#defineDEBUG_PRINT(x)Serial.print(x)#defineDEBUG_PRINT_DEC(x)Serial.print(x,DEC)#defineDEBUG_PRINT_HEX(x)Serial.print(x,HEX)#defineDEBUG_PRINTLN(x)Serial.println(x)#defineDEBUG_PRINT_STR(x)Serial.print(F(x))#defineDEBUG_PRINTLN_STR(x)Serial.println(F(x))#else#defineDEBUG_BEGIN(x)#defineDEBUG_PRINT(x)#defineDEBUG_PRINT_DEC(x)#defineDEBUG_PRINT_HEX(x)#defineDEBUG_PRINTLN(x)#defineDEBUG_PRINT_STR(x)#defineDEBUG_PRINTLN_STR(x)#endif// Mozzi control rate in Hz#defineCONTROL_RATE512// RSSI range bounds#defineRSSI_LOW-60#defineRSSI_HIGH-30// freqency range bounds in Hz#defineFREQ_LOW0#defineFREQ_HIGH1000// SX1278 object instanceSX1278lora=newLoRa;// rolling average filter RollingAverage<float,32>kAverage;// theremin effect echo cellsunsignedintecho_cells_1=32;unsignedintecho_cells_2=60;unsignedintecho_cells_3=127;// 2 second delayControlDelay<128,int>kDelay;// oscils to compare bumpy to averaged control inputOscil<SIN2048_NUM_CELLS,AUDIO_RATE>aSin0(SIN2048_DATA);Oscil<SIN2048_NUM_CELLS,AUDIO_RATE>aSin1(SIN2048_DATA);Oscil<SIN2048_NUM_CELLS,AUDIO_RATE>aSin2(SIN2048_DATA);Oscil<SIN2048_NUM_CELLS,AUDIO_RATE>aSin3(SIN2048_DATA);voidsetup(){// start Serial port for debuggingDEBUG_BEGIN(115200);// initialize SX1278intstate=lora.begin();DEBUG_PRINTLN(state);// set LoRa bandwidthstate=lora.setBandwidth(250);DEBUG_PRINTLN(state);// set interrupt actionlora.setDio0Action(setFlag);// start listening for LoRa transmissionslora.startReceive();// start MozzistartMozzi(CONTROL_RATE);// set echo cellskDelay.set(echo_cells_1);}// flag to indicate that a packet was receivedvolatileboolreceivedFlag=false;// disable interrupt when it's not neededvolatileboolenableInterrupt=true;voidsetFlag(void){// check if the interrupt is enabledif(!enableInterrupt){return;}// we got a packet, set the flagreceivedFlag=true;}voidupdateControl(){// check if packet was receivedif(receivedFlag){// disable the interrupt service routine while processing the dataenableInterrupt=false;// reset flagreceivedFlag=false;// read packet and update RSSI readingStringstr;lora.readData(str);// get raw RSSI valueintrssiRaw=lora.lastPacketRSSI;// map raw RSSI value to frequency rangeintrssiMapped=map(rssiRaw,RSSI_LOW,RSSI_HIGH,FREQ_LOW,FREQ_HIGH);// filter frequency valuefloatfreqFiltered=kAverage.next(rssiMapped);// print RSSI values for debuggingDEBUG_PRINT(rssiRaw);DEBUG_PRINT('\t');DEBUG_PRINT(rssiMapped);DEBUG_PRINT('\t');DEBUG_PRINTLN(freqFiltered);// set new frequencyaSin0.setFreq(freqFiltered);aSin1.setFreq(kDelay.next(freqFiltered));aSin2.setFreq(kDelay.read(echo_cells_2));aSin3.setFreq(kDelay.read(echo_cells_3));// enable interrupt againenableInterrupt=true;}}intupdateAudio(){return3*((int)aSin0.next()+aSin1.next()+(aSin2.next()>>1)+(aSin3.next()>>2))>>3;}voidloop(){audioHook();}
Cognitum (‘Razor clam’) Uses the Detuned Beats Wash (Mozzi) synth
// include all libraries#include<LoRaLib.h>#include<MozziGuts.h>#include<Oscil.h>#include<tables/cos8192_int8.h>#include<mozzi_rand.h>#include<mozzi_midi.h>#include<mozzi_fixmath.h>#include<RollingAverage.h>// uncomment the following line for debug output// NOTE: debug output will slow down Arduino!//#define DEBUG#ifdefDEBUG#defineDEBUG_BEGIN(x)Serial.begin(x)#defineDEBUG_PRINT(x)Serial.print(x)#defineDEBUG_PRINT_DEC(x)Serial.print(x,DEC)#defineDEBUG_PRINT_HEX(x)Serial.print(x,HEX)#defineDEBUG_PRINTLN(x)Serial.println(x)#defineDEBUG_PRINT_STR(x)Serial.print(F(x))#defineDEBUG_PRINTLN_STR(x)Serial.println(F(x))#else#defineDEBUG_BEGIN(x)#defineDEBUG_PRINT(x)#defineDEBUG_PRINT_DEC(x)#defineDEBUG_PRINT_HEX(x)#defineDEBUG_PRINTLN(x)#defineDEBUG_PRINT_STR(x)#defineDEBUG_PRINTLN_STR(x)#endif// Mozzi control rate in Hz#defineCONTROL_RATE512// RSSI range bounds#defineRSSI_LOW-60#defineRSSI_HIGH-30// freqency range bounds in Hz#defineFREQ_LOW0#defineFREQ_HIGH1000// SX1278 object instanceSX1278lora=newLoRa;// rolling average filter RollingAverage<float,32>kAverage;// harmonicsOscil<COS8192_NUM_CELLS,AUDIO_RATE>aCos1(COS8192_DATA);Oscil<COS8192_NUM_CELLS,AUDIO_RATE>aCos2(COS8192_DATA);Oscil<COS8192_NUM_CELLS,AUDIO_RATE>aCos3(COS8192_DATA);Oscil<COS8192_NUM_CELLS,AUDIO_RATE>aCos4(COS8192_DATA);Oscil<COS8192_NUM_CELLS,AUDIO_RATE>aCos5(COS8192_DATA);Oscil<COS8192_NUM_CELLS,AUDIO_RATE>aCos6(COS8192_DATA);Oscil<COS8192_NUM_CELLS,AUDIO_RATE>aCos7(COS8192_DATA);// duplicates but slightly off frequency for adding to originalsOscil<COS8192_NUM_CELLS,AUDIO_RATE>aCos1b(COS8192_DATA);Oscil<COS8192_NUM_CELLS,AUDIO_RATE>aCos2b(COS8192_DATA);Oscil<COS8192_NUM_CELLS,AUDIO_RATE>aCos3b(COS8192_DATA);Oscil<COS8192_NUM_CELLS,AUDIO_RATE>aCos4b(COS8192_DATA);Oscil<COS8192_NUM_CELLS,AUDIO_RATE>aCos5b(COS8192_DATA);Oscil<COS8192_NUM_CELLS,AUDIO_RATE>aCos6b(COS8192_DATA);Oscil<COS8192_NUM_CELLS,AUDIO_RATE>aCos7b(COS8192_DATA);// base pitch frequencies in 24n8 fixed int format (for speed later)Q16n16f1,f2,f3,f4,f5,f6,f7;Q16n16variation(){// 32 random bits & with 524287 (b111 1111 1111 1111 1111)// gives between 0-8 in Q16n16 formatreturn(Q16n16)(xorshift96()&524287UL);}voidsetup(){// start Serial port for debuggingDEBUG_BEGIN(115200);// initialize SX1278intstate=lora.begin();DEBUG_PRINTLN(state);// set LoRa bandwidthstate=lora.setBandwidth(250);DEBUG_PRINTLN(state);// set interrupt actionlora.setDio0Action(setFlag);// start listening for LoRa transmissionslora.startReceive();// start MozzistartMozzi(CONTROL_RATE);// select base frequencies using mtof (midi to freq) and fixed-point numbersf1=Q16n16_mtof(Q16n0_to_Q16n16(48));f2=Q16n16_mtof(Q16n0_to_Q16n16(74));f3=Q16n16_mtof(Q16n0_to_Q16n16(64));f4=Q16n16_mtof(Q16n0_to_Q16n16(77));f5=Q16n16_mtof(Q16n0_to_Q16n16(67));f6=Q16n16_mtof(Q16n0_to_Q16n16(57));f7=Q16n16_mtof(Q16n0_to_Q16n16(60));// set Oscils with chosen frequenciesaCos1.setFreq_Q16n16(f1);aCos2.setFreq_Q16n16(f2);aCos3.setFreq_Q16n16(f3);aCos4.setFreq_Q16n16(f4);aCos5.setFreq_Q16n16(f5);aCos6.setFreq_Q16n16(f6);aCos7.setFreq_Q16n16(f7);// set frequencies of duplicate oscillatorsaCos1b.setFreq_Q16n16(f1+variation());aCos2b.setFreq_Q16n16(f2+variation());aCos3b.setFreq_Q16n16(f3+variation());aCos4b.setFreq_Q16n16(f4+variation());aCos5b.setFreq_Q16n16(f5+variation());aCos6b.setFreq_Q16n16(f6+variation());aCos7b.setFreq_Q16n16(f7+variation());}// flag to indicate that a packet was receivedvolatileboolreceivedFlag=false;// disable interrupt when it's not neededvolatileboolenableInterrupt=true;voidsetFlag(void){// check if the interrupt is enabledif(!enableInterrupt){return;}// we got a packet, set the flagreceivedFlag=true;}voidupdateControl(){// check if packet was receivedif(receivedFlag){// disable the interrupt service routine while processing the dataenableInterrupt=false;// reset flagreceivedFlag=false;// read packet and update RSSI readingStringstr;lora.readData(str);// get raw RSSI valueintrssiRaw=lora.lastPacketRSSI;// map raw RSSI value to frequency rangeintrssiMapped=map(rssiRaw,RSSI_LOW,RSSI_HIGH,FREQ_LOW,FREQ_HIGH);// filter frequency valuefloatfreqFiltered=kAverage.next(rssiMapped);// print RSSI values for debuggingDEBUG_PRINT(rssiRaw);DEBUG_PRINT('\t');DEBUG_PRINT(rssiMapped);DEBUG_PRINT('\t');DEBUG_PRINTLN(freqFiltered);// change frequencies of the b oscillatorsswitch(lowByte(xorshift96())&7){case0:aCos1b.setFreq_Q16n16(f1+float_to_Q16n16(freqFiltered)+variation());break;case1:aCos2b.setFreq_Q16n16(f2+float_to_Q16n16(freqFiltered)+variation());break;case2:aCos3b.setFreq_Q16n16(f3+float_to_Q16n16(freqFiltered)+variation());break;case3:aCos4b.setFreq_Q16n16(f4+float_to_Q16n16(freqFiltered)+variation());break;case4:aCos5b.setFreq_Q16n16(f5+float_to_Q16n16(freqFiltered)+variation());break;case5:aCos6b.setFreq_Q16n16(f6+float_to_Q16n16(freqFiltered)+variation());break;case6:aCos7b.setFreq_Q16n16(f7+float_to_Q16n16(freqFiltered)+variation());break;}// enable interrupt againenableInterrupt=true;}}intupdateAudio(){intasig=aCos1.next()+aCos1b.next()+aCos2.next()+aCos2b.next()+aCos3.next()+aCos3b.next()+aCos4.next()+aCos4b.next()+aCos5.next()+aCos5b.next()+aCos6.next()+aCos6b.next()+aCos7.next()+aCos7b.next();returnasig>>3;}voidloop(){audioHook();}
Undarum (‘Wide shell’)
Uses the Knob Light (Mozzi) synth
// include all libraries#include<LoRaLib.h>#include<MozziGuts.h>#include<Oscil.h>#include<tables/cos2048_int8.h>#include<RollingAverage.h>// uncomment the following line for debug output// NOTE: debug output will slow down Arduino!//#define DEBUG#ifdefDEBUG#defineDEBUG_BEGIN(x)Serial.begin(x)#defineDEBUG_PRINT(x)Serial.print(x)#defineDEBUG_PRINT_DEC(x)Serial.print(x,DEC)#defineDEBUG_PRINT_HEX(x)Serial.print(x,HEX)#defineDEBUG_PRINTLN(x)Serial.println(x)#defineDEBUG_PRINT_STR(x)Serial.print(F(x))#defineDEBUG_PRINTLN_STR(x)Serial.println(F(x))#else#defineDEBUG_BEGIN(x)#defineDEBUG_PRINT(x)#defineDEBUG_PRINT_DEC(x)#defineDEBUG_PRINT_HEX(x)#defineDEBUG_PRINTLN(x)#defineDEBUG_PRINT_STR(x)#defineDEBUG_PRINTLN_STR(x)#endif// Mozzi control rate in Hz#defineCONTROL_RATE512// RSSI range bounds#defineRSSI_LOW-60#defineRSSI_HIGH-30// freqency range bounds in Hz#defineFREQ_LOW400#defineFREQ_HIGH800// SX1278 object instanceSX1278lora=newLoRa;// rolling average filter RollingAverage<float,32>kAverage;// Mozzi oscillatorsOscil<COS2048_NUM_CELLS,AUDIO_RATE>aCarrier(COS2048_DATA);Oscil<COS2048_NUM_CELLS,AUDIO_RATE>aModulator(COS2048_DATA);// harmonicsintmod_ratio=3;// carries control info from updateControl() to updateAudio()longfm_intensity;voidsetup(){// start Serial port for debuggingDEBUG_BEGIN(115200);// initialize SX1278intstate=lora.begin();DEBUG_PRINTLN(state);// set LoRa bandwidthstate=lora.setBandwidth(250);DEBUG_PRINTLN(state);// set interrupt actionlora.setDio0Action(setFlag);// start listening for LoRa transmissionslora.startReceive();// start MozzistartMozzi(CONTROL_RATE);}// flag to indicate that a packet was receivedvolatileboolreceivedFlag=false;// disable interrupt when it's not neededvolatileboolenableInterrupt=true;voidsetFlag(void){// check if the interrupt is enabledif(!enableInterrupt){return;}// we got a packet, set the flagreceivedFlag=true;}voidupdateControl(){// check if packet was receivedif(receivedFlag){// disable the interrupt service routine while processing the dataenableInterrupt=false;// reset flagreceivedFlag=false;// read packet and update RSSI readingStringstr;lora.readData(str);// get raw RSSI valueintrssiRaw=lora.lastPacketRSSI;// map raw RSSI value to frequency rangeintrssiMapped=map(rssiRaw,RSSI_LOW,RSSI_HIGH,FREQ_LOW,FREQ_HIGH);// filter frequency valuefloatfreqFiltered=kAverage.next(rssiMapped);// print RSSI values for debuggingDEBUG_PRINT(rssiRaw);DEBUG_PRINT('\t');DEBUG_PRINT(rssiMapped);DEBUG_PRINT('\t');DEBUG_PRINTLN(freqFiltered);// set new frequencyaCarrier.setFreq(freqFiltered);aModulator.setFreq(freqFiltered*mod_ratio);fm_intensity=freqFiltered;// enable interrupt againenableInterrupt=true;}}intupdateAudio(){longmodulation=fm_intensity*aModulator.next();returnaCarrier.phMod(modulation);}voidloop(){audioHook();}
Marginis (‘Shape 4’)
Uses the Sine Wave Pulse (Mozzi) synth
// include all libraries#include<LoRaLib.h>#include<MozziGuts.h>#include<Oscil.h>#include<tables/sin2048_int8.h>#include<Smooth.h>#include<RollingAverage.h>// uncomment the following line for debug output// NOTE: debug output will slow down Arduino!//#define DEBUG#ifdefDEBUG#defineDEBUG_BEGIN(x)Serial.begin(x)#defineDEBUG_PRINT(x)Serial.print(x)#defineDEBUG_PRINT_DEC(x)Serial.print(x,DEC)#defineDEBUG_PRINT_HEX(x)Serial.print(x,HEX)#defineDEBUG_PRINTLN(x)Serial.println(x)#defineDEBUG_PRINT_STR(x)Serial.print(F(x))#defineDEBUG_PRINTLN_STR(x)Serial.println(F(x))#else#defineDEBUG_BEGIN(x)#defineDEBUG_PRINT(x)#defineDEBUG_PRINT_DEC(x)#defineDEBUG_PRINT_HEX(x)#defineDEBUG_PRINTLN(x)#defineDEBUG_PRINT_STR(x)#defineDEBUG_PRINTLN_STR(x)#endif// Mozzi control rate in Hz#defineCONTROL_RATE512// RSSI range bounds#defineRSSI_LOW-60#defineRSSI_HIGH-30// freqency range bounds in Hz#defineFREQ_LOW400#defineFREQ_HIGH800// SX1278 object instanceSX1278lora=newLoRa;// rolling average filter RollingAverage<float,32>kAverage;// Mozzi sine oscillatorOscil<SIN2048_NUM_CELLS,AUDIO_RATE>aSin(SIN2048_DATA);// target gain for smoothing, toggle between 255/0bytegain=0;// Smoothing filter, higher number = smoother outputSmooth<long>aSmoothGain(0.9995);voidsetup(){// start Serial port for debuggingDEBUG_BEGIN(115200);// initialize SX1278intstate=lora.begin();DEBUG_PRINTLN(state);// set LoRa bandwidthstate=lora.setBandwidth(250);DEBUG_PRINTLN(state);// set interrupt actionlora.setDio0Action(setFlag);// start listening for LoRa transmissionslora.startReceive();// start MozzistartMozzi(CONTROL_RATE);// set output frequency to the lowest valueaSin.setFreq(FREQ_LOW);}// flag to indicate that a packet was receivedvolatileboolreceivedFlag=false;// disable interrupt when it's not neededvolatileboolenableInterrupt=true;voidsetFlag(void){// check if the interrupt is enabledif(!enableInterrupt){return;}// we got a packet, set the flagreceivedFlag=true;}// variable to save starting timestampunsignedlongstart=0;// delay length in microsecondsunsignedlongdelayMicros=FREQ_LOW*1000;voidupdateControl(){// check if packet was receivedif(receivedFlag){// disable the interrupt service routine while processing the dataenableInterrupt=false;// reset flagreceivedFlag=false;// read packet and update RSSI readingStringstr;lora.readData(str);// get raw RSSI valueintrssiRaw=lora.lastPacketRSSI;// map raw RSSI value to frequency rangeintrssiMapped=map(rssiRaw,RSSI_LOW,RSSI_HIGH,FREQ_LOW,FREQ_HIGH);// filter frequency valuefloatfreqFiltered=kAverage.next(rssiMapped);// print RSSI values for debuggingDEBUG_PRINT(rssiRaw);DEBUG_PRINT('\t');DEBUG_PRINT(rssiMapped);DEBUG_PRINT('\t');DEBUG_PRINTLN(freqFiltered);// set new frequencyaSin.setFreq(freqFiltered);// set new delay lengthdelayMicros=(FREQ_HIGH-freqFiltered)*1000;// check if it's time to pulseif(mozziMicros()-start>=delayMicros){// save current timestart=mozziMicros();// flip 0/255gain=255-gain;}// enable interrupt againenableInterrupt=true;}}intupdateAudio(){return(aSmoothGain.next(gain)*aSin.next())>>8;}voidloop(){audioHook();}
And the code for the transmitter, which links the four Space Rocks together.
// include library#include<LoRaLib.h>// SX1278 object instanceSX1278lora=newLoRa;voidsetup(){// start Serial port for debuggingSerial.begin(115200);// initialize SX1278Serial.println(lora.begin());// set LoRa bandwidthSerial.println(lora.setBandwidth(250));// set output power to limit jammingSerial.println(lora.setOutputPower(5));}voidloop(){// transmit packets as fast as we canlora.transmit("WeAreHere");}
So…it seems that the combination of XBee RSSI data and the Mozzi synth can only produce intermittent or pulsing sound and doesn’t allow for constant tones, as witnessed by the following video demos. All the data that has to be sent from Arduino to the XBee in order to get it to send and receive packets is causing the audio to stutter.
To get the tones required for the final piece, the circuits will need to be created using SX1278 LoRa modules instead. These will connect the four Space Rocks via an additional transmitter, and based around this circuit:
This should guarantee that the Mozzi synths can play continuous tones rather than interrupted pulses. Although I’ll now need to find a way to fit 4+” 433MHz antennaes into the Space Rocks too.
Thinking about the network that connects the Space Rocks. Initially I started with the idea of all four rocks connecting equally, which isn’t really possible.
Electric plinths – equal connections
The network will be connected as a star formation, as shown here:
The co-ordinator would be loaded with one Mozzi synth, and the same synth is installed on the three nodes, with each node changing a different filter (attack, decay and sustain). The code for this looks like so:
Co-ordinator:
// include all libraries#include<XBee.h>#include<SoftwareSerial.h>#include<MozziGuts.h>#include<Oscil.h>#include<EventDelay.h>#include<ADSR.h>#include<tables/sin8192_int8.h>#include<mozzi_rand.h>#include<mozzi_midi.h>// uncomment the following line to enable debug output#defineDEBUG// debug macros#ifdefDEBUG#defineDEBUG_BEGIN(x)Serial.begin(x)#defineDEBUG_PRINT(x)Serial.print(x)#defineDEBUG_PRINTLN(x)Serial.println(x)#else#defineDEBUG_BEGIN(x)#defineDEBUG_PRINT(x)#defineDEBUG_PRINTLN(x)#endif// Mozzi control rate in Hz#defineCONTROL_RATE128Oscil<8192,AUDIO_RATE>aOscil(SIN8192_DATA);// for triggering the envelopeEventDelaynoteDelay;ADSR<CONTROL_RATE,CONTROL_RATE>envelope;booleannote_is_on=true;bytegain;// XBee packet reception timeout in ms#defineXBEE_PACKET_TIMEOUT250// RSSI reading timeout in us#defineRSSI_READING_TIMEOUT100// Arduino pin connections#defineRSSI_PIN5#defineXBEE_DOUT6#defineXBEE_DIN7#defineAUDIO_PIN9// freqency range bounds in Hz#defineATTACK_LOW0#defineATTACK_HIGH1000#defineDECAY_LOW0#defineDECAY_HIGH1000#defineSUSTAIN_LOW0#defineSUSTAIN_HIGH1000// RSSI range boundsintRSSI_LOW=25;intRSSI_HIGH=45;intRSSI_LOW_2=25;intRSSI_HIGH_2=45;intRSSI_LOW_3=25;intRSSI_HIGH_3=45;// number of samples for moving average filter#defineSAMPLES10// XBee object instanceXBeexbee=XBee();// address of destination XBee (node address)XBeeAddress64addr64=XBeeAddress64(0x0013a200,0x40A58A5D);// packet to be sent to coordinatoruint8_tping[]={0xAA,0xAA};ZBTxRequestpingRequest=ZBTxRequest(addr64,ping,sizeof(ping));XBeeResponseresponse=XBeeResponse();// create reusable response objects for responses we expect to handleZBRxResponserx=ZBRxResponse();// SoftwareSerial portSoftwareSerialbeeSerial(XBEE_DOUT,XBEE_DIN);// Mozzi sine oscillator//Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SIN2048_DATA);// array to hold RSSI samples for filterfloatrssiSamples[SAMPLES];// current position in arrayintpos=0;floatrssiAttack=0;floatrssiDecay=0;floatrssiSustain=0;// moving average filterfloatmovingAverage(float*arr,intnewValue,intpos){// update arrayarr[pos]=newValue;// get array totalfloattotal=0;for(inti=0;i<SAMPLES;i++){total+=arr[i];}// get averagefloataverage=total/(float)SAMPLES;return(average);}voidsetup(){randSeed();// fresh randomnoteDelay.set(2000);// begin Serial communication with PCDEBUG_BEGIN(9600);// set pin modespinMode(RSSI_PIN,INPUT);pinMode(AUDIO_PIN,OUTPUT);// initialize array contents to 0for(inti=0;i<SAMPLES;i++){rssiSamples[i]=0;}// begin Serial communication with XBeebeeSerial.begin(9600);// set XBee serial portxbee.setSerial(beeSerial);// start MozzistartMozzi(CONTROL_RATE);// set output frequency to the lowest value//aSin.setFreq(FREQ_LOW);}unsignedintduration,attack,decay,sustain,release_ms;voidupdateControl(){// send packet to nodexbee.send(pingRequest);// try to receive pong packetif(xbee.readPacket(XBEE_PACKET_TIMEOUT)){if(xbee.getResponse().isAvailable()){if(xbee.getResponse().getApiId()==16){xbee.getResponse().getZBRxResponse(rx);uint16_tsender=rx.getRemoteAddress16();//DEBUG_PRINTLN(sender);if(sender==18431){// try to read raw RSSI valueintrssiRaw=pulseIn(RSSI_PIN,HIGH,RSSI_READING_TIMEOUT);// check if the read was successfulif(rssiRaw>0){if(rssiRaw<=RSSI_LOW){RSSI_LOW=rssiRaw;}if(rssiRaw>=RSSI_HIGH){RSSI_HIGH=rssiRaw;}// if so, map the raw RSSI value to frequency rangeintrssiMapped=map(rssiRaw,RSSI_LOW,RSSI_HIGH,ATTACK_LOW,ATTACK_HIGH);// filter RSSI valuerssiAttack=movingAverage(rssiSamples,rssiMapped,pos);// check array position overflowif(pos==SAMPLES-1){pos=0;}else{pos++;}// print both values to PC for debugging/*DEBUG_PRINT(rssiRaw); DEBUG_PRINT('\t'); DEBUG_PRINT(rssiMapped); DEBUG_PRINT('\t'); DEBUG_PRINTLN(rssiFiltered);*/}}elseif(sender==24063){intrssiRaw=pulseIn(RSSI_PIN,HIGH,RSSI_READING_TIMEOUT);// check if the read was successfulif(rssiRaw>0){if(rssiRaw<=RSSI_LOW_2){RSSI_LOW_2=rssiRaw;}if(rssiRaw>=RSSI_HIGH_2){RSSI_HIGH_2=rssiRaw;}// if so, map the raw RSSI value to frequency rangeintrssiMapped=map(rssiRaw,RSSI_LOW_2,RSSI_HIGH_2,DECAY_LOW,DECAY_HIGH);// filter RSSI valuerssiDecay=movingAverage(rssiSamples,rssiMapped,pos);// check array position overflowif(pos==SAMPLES-1){pos=0;}else{pos++;}// print both values to PC for debugging/*DEBUG_PRINT(rssiRaw); DEBUG_PRINT('\t'); DEBUG_PRINT(rssiMapped); DEBUG_PRINT('\t'); DEBUG_PRINTLN(rssiFiltered);*/}}elseif(sender==20479){intrssiRaw=pulseIn(RSSI_PIN,HIGH,RSSI_READING_TIMEOUT);// check if the read was successfulif(rssiRaw>0){if(rssiRaw<=RSSI_LOW_3){RSSI_LOW_3=rssiRaw;}if(rssiRaw>=RSSI_HIGH_3){RSSI_HIGH_3=rssiRaw;}// if so, map the raw RSSI value to frequency rangeintrssiMapped=map(rssiRaw,RSSI_LOW_3,RSSI_HIGH_3,SUSTAIN_LOW,SUSTAIN_HIGH);// filter RSSI valuerssiSustain=movingAverage(rssiSamples,rssiMapped,pos);// check array position overflowif(pos==SAMPLES-1){pos=0;}else{pos++;}// print both values to PC for debugging/*DEBUG_PRINT(rssiRaw); DEBUG_PRINT('\t'); DEBUG_PRINT(rssiMapped); DEBUG_PRINT('\t'); DEBUG_PRINTLN(rssiFiltered);*/}}}}}if(noteDelay.ready()){// choose envelope levelsbyteattack_level=rand(128)+127;bytedecay_level=rand(255);envelope.setADLevels(attack_level,decay_level);// generate a random new adsr parameter value in millisecondsintr=rand(1000)-rand(1000);unsignedintnew_value=abs(r);release_ms=new_value;// randomly choose one of the adsr parameters and set the new value/*switch (rand(4)){ case 0: attack = new_value; break; case 1: decay = new_value; break; case 2: sustain = new_value; break; case 3: release_ms = new_value; break; }*/attack=rssiAttack;decay=rssiDecay;sustain=rssiSustain;envelope.setTimes(attack,decay,sustain,release_ms);envelope.noteOn();bytemidi_note=rand(107)+20;aOscil.setFreq((int)mtof(midi_note));DEBUG_PRINT("ATTACK - ");DEBUG_PRINTLN(attack);DEBUG_PRINT("DECAY - ");DEBUG_PRINTLN(decay);DEBUG_PRINT("SUSTAIN - ");DEBUG_PRINTLN(sustain);// print to screen/*Serial.print("midi_note\t"); Serial.println(midi_note); Serial.print("attack_level\t"); Serial.println(attack_level); Serial.print("decay_level\t"); Serial.println(decay_level); Serial.print("attack\t\t"); Serial.println(attack); Serial.print("decay\t\t"); Serial.println(decay); Serial.print("sustain\t\t"); Serial.println(sustain); Serial.print("release\t\t"); Serial.println(release_ms); Serial.println();*/noteDelay.start(attack+decay+sustain+release_ms);}envelope.update();gain=envelope.next();// this is where it's different to an audio rate envelope}intupdateAudio(){return(int)(gain*aOscil.next())>>8;}voidloop(){audioHook();}
And the three nodes:
// include all libraries#include<XBee.h>#include<SoftwareSerial.h>#include<MozziGuts.h>#include<Oscil.h>// oscillator template#include<tables/sin2048_int8.h>// sine table for oscillator#include<RollingAverage.h>#include<ControlDelay.h>// uncomment the following line to enable debug output#defineDEBUG// debug macros#ifdefDEBUG#defineDEBUG_BEGIN(x)Serial.begin(x)#defineDEBUG_PRINT(x)Serial.print(x)#defineDEBUG_PRINTLN(x)Serial.println(x)#else#defineDEBUG_BEGIN(x)#defineDEBUG_PRINT(x)#defineDEBUG_PRINTLN(x)#endifunsignedintecho_cells_1=32;unsignedintecho_cells_2=60;unsignedintecho_cells_3=127;// Mozzi control rate in HzControlDelay<128,int>kDelay;// 2seconds// oscils to compare bumpy to averaged control inputOscil<SIN2048_NUM_CELLS,AUDIO_RATE>aSin0(SIN2048_DATA);Oscil<SIN2048_NUM_CELLS,AUDIO_RATE>aSin1(SIN2048_DATA);Oscil<SIN2048_NUM_CELLS,AUDIO_RATE>aSin2(SIN2048_DATA);Oscil<SIN2048_NUM_CELLS,AUDIO_RATE>aSin3(SIN2048_DATA);// use: RollingAverage <number_type, how_many_to_average> myThingRollingAverage<int,32>kAverage;// how_many_to_average has to be power of 2intaveraged;floatrssiFiltered=512;// XBee packet reception timeout in ms#defineXBEE_PACKET_TIMEOUT250// RSSI reading timeout in us#defineRSSI_READING_TIMEOUT100// Arduino pin connections#defineRSSI_PIN5#defineXBEE_DOUT6#defineXBEE_DIN7#defineAUDIO_PIN9// freqency range bounds in Hz#defineFREQ_LOW512#defineFREQ_HIGH1023// RSSI range boundsintRSSI_LOW=11;intRSSI_HIGH=20;// number of samples for moving average filter#defineSAMPLES10// XBee object instanceXBeexbee=XBee();// address of destination XBee (coordinator address)XBeeAddress64addr64=XBeeAddress64(0x0013a200,0x4176E94F);// packet to be sent to coordinatoruint8_tpong[]={0xAA,0xAA};ZBTxRequestpongRequest=ZBTxRequest(addr64,pong,sizeof(pong));XBeeResponseresponse=XBeeResponse();// create reusable response objects for responses we expect to handleZBRxResponserx=ZBRxResponse();// SoftwareSerial portSoftwareSerialbeeSerial(XBEE_DOUT,XBEE_DIN);// Mozzi sine oscillatorOscil<SIN2048_NUM_CELLS,AUDIO_RATE>aSin(SIN2048_DATA);// array to hold RSSI samples for filterfloatrssiSamples[SAMPLES];// current position in arrayintpos=0;// moving average filterfloatmovingAverage(float*arr,intnewValue,intpos){// update arrayarr[pos]=newValue;// get array totalfloattotal=0;for(inti=0;i<SAMPLES;i++){total+=arr[i];}// get averagefloataverage=total/(float)SAMPLES;return(average);}voidsetup(){// begin Serial communication with PCDEBUG_BEGIN(9600);// set pin modespinMode(RSSI_PIN,INPUT);pinMode(AUDIO_PIN,OUTPUT);// initialize array contents to 0for(inti=0;i<SAMPLES;i++){rssiSamples[i]=0;}// begin Serial communication with XBeebeeSerial.begin(9600);// set XBee serial portxbee.setSerial(beeSerial);// start MozzikDelay.set(echo_cells_1);startMozzi();// set output frequency to the lowest value//aSin.setFreq(FREQ_LOW);}voidupdateControl(){// try to receive ping packet with 250 ms timeoutif(xbee.readPacket(XBEE_PACKET_TIMEOUT)){if(xbee.getResponse().isAvailable()){//DEBUG_PRINTLN(xbee.getResponse().getApiId());if(xbee.getResponse().getApiId()==16){xbee.getResponse().getZBRxResponse(rx);uint16_tsender=rx.getRemoteAddress16();//DEBUG_PRINTLN(sender);}}// try to read raw RSSI value with 100 us timeoutintrssiRaw=pulseIn(RSSI_PIN,LOW,RSSI_READING_TIMEOUT);// check if the read was successfulif(rssiRaw>0){// if so, map the raw RSSI value to frequency rangeif(rssiRaw<=RSSI_LOW){RSSI_LOW=rssiRaw;}if(rssiRaw>=RSSI_HIGH){RSSI_HIGH=rssiRaw;}intrssiMapped=map(rssiRaw,RSSI_LOW,RSSI_HIGH,FREQ_LOW,FREQ_HIGH);// filter RSSI valuerssiFiltered=movingAverage(rssiSamples,rssiMapped,pos);// check array position overflowif(pos==SAMPLES-1){pos=0;}else{pos++;}// print both values to PC for debugging/*DEBUG_PRINT(rssiRaw); DEBUG_PRINT('\t'); DEBUG_PRINT(rssiMapped); DEBUG_PRINT('\t'); DEBUG_PRINTLN(rssiFiltered);*/DEBUG_PRINT(rssiFiltered);DEBUG_PRINT(" - ");DEBUG_PRINT(RSSI_LOW);DEBUG_PRINT(" - ");DEBUG_PRINTLN(RSSI_HIGH);//rssiFiltered);// adjust sine wave frequency//aSin.setFreq(rssiFiltered);}}averaged=kAverage.next(rssiFiltered);aSin0.setFreq(averaged);aSin1.setFreq(kDelay.next(averaged));aSin2.setFreq(kDelay.read(echo_cells_2));aSin3.setFreq(kDelay.read(echo_cells_3));// send packet to coordinatorxbee.send(pongRequest);// delay(2000);}intupdateAudio(){return3*((int)aSin0.next()+aSin1.next()+(aSin2.next()>>1)+(aSin3.next()>>2))>>3;//return aSin.next();}voidloop(){pong[0]=100>>8&0xff;pong[1]=100&0xff;audioHook();}
You must be logged in to post a comment.