boolean digitalInputsEnabled = false; // output digital inputs or not
int digitalInputs;
int previousDigitalInputs; // previous output to test for change
int digitalPinStatus = 3; // bitwise array to store pin status, ignore RxTx pins
/* PWM/analog outputs /
int pwmStatus = 0; // bitwise array to store PWM status
/ analog inputs /
unsigned int analogPinsToReport = 0; // bitwise array to store pin reporting
int analogPin = 0; // counter for reading analog pins
int analogData; // storage variable for data from analogRead()
/ timer variables */
extern volatile unsigned long timer0_overflow_count; // timer0 from wiring.c
unsigned long nextExecuteTime; // for comparison with timer0_overflow_count
int heart = 8;
//pin that the heart rate is read from
int heartDetect=0;
//detects if value has been sent to Arduino
int heartRate = 0;
//heart rate
int prevState = 0;
//used to check if data has come through
unsigned long previousPrintTime;
//declares time variable
/*==============================================================================
- FUNCTIONS
============================================================================/
/* -----------------------------------------------------------------------------
- output the version message to the serial port */
void printVersion() {
Serial.print(REPORT_VERSION, BYTE);
Serial.print(FIRMATA_MINOR_VERSION, BYTE);
Serial.print(FIRMATA_MAJOR_VERSION, BYTE);
}
/* -----------------------------------------------------------------------------
- output digital bytes received from the serial port */
void outputDigitalBytes(byte pin0_6, byte pin7_13) {
int i;
int mask;
int twoBytesForPorts;
// this should be converted to use PORTs
twoBytesForPorts = pin0_6 + (pin7_13 << 7);
for(i=2; i<TOTAL_DIGITAL_PINS; ++i) { // ignore Rx,Tx pins (0 and 1)
mask = 1 << i;
if( (digitalPinStatus & mask) && !(pwmStatus & mask) ) {
digitalWrite(i, twoBytesForPorts & mask ? HIGH : LOW);
}
}
}
/* -----------------------------------------------------------------------------
- check all the active digital inputs for change of state, then add any events
- to the Serial output queue using Serial.print() */
void checkDigitalInputs(void) {
if(digitalInputsEnabled) {
previousDigitalInputs = digitalInputs;
digitalInputs = PINB << 8; // get pins 8-13
digitalInputs += PIND; // get pins 0-7
digitalInputs = digitalInputs &~ digitalPinStatus; // ignore pins set OUTPUT
if(digitalInputs != previousDigitalInputs) {
// TODO: implement more ports as channels for more than 16 digital pins
Serial.print(DIGITAL_MESSAGE,BYTE);
Serial.print(digitalInputs % 128, BYTE); // Tx pins 0-6
Serial.print(digitalInputs >> 7, BYTE); // Tx pins 7-13
}
}
}
// -----------------------------------------------------------------------------
/* sets the pin mode to the correct state and sets the relevant bits in the
- two bit-arrays that track Digital I/O and PWM status
*/
void setPinMode(byte pin, byte mode) {
if(pin > 1) { // ignore RxTx pins (0,1)
if(mode == INPUT) {
digitalPinStatus = digitalPinStatus &~ (1 << pin);
pwmStatus = pwmStatus &~ (1 << pin);
digitalWrite(pin,LOW); // turn off pin before switching to INPUT
pinMode(pin,INPUT);
}
else if(mode == OUTPUT) {
digitalPinStatus = digitalPinStatus | (1 << pin);
pwmStatus = pwmStatus &~ (1 << pin);
pinMode(pin,OUTPUT);
}
else if( mode == PWM ) {
digitalPinStatus = digitalPinStatus | (1 << pin);
pwmStatus = pwmStatus | (1 << pin);
pinMode(pin,OUTPUT);
}
// TODO: save status to EEPROM here, if changed
}
}
// -----------------------------------------------------------------------------
/* sets bits in a bit array (int) to toggle the reporting of the analogIns
*/
void setAnalogPinReporting(byte pin, byte state) {
if(state == 0) {
analogPinsToReport = analogPinsToReport &~ (1 << pin);
}
else { // everything but 0 enables reporting of that pin
analogPinsToReport = analogPinsToReport | (1 << pin);
}
// TODO: save status to EEPROM here, if changed
}
/* -----------------------------------------------------------------------------
- processInput() is called whenever a byte is available on the
- Arduino's serial port. This is where the commands are handled. */
void processInput(int inputData) {
int command;
// a few commands have byte(s) of data following the command
if( (waitForData > 0) && (inputData < 128) ) {
waitForData--;
storedInputData[waitForData] = inputData;
if( (waitForData==0) && executeMultiByteCommand ) { // got the whole message
switch(executeMultiByteCommand) {
case ANALOG_MESSAGE:
setPinMode(multiByteChannel,PWM);
analogWrite(multiByteChannel,
(storedInputData[0] << 7) + storedInputData[1] );
break;
case DIGITAL_MESSAGE:
outputDigitalBytes(storedInputData[1], storedInputData[0]); //(LSB, MSB)
break;
case SET_DIGITAL_PIN_MODE:
setPinMode(storedInputData[1], storedInputData[0]); // (pin#, mode)
if(storedInputData[0] == INPUT)
digitalInputsEnabled = true; // enable reporting of digital inputs
break;
case REPORT_ANALOG_PIN:
setAnalogPinReporting(multiByteChannel,storedInputData[0]);
break;
case REPORT_DIGITAL_PORTS:
// TODO: implement MIDI channel as port base for more than 16 digital inputs
if(storedInputData[0] == 0)
digitalInputsEnabled = false;
else
digitalInputsEnabled = true;
break;
}
executeMultiByteCommand = 0;
}
} else {
// remove channel info from command byte if less than 0xF0
if(inputData < 0xF0) {
command = inputData & 0xF0;
multiByteChannel = inputData & 0x0F;
} else {
command = inputData;
// commands in the 0xF* range don't use channel data
}
switch (command) { // TODO: these needs to be switched to command
case ANALOG_MESSAGE:
case DIGITAL_MESSAGE:
case SET_DIGITAL_PIN_MODE:
waitForData = 2; // two data bytes needed
executeMultiByteCommand = command;
break;
case REPORT_ANALOG_PIN:
case REPORT_DIGITAL_PORTS:
waitForData = 1; // two data bytes needed
executeMultiByteCommand = command;
break;
case SYSTEM_RESET:
// this doesn't do anything yet
break;
case REPORT_VERSION:
printVersion();
break;
}
}
}
/* -----------------------------------------------------------------------------
- this function checks to see if there is data waiting on the serial port
- then processes all of the stored data
*/
void checkForSerialReceive() {
while(Serial.available())
processInput(Serial.read());
}
// =============================================================================
// used for flashing the pin for the version number
void pin13strobe(int count, int onInterval, int offInterval) {
byte i;
pinMode(13, OUTPUT);
for(i=0; i<count; i++) {
delay(offInterval);
digitalWrite(13,1);
delay(onInterval);
digitalWrite(13,0);
}
}
/*==============================================================================
- SETUP()
============================================================================/
void setup() {
byte i;
Serial.begin(57600); // 9600, 14400, 38400, 57600, 115200
// flash the pin 13 with the protocol version
pinMode(13,OUTPUT);
pin13strobe(2,1,4); // separator, a quick burst
delay(500);
pin13strobe(FIRMATA_MAJOR_VERSION, 200, 400);
delay(500);
pin13strobe(2,1,4); // separator, a quick burst
delay(500);
pin13strobe(FIRMATA_MINOR_VERSION, 200, 400);
delay(500);
pin13strobe(2,1,4); // separator, a quick burst
for(i=0; i<TOTAL_DIGITAL_PINS; ++i) {
setPinMode(i,INPUT);
}
// TODO: load state from EEPROM here
printVersion();
/* TODO: send digital inputs here, if enabled, to set the initial state on the
- host computer, since once in the loop(), the Arduino will only send data on
- change. */
}
void setup() {
Serial.begin(19200);
//sets serial speed
pinMode(heart, INPUT);
//declares didgital pin as an input pin
}
/*==============================================================================
- LOOP()
============================================================================/
void loop() {
/* DIGITALREAD - as fast as possible, check for changes and output them to the