Mixed Relations - Arduino Code with Temperature

From Banglab

Jump to: navigation, search
/*
 * Technesexual arduino code for heart rate monitor and lilypad temp sensor.
 * 
 * Based on code from dan julio designs for heart rate monitor/i2c.
 *
 * Simple Arduino-based program to read values from the HRMI using the I2C interface
 *
 * Connections
 *    Arduino            HRMI
 *    -----------------------
 *      +5                 +5 (Power for the HRMI)
 *      GND                GND
 *      Analog In 5        TX (I2C SCL) (recommend 4.7 kOhm pullup)
 *      Analog In 4        RX (I2C SDA) (recommend 4.7 kOhm pullup)
 *      Analog In 1        Temperature Sensor
 *
 *
 * Note: By default the Arduino Wiring library is limited to a maximum
 *       I2C read of 32 bytes. The Get Heartrate command is limited
 *       by this code to a maximum of 30 values (for a max I2C packet
 *       of 32 bytes).
 *
 */
#include "Wire.h"
#include "hrmi_funcs.h"
#include <Firmata.h>

/*
 * Configuration Information
 *
 * Change these constants to match your specific configuration. These
 * values are the factory default (no OP1-OP7 jumpers installed). Jumper
 * OP1 should be installed and jumper SJ1 removed.
 *
 * HRMI_HOST_BAUDRATE should be set to the baudrate the host will use
 *   to communicate with the Arduino over the serial interface.
 *
 * HRMI_I2C_ADDR should be set to the I2C address the HRMI is configured
 *   with.
 */
#define HRMI_HOST_BAUDRATE 9600
#define HRMI_I2C_ADDR       127

/*
 * Program constants
 */
#define MAX_IN_BUFFSIZE 16

/*
 * Global variables
 */
char serInStr[MAX_IN_BUFFSIZE];    // Serial input string array
int numEntries = 0;                // Number of HR values to request
int numRspBytes;                   // Number of Response bytes to read
byte i2cRspArray[34];
                 // I2C response array, sized to read 32 HR values
byte hrmi_addr = HRMI_I2C_ADDR;      // I2C address to use

int tempSensorPwrPin = 13;
int sensorPin = 1;
int sensorValue;


/*
  * Arduino initialization code segment
  */
void setup()
{
   // Initialize the I2C communication
   hrmi_open();
   // Initialize the serial interface
   Serial.begin(HRMI_HOST_BAUDRATE);

  // TURN*ON the temperature sensor   
  pinMode(tempSensorPwrPin, OUTPUT);
  digitalWrite(tempSensorPwrPin, HIGH);
        
}
/*
  * Arduino main code loop
  */
void loop()
{
     
   // Request a set of heart rate values
   hrmiCmdArg(hrmi_addr, 'G', (byte) numEntries);
   // Get the response from the HRMI
   numRspBytes = numEntries + 2;
   if (hrmiGetData(hrmi_addr, numRspBytes, i2cRspArray) != -1) {
                
    //send the heart rate    
    Serial.print(i2cRspArray[2]);
    
    //debug code
    //Serial.print("H:");  
    //Serial.println(i2cRspArray[2],DEC);
       
    }
     
   // Setup to do it again
   if (++numEntries > 1)
         numEntries = 0;

   //send the temperature reading, but double it so that its outside the range
   //  of the heartrate, and use moses in pd to split the values
   
   delay(50);
   
   sensorValue = analogRead(sensorPin);
   Serial.print(sensorValue, BYTE);
   
   //Serial.print("T:");  
   //Serial.println(sensorValue);

   delay(50);                    // Delay 15 msec between commands

}
Personal tools