Skip to content

Instantly share code, notes, and snippets.

@joelsimonoff
Created April 10, 2016 07:34
Show Gist options
  • Save joelsimonoff/7d0f8c479fedf099d443b6e758012976 to your computer and use it in GitHub Desktop.
Save joelsimonoff/7d0f8c479fedf099d443b6e758012976 to your computer and use it in GitHub Desktop.
//
// General
//
#define NUM_RELAYS 4
typedef enum {OFF, ON} State;
State relayStates[NUM_RELAYS] = {OFF, OFF, OFF, OFF};
//
// Commands
//
const uint8_t R_1_ON = 0;
const uint8_t R_1_OFF = 1;
const uint8_t R_2_ON = 2;
const uint8_t R_2_OFF = 3;
const uint8_t R_3_ON = 4;
const uint8_t R_3_OFF = 5;
const uint8_t R_4_ON = 6;
const uint8_t R_4_OFF = 7;
//
// Relay Pin Numbers
//
#define R_1 9
#define R_2 10
#define R_3 11
#define R_4 A0
//
// Relay Switch Input Numbers
//
#define R_1_I 2
#define R_2_I 3
#define R_3_I 5
#define R_4_I 6
/*********************************************************************
Blue Car Version 0
MIT license, check LICENSE for more information
All text above, and the splash screen below must be included in
any redistribution
Modified By Joel Simonoff November 2015 for BlueCar. Joels edits will
be covered under the MIT License.
*********************************************************************/
#include <Arduino.h>
#include <SPI.h>
#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "BluefruitConfig.h"
#include "BlueCarConfig.h"
#define MODE_LED_BEHAVIOUR "MODE"
#define LED 13
Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
void setup(void){
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
setupRelayPins();
while (!Serial); // required for Flora & Micro
delay(500);
Serial.begin(115200);
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) )
{
Serial.println(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() ){
Serial.println(F("Couldn't factory reset"));
}
/* Disable command echo from Bluefruit */
ble.echo(false);
Serial.println("REQing Bluefruit info:");
/* Print Bluefruit information */
ble.info();
ble.verbose(false); // debug info is a little annoying after this point!
// Set device name
Serial.println("Set Device Name to Joel's Bluefruit Micro");
ble.sendCommandCheckOK("AT+GAPDEVNAME=Joel's Bluefruit Micro");
/* Wait for connection */
Serial.println("Waiting For Connection");
while (! ble.isConnected()) {
digitalWrite(LED, HIGH);
delay(250);
digitalWrite(LED, LOW);
delay(250);
}
Serial.println(F("******************************"));
// Change Mode LED Activity
Serial.println(F("Change LED activity to " MODE_LED_BEHAVIOUR));
ble.sendCommandCheckOK("AT+HWModeLED=" MODE_LED_BEHAVIOUR);
// Set module to DATA mode
Serial.println( F("Switching to DATA mode!") );
ble.setMode(BLUEFRUIT_MODE_DATA);
Serial.println(F("******************************"));
}
void loop(void){
//
// Check for input from serial port
//
char n, inputs[BUFSIZE+1];
if (Serial.available()){
n = Serial.readBytes(inputs, BUFSIZE);
inputs[n] = 0;
// Send characters to Bluefruit
Serial.print("Sending: ");
Serial.print(inputs);
// Send input data to host via Bluefruit
ble.write(inputs);
}
// Echo received data
while (ble.available()){
uint8_t c = ble.read();
Serial.print("Command: ");
Serial.println(c);
execute(c);
}
checkAndExecuteInput();
}
//
// Checks to see if the user manually switched a relay on
//
void checkAndExecuteInput(){
switch(digitalRead(R_1_I)){
case HIGH:
if (relayStates[0]){
ble.write(R_1_OFF);
relayStates[0] = OFF;
digitalWrite(R_1, LOW);
}
break;
case LOW:
if (!relayStates[0]){
ble.write(R_1_ON);
relayStates[0] = ON;
digitalWrite(R_1, HIGH);
}
break;
}
switch(digitalRead(R_2_I)){
case HIGH:
if (relayStates[1]){
ble.write(R_2_OFF);
relayStates[1] = OFF;
digitalWrite(R_2, LOW);
}
break;
case LOW:
if (!relayStates[1]){
ble.write(R_2_ON);
relayStates[1] = ON;
digitalWrite(R_2, HIGH);
}
break;
}
switch(digitalRead(R_3_I)){
case HIGH:
if (relayStates[2]){
ble.write(R_3_OFF);
relayStates[2] = OFF;
digitalWrite(R_3, LOW);
}
break;
case LOW:
if (!relayStates[2]){
ble.write(R_3_ON);
relayStates[2] = ON;
digitalWrite(R_3, HIGH);
}
break;
}
switch(digitalRead(R_4_I)){
case HIGH:
if (relayStates[3]){
ble.write(R_4_OFF);
relayStates[3] = OFF;
digitalWrite(R_4, LOW);
}
break;
case LOW:
if (!relayStates[3]){
ble.write(R_4_ON);
relayStates[3] = ON;
digitalWrite(R_4, HIGH);
}
break;
}
}
//
// Executes commands recieved via BLE
//
void execute(uint8_t c){
switch(c){
case R_1_ON:
digitalWrite(R_1, HIGH);
break;
case R_1_OFF:
digitalWrite(R_1, LOW);
break;
case R_2_ON:
digitalWrite(R_2, HIGH);
break;
case R_2_OFF:
digitalWrite(R_2, LOW);
break;
case R_3_ON:
digitalWrite(R_3, HIGH);
break;
case R_3_OFF:
digitalWrite(R_3, LOW);
break;
case R_4_ON:
digitalWrite(R_4, HIGH);
break;
case R_4_OFF:
digitalWrite(R_4, LOW);
break;
default:
Serial.println("Command Unrecognized");
break;
}
}
//
// establsihes the pinmodes for the relay pins
//
void setupRelayPins(){
pinMode(R_1, OUTPUT);
pinMode(R_2, OUTPUT);
pinMode(R_3, OUTPUT);
pinMode(R_4, OUTPUT);
digitalWrite(R_1, LOW);
digitalWrite(R_2, LOW);
digitalWrite(R_3, LOW);
digitalWrite(R_4, LOW);
pinMode(R_1_I, INPUT_PULLUP);
pinMode(R_2_I, INPUT_PULLUP);
pinMode(R_3_I, INPUT_PULLUP);
pinMode(R_4_I, INPUT_PULLUP);
Serial.println("Pins Setup");
}
// COMMON SETTINGS
// ----------------------------------------------------------------------------------------------
// These settings are used in both SW UART, HW UART and SPI mode
// ----------------------------------------------------------------------------------------------
#define BUFSIZE 128 // Size of the read buffer for incoming data
#define VERBOSE_MODE true // If set to 'true' enables debug output
// SHARED SPI SETTINGS
// ----------------------------------------------------------------------------------------------
// The following macros declare the pins to use for HW and SW SPI communication.
// SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno when
// using HW SPI. This should be used with nRF51822 based Bluefruit LE modules
// that use SPI (Bluefruit LE SPI Friend).
// ----------------------------------------------------------------------------------------------
#define BLUEFRUIT_SPI_CS 8
#define BLUEFRUIT_SPI_IRQ 7
#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment