coding arduino

#include CapSense.h

/*
* CapitiveSense Library Demo Sketch
* Paul Badger 2008
* Slightly adapted by Bare Conductive 2011
* Uses a high value resistor e.g. 10 megohm between send pin and receive pin
* Resistor effects sensitivity, experiment with values, 50 kilohm - 50 megohm. Larger resistor values yield larger sensor values.
* Receive pin is the sensor pin - try different amounts of Bare Paint
* Best results are obtained if sensor foil and wire is covered with an insulator such as paper or plastic sheet
*/

/*
* Arduino Code for 'Wave in Field'
* Damien Borowik 2013
*
* Capacitance data is sent to Max/MSP via Serial.
* 2 DACs (model MCP4822) are daisy chained to communicate using SPI.
* each DAC has 2 channels, which are used to pass data from Max/MSP to voltage to control the sound made by the analog synth (Monotron).
*/

// capacitance sensor with pins
CapSense cs_7_4 = CapSense(7,4);


// include SPI library
#include

// setup of pins to control the DACs (CS necessary to Chip Select in SPI, pin 2 on each DACs)
const int PIN1_CS = 10;
const int PIN2_CS = 3;
// different GAIN used to pass data to DACs
const int GAIN_1 = 0x1;
const int GAIN_2 = 0x0;

// gate pin for gate control of monotron
const int gatePin = 2;

// serialMessage received from Max/MSP
int serialMessage = 0;

// values received from Max/MSP
int val0 = 2;
int val1 = 2;
int val2 = 2;
int val3 = 2;

void setup()
{
// autocalibrate capacitance sensing
cs_7_4.set_CS_AutocaL_Millis(0xFFFFFFFF);

// initialise Serial at 9600 Baud rate
Serial.begin(9600);
// initialise slave pins
pinMode(PIN1_CS, OUTPUT);
pinMode(PIN2_CS, OUTPUT);
// setup SPI
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE2);
SPI.setClockDivider(SPI_CLOCK_DIV4);
}

// function used to produce a note with monotron, for each channels controlled on the DACs (x4), see use in loop().
void setOutput(int pin, byte channel, byte gain, byte shutdown, unsigned int val)
{
// val is the data/voltage transmitted, set in bytes
byte lowByte = val & 0xff;
byte highByte = ((val >> 8) & 0xff) | channel << 7 | gain << 5 | shutdown << 4;

// DAC is selected when LOW
digitalWrite(pin, LOW);
// transfer of data
SPI.transfer(highByte);
SPI.transfer(lowByte);
// deselect DAC
digitalWrite(pin, HIGH);

// send gate signal (turning the signal off again makes a clickety noise which was not desirable).
digitalWrite(gatePin, HIGH);
delay(5);

}

void loop()
{


// sending the capacitance data to Max/MSP
long capSenseVal = cs_7_4.capSense(30);
Serial.println(capSenseVal);


// when receiving data from Max/MSP
while (Serial.available() > 1)
{
serialMessage = Serial.read();
// val0 PITCH
if (serialMessage == 20) {
val0 = Serial.read();
// val1 LFO
} else if (serialMessage == 21) {
val1 = Serial.read();
// val2 CUT OFF
} else if (serialMessage == 22) {
val2 = Serial.read();
// val3 RATE
} else if (serialMessage == 23) {
val3 = Serial.read();
}
}
Serial.flush();

// updating the sound using setOutput
setOutput(PIN1_CS, 0, GAIN_2, 1, (val0 * 256));
setOutput(PIN2_CS, 0, GAIN_2, 1, (val1 * 256));
setOutput(PIN2_CS, 1, GAIN_2, 1, (val2 * 256));
setOutput(PIN1_CS, 1, GAIN_2, 1, (val3 * 256));

}