Saturday 29 November 2014

Android-Arduino Controlled Eight-Relay Board

via Blue Tooth  Part II

  Arduino Code



Here is the Arduino Blue-tooth code to control the eight-relay board. The Android Java code is in the next post. You can control the relays using a smart-phone or tablet. In the meantime you can look at this post to see a basic Android Java Bluetooth project [http://gampageek.blogspot.co.uk/2013/12/set-paireddevices-mbluetoothadapter.html]

 ARDUINO
Start code:---------------------------------------------------------------------------------
 /* Blue tooth transceiver to trigger relays according to commands recieved CT 29/11/2014

// Sketch is provided "As is" with no guarantees, or support from the Author.
// Help with Arduino and shields can be found by joining the forum on the Arduino website: http://arduino.cc/en/

*/

// Includes here
#include <SoftwareSerial.h>

char sendReady = 'r'; // ready to send to Android

int bluetoothTx = 2;
int bluetoothRx = 3;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
const int LED = 10;
char incomingByte = ' ';


const int relay [8] = {
  3, 4, 5, 6, 7, 8, 9, 14};// relay digital control pins 3 to 9 and 14 (A0)
// ie relays 1 to 8. relay 8 = pin14 / A0 is firing power

const int alarm = 15;// alarm pin buzz 15 (A1)
const int powerLED = 16; //RED power led pin 16 (A2)
const int alarmGrnd = 17;// alarm pin buzz grnd 17 (A3)


byte command = ' ';//byte to hold a command char


word counter;           // incremented each second


boolean fired = false;

void setup ()
{
 
   //Setup  serial connection to computer
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
 
  //Setup Bluetooth serial connection to android
  bluetooth.begin(115200);
  bluetooth.print("$$$");
  delay(100);
  bluetooth.println("U,9600,N");
  bluetooth.begin(9600);
  Serial.println("Start");
 

  Serial.print("");//debug

  // alarms and notifications
  pinMode(alarm, OUTPUT);   
  pinMode(powerLED, OUTPUT);
  pinMode(alarmGrnd, OUTPUT);
 
  digitalWrite(alarmGrnd, LOW);// sink buzzer current

  // buzz 2 sec on power up
    buzz(2);
 
  // show power ON
  digitalWrite(powerLED, HIGH);//powerLED (red) on

  //setup relay control pins

  for (int i = 0; i < 8; i++)
  {
    pinMode(relay[i], OUTPUT); // set as outputs relay wired Normally Open (no circuit)
    digitalWrite(relay[i], HIGH);// set high = off (low triggers relay to close = make circuit)
  }
 
 // relay 8 = Firing-power set to ON
 
    digitalWrite(int(relay[7]), LOW); //relay [7](array) => pin 14 => relay-board_8
 
  //BT send ready signal

  //Serial.print(sendNull);// debug
 
  bluetooth.print(sendReady);// ready signal send to Android
  bluetooth.print('#');// delimiter
Serial.println();

 
//  delay (50000);// debug

}

void loop ()
{
  //start of BT code==================================
 
  // recieve code
 
  //Read from bluetooth
  if(bluetooth.available())
  {
    Serial.println("BlueTooth OK");// debug
    char toSend = (char)bluetooth.read();
    Serial.println(toSend);
    incomingByte = toSend;
    command = toSend;
  }
  //end of  of BT code==================================
 

 
  switch  (command) // start of SWITCH CASE ****************************************
  {
 
  case '*': // buzz warning
  buzz(2);
 
  break;
  
  case '1' : // relay 1
    
 
    digitalWrite(int(relay[0]), LOW); //relay [0](array) => pin 3 => relay-board_1 
  
    //Serial.println("Relay: ");// debug

    //Serial.print(int(relay[i]));//debug
    //Serial.print(" Triggered ");//debug
    //Serial.println();//debug
  
    break;

  case '2': // relay 2
 
  
    digitalWrite(int(relay[1]), LOW); //relay [1](array) => pin 4 => relay-board_2 
     
 
    break;

  case '3': // relay 3
 
 
 
    digitalWrite(int(relay[2]), LOW); //relay [2](array) => pin 5 => relay-board_3 
 
  
    break;   

  case '4': // relay 4
 
  
    digitalWrite(int(relay[3]), LOW); //relay [3](array) => pin 6 => relay-board_4
     
     
    break;

  case '5': // relay 5
 
  
    digitalWrite(int(relay[4]), LOW); //relay [4](array) => pin 7 => relay-board_5
      
  
    break;

  case '6': // relay 6
 
 
    digitalWrite(int(relay[5]), LOW); //relay [5](array) => pin 8 => relay-board_6
   
   
    break;

  case '7': // relay 7
 
 
    digitalWrite(int(relay[6]), LOW); //relay [6](array) => pin 9 => relay-board_7
   
  
    break;

 

  case '#': // Trigger all relays together
 
  
    
    for (int i = 0; i < 7; i++)
    {  
    digitalWrite(relay[i], LOW);// set all LOW = ALL ON (low triggers relay to close circuit)
    }
  
  
    break;

  default:// all OFF default HIGH all open (no circuit) eg 'X'
    for (int i = 0; i < 7; i++)
    {  
      digitalWrite(relay[i], HIGH);// set high = off (low triggers relay to close)
    }
 
  
    break;
  
  }// end of switch case********************************************************************

 

}// end of loop

void buzz(int multi){// buzz multi x 1 sec

  digitalWrite(alarm, HIGH);//buzz
 
    for (int i = 1; i < (multi * 50); i++) // wait
    {
      delayMicroseconds(10000);   // multi x 100 x 10000 us
    }
  digitalWrite(alarm, LOW);//buzz off
}

void delaySeconds (int multi){// delay multi x 1 sec

    for (int i = 1; i < (multi * 100); i++) // wait
    {
      delayMicroseconds(10000);   // multi x 100 x 10000 us
    }
}
 
 

End code:---------------------------------------------------------------------------------

No comments:

Post a Comment