/* ReadMultiplexedSensorsAndSendMIDI * License: http://creativecommons.org/licenses/by/3.0/at/deed.en * ------------ * * Reads 4 16 channel Multiplexers and sends the values * * Created 12 December 2005 * copyleft 2005 Stephen Williams * */ #define LEDpin 1 #define CONTROLpin1 2 #define CONTROLpin2 3 #define CONTROLpin3 4 #define CONTROLpin4 5 int lastValues[64]; unsigned long lastCompleteSend = 0; // Some error chacking would be good here. //If the values exceede 128 the will not fit in one byte. void sendCommand(char command, char sensorID, char value) { serialWrite(command); serialWrite(sensorID); serialWrite(value); } void setup() { // set the states of the I/O pins: pinMode(LEDpin, OUTPUT); pinMode(CONTROLpin1, OUTPUT); pinMode(CONTROLpin2, OUTPUT); pinMode(CONTROLpin3, OUTPUT); pinMode(CONTROLpin4, OUTPUT); beginSerial(57600); int i = 0; for(i=0; i<4; i++){ lastValues[i] = -100; } lastCompleteSend = millis(); } void loop() { boolean sendAll = false; if( abs(millis() - lastCompleteSend) > 5000 ){ sendAll = true; } int i; int j; for (i=0; i <16; i++) { // set control pins on the multiplexers digitalWrite(CONTROLpin1, (i&15)>>3);//bit4 digitalWrite(CONTROLpin2, (i&7)>>2);//bit3 digitalWrite(CONTROLpin3, (i&3)>>1);//bit2 digitalWrite(CONTROLpin4, (i&1) );//bit1 // read the analogue inputs and send the values of the sensors. for(j=0; j<4; j++){ int sensorValue = analogRead(j) / 5;// make the value fit in one byte int index = i+16*j; // sends a value every second or when it changes significantly if( sendAll || abs(lastValues[index] - sensorValue) > 1){ lastValues[index] = sensorValue; sendCommand(0x90, index, sensorValue); } } // if you uncomment this line you can check the control pins // of the multiplexers with your multimeter. You could change this to // turn on when an digital input is set so you can turn it on without // reloading the software. //delay(500); } if( sendAll ){ lastCompleteSend = millis(); } }