View Single Post
Old 07-01-2013, 12:19 AM   #4
mercraus
It doesn't mean anything
 
Drives: 2011 Camaro LS
Join Date: Jul 2012
Location: Wadsworth, Ohio
Posts: 85
My Arduino parts arrived so I started that part of the project over the weekend. I somehow ordered the wrong Relay Shield, but no big deal as I can just attach the right one when it arrives.

I finished the code the Arduino will use then tested it with my computer and some LEDs. Everything seems to be working great. Here's a video of the test:



For anyone interested, here are the details:
The Relay Shield arrived in one piece and didn't really require any setup. The Bluetooth Shield however, was in pieces and needed to be soldered.


First thing I soldered on was the pins to connect to the Arduino. I kinda got ahead of myself though and used the wrong set of pins in one section...
Here's how I originally soldered it (the wrong way):


Here's the right way:

Notice the smaller set of pins on the bottom left corner.

By the time I realized I made the mistake, I had already soldered the shield headers. So unfortunately one melted a little while trying to de-solder the pins, but luckily it still works.



Finally was the pins for the transmission jumpers and the cover for the Bluetooth chip.


Here's a picture showing the position of the jumpers and switch.

The switch is down, BT_RX is jumppered to 3, and BT_TX is jumppered to 2. This is necessary for my code to work.


Here's the code, it's pretty simple. Pins 4-6 are activated based on the bluetooth serial input, and each pin corresponds to a relay. Analog pin 1 is monitored for Bluetooth state, and sets all pins to low if connection is lost. The status of analog pins 2 and 3 (for the gauges) and the digital pins are sent back 20x a second.
Code:
/*
BluetoothShield Code NOS.pde. This sketch allows the shield
to commmunicate over software serial using pins 2 and 3 and
trigger pins 4 - 6 based on the characters received. Output
state of trigger pins are transmitted back. Analog pin 1 is 
monitored for Bluetooth Status. Analog pins 2 and 3 are also
monitored. Their values are transformed and also transmitted back.

Author: Alphonso Samano
 
*/
 
 
/* Upload this sketch into Arduino and press reset*/
 
#include <SoftwareSerial.h>   //Software Serial Port

#define RxD 2
#define TxD 3
#define ARM 4
#define PURGE 5
#define OPENER 6

#define BT 1
#define AFR 2
#define BP 3

char recvChar;
String systemState;
int btStatus;
int btTracker = 0;
int afrGauge;
int bpGauge;
unsigned long refresh;
 
SoftwareSerial blueToothSerial(RxD,TxD);
 
void setup() 
{
  Serial.begin(9600);
  pinMode(RxD, INPUT);
  pinMode(TxD, OUTPUT);
  pinMode(ARM, OUTPUT);
  pinMode(PURGE, OUTPUT);
  pinMode(OPENER, OUTPUT);
  setupBlueToothConnection();
} 
 
void loop() 
{
  CheckBTStatus();
  if (btTracker >= 5)
  {
    CheckSwitches();
    SendSystemState();
  }
} 
 
void setupBlueToothConnection()
{
  blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
  blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
  blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave"
  blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
  blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
  delay(2000); // This delay is required.
  blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable 
  delay(2000); // This delay is required.
  blueToothSerial.flush();
}

void CheckBTStatus()
{
  btStatus = analogRead(BT);
  if (btTracker == 0 && btStatus >= 5)
  {
    btTracker = btStatus;
    Serial.print("Bluetooth Connection ESTABLISHED");
    delay(2000); // This delay is required.
    refresh = millis();
  }
  if (btTracker >= 5 && btStatus == 0)
  {
    btTracker = 0;
    Serial.print("Bluetooth Connection LOST");
    digitalWrite(ARM, LOW);
    digitalWrite(PURGE, LOW);
    digitalWrite(OPENER, LOW);
    delay(2000); // This delay is required.
  }
}

void CheckSwitches()
{
  if(blueToothSerial.available()) //check if there's any data sent from the remote bluetooth shield
  {
    recvChar = blueToothSerial.read();
    switch (recvChar)
    {
      case 'A':
        digitalWrite(ARM, HIGH);
        break;
      case 'D':
        digitalWrite(ARM, LOW);
        break;
      case 'P':
        digitalWrite(PURGE, HIGH);
        break;
      case 'F':
        digitalWrite(PURGE, LOW);
        break;
      case 'O':
        digitalWrite(OPENER, HIGH);
        break;
      case 'C':
        digitalWrite(OPENER, LOW);
        break;
      }
      blueToothSerial.flush();
    }
}

void SendSystemState()
{
  if (millis() - refresh >= 50UL)
  {
    afrGauge = analogRead(AFR);
    bpGauge = analogRead(BP);
    blueToothSerial.print('{');
    blueToothSerial.print(afrGauge);
    blueToothSerial.print(',');
    blueToothSerial.print(bpGauge);
    blueToothSerial.print(',');
    blueToothSerial.print(digitalRead(ARM));
    blueToothSerial.print(',');
    blueToothSerial.print(digitalRead(PURGE));
    blueToothSerial.print(',');
    blueToothSerial.print(digitalRead(OPENER));
    blueToothSerial.print('}');
    blueToothSerial.print('\r');
    blueToothSerial.print('\n');
    refresh = millis();
  }
}

That's all for now. Next step is the Android app.
__________________
Hmmmm..... I should probably get a fancy sig

Last edited by mercraus; 07-01-2013 at 01:09 AM.
mercraus is offline   Reply With Quote