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; }*/ }
You must be logged in to post a comment.