Hi ,
I'm trying to make a midi contoller with 4 arduinos,1 Master and 3 slaves(Buttons,Rotary potentiometers and Faders) using the wire library and not the midi library.It was imposed by my Teacher.I wrote and tested code on one master and one slave.It works find.My problem is when I connect all the arduinos together.What I want si that when the state of a button/potentiometer changes a value is send to the master.I think that to make it work on each slave I have to write a function in the loop which tells us whether one of the control state has changed.On the master in the loop I make requests to each slave.the first request to know if there was a change and as long as there are changes of state on that arduino the master should make request to the arduino to have the change value.But I don't know how to synchronise all this .I tried many things without success.Please help.
Here is the code for testing on the master and the slave rotary potentiometer
Master
#include <Wire.h>
void setup() {
Wire.begin();
Serial.begin(9600);
}
void loop() {
Wire.requestFrom(8,3);
while(Wire.available()){
byte c = Wire.read();
Serial.println(c);
}
delay(1000);
}
slave
#include <Wire.h>
int address = 8;
#define NUM_AI 2
#define FILTER_AMOUNT 2
#define ANALOGUE_PIN_ORDER A0, A1
// Timeout is in microseconds
#define ANALOGUE_INPUT_CHANGE_TIMEOUT 250000
byte analogueInputs[NUM_AI];
// Variable to hold temporary analogue values, used for analogue filtering logic.
byte tempAnalogueInput;
// Variable to hold difference between current and new analogue input values.
int analogueDiff = 0;
// Array containing a mapping of analogue pins to channel index. This array size must match NUM_AI above.
byte analogueInputMapping[NUM_AI] = {ANALOGUE_PIN_ORDER};
// This is used as a flag to indicate that an analogue input is changing.
boolean analogueInputChanging[NUM_AI];
// Time the analogue input was last moved
unsigned long analogueInputTimer[NUM_AI];
void setup() {
Wire.begin(address);
Wire.onRequest(onRequest);
// Initialise each analogue input channel.
for (int i = 0; i < NUM_AI; i++)
{
// Set the pin direction to input.
pinMode(analogueInputMapping[i], INPUT);
// Initialise the analogue value with a read to the input pin.
analogueInputs[i] = analogRead(analogueInputMapping[i]);
// Assume no analogue inputs are active
analogueInputChanging[i] = false;
analogueInputTimer[i] = 0;
}
}
void loop() {
delay(100);
}
void onRequest(){
for ( int i = 0; i < NUM_AI; i++)
{
// Read the analogue input pin, dividing it by 8 so the 10-bit ADC value (0-1023) is converted to a 7-bit MIDI value (0-127).
tempAnalogueInput = analogRead(analogueInputMapping[i]) / 8;
// Take the absolute value of the difference between the curent and new values
analogueDiff = abs(tempAnalogueInput - analogueInputs[i]);
// Only continue if the threshold was exceeded, or the input was already changing
if ((analogueDiff > 0 && analogueInputChanging[i] == true) || analogueDiff >= FILTER_AMOUNT)
{
// Only restart the timer if we're sure the input isn't 'between' a value
// ie. It's moved more than FILTER_AMOUNT
if (analogueInputChanging[i] == false || analogueDiff >= FILTER_AMOUNT)
{
// Reset the last time the input was moved
analogueInputTimer[i] = micros();
// The analogue input is moving
analogueInputChanging[i] = true;
}
else if (micros() - analogueInputTimer[i] > ANALOGUE_INPUT_CHANGE_TIMEOUT)
{
analogueInputChanging[i] = false;
}
// Only send data if we know the analogue input is moving
if (analogueInputChanging[i] == true)
{
// Record the new analogue value
analogueInputs[i] = tempAnalogueInput;
controlChange(16,22, analogueInputs[i]);
}
}
if (analogueInputs[i] != tempAnalogueInput)
{
// Record the new analogue value
analogueInputs[i] = tempAnalogueInput;
// Send the analogue value
controlChange(16,22, analogueInputs[i]);
}
else{
NoControlChange(16, 22);
}
}
}
void controlChange(int channel, int control, int value){
byte SendMidi[] = {channel,control,value};
Wire.write(SendMidi,3);
}
void NoControlChange(int channel, int control){
byte SendMidi[] = {channel,control,200};
Wire.write(SendMidi,3);
}