<repo_name>DDuunk/ECG-Device<file_sep>/README.md
# ECG-Device
Hardware code for a ECG device, see [ECG-App](https://github.com/DDuunk/ECG-App/) for the app used in this project.
<file_sep>/src/ECG/main.cpp
#include "main.h"

void error(const __FlashStringHelper*err) {
    Serial.println(err);
    while (1);
}

void setup() {
    Serial.begin(SERIAL_BAUD_RATE);

    pinMode(LO_POSITIVE, INPUT);
    pinMode(LO_NEGATIVE, INPUT);

    Serial.println(F("Adafruit Bluefruit Command <-> Data Mode Example"));
    Serial.println(F("------------------------------------------------"));
    /* Initialise the module */
    Serial.print(F("Initialising the Bluefruit LE module: "));

    if (!ble.begin(VERBOSE_MODE)) {
        error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?"));
    }
    Serial.println(F("OK!"));

    /* Perform a factory reset to make sure everything is in a known state */
    Serial.println(F("Performing a factory reset: "));
    if (!ble.factoryReset()) {
      error(F("Couldn't factory reset"));
    }

    /* Disable command echo from Bluefruit */
    ble.echo(false);

    Serial.println("Requesting Bluefruit info:");
    /* Print Bluefruit information */
    ble.info();

    Serial.println(F("Please use Adafruit Bluefruit LE app to connect in UART mode"));
    Serial.println(F("Then Enter characters to send to Bluefruit"));

    ble.verbose(false);  // debug info is a little annoying after this point!

    // Set module to DATA mode
    Serial.println(F("Switching to DATA mode!"));
    ble.setMode(BLUEFRUIT_MODE_DATA);
}

void loop() {
    if((digitalRead(LO_POSITIVE) == 1) || (digitalRead(LO_NEGATIVE) == 1)) {
        Serial.println(F("!"));
        delay(1000);
    } else {
        Serial.print(F("Updating HRM value to "));
        Serial.print((analogRead(AD8232_INPUT)));
        Serial.println("\r");
        Serial.println(F(" BPM"));
        
       ble.print((analogRead(AD8232_INPUT)));
       ble.println();
    }
    delay(1);
}<file_sep>/src/ECG/main.h
#ifndef MAIN_H
#define MAIN_H

#include "Arduino.h"
#include <SPI.h>
#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "Adafruit_BluefruitLE_UART.h"

#define SERIAL_BAUD_RATE 115200

/**
 * AD8232 Settings
 */ 
#define LO_POSITIVE 3
#define LO_NEGATIVE 4
#define AD8232_INPUT A0

/**
 * Adafruit Bluefruit UART Settings
 */
int32_t hrmServiceId;
int32_t hrmMeasureCharId;
int32_t hrmLocationCharId;
boolean success;

#define BUFSIZE                        128   // Size of the read buffer for incoming data
#define VERBOSE_MODE                   true  // If set to 'true' enables debug output
#define BLUEFRUIT_SWUART_RXD_PIN       9    // Required for software serial!
#define BLUEFRUIT_SWUART_TXD_PIN       10   // Required for software serial!
#define BLUEFRUIT_UART_CTS_PIN         11   // Required for software serial!
#define BLUEFRUIT_UART_RTS_PIN         8    // Optional, set to -1 if unused
#define BLUEFRUIT_UART_MODE_PIN        12    // Set to -1 if unused

#ifdef Serial1    // this makes it not complain on compilation if there's no Serial1
  #define BLUEFRUIT_HWSERIAL_NAME      Serial1
#endif

SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN);
Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN, BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN);

#endif //MAIN_H