Created
February 7, 2017 16:21
-
-
Save ajsdo222/462fa671889c36f9fa33038aa422dd96 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
SoftwareSerial BTSerial(2,3); // RX | TX | |
unsigned long lastTime; | |
unsigned long interval = 2000; | |
char texto[105]; | |
char *test; | |
char *test2; | |
char *id; | |
int h; | |
int REDPin = A0; | |
int GREENPin = A1; | |
int BLUEPin = A2; | |
int motorPin = 4; | |
void setup() | |
{ | |
pinMode(REDPin, OUTPUT); | |
pinMode(GREENPin, OUTPUT); | |
pinMode(BLUEPin, OUTPUT); | |
pinMode(motorPin, OUTPUT); | |
Serial.begin(9600); | |
Serial.println("Any device connected !"); | |
BTSerial.begin(38400); // HC-05 default speed in AT command more | |
// setting up the bluetooth device with AT command. | |
BTSerial.print("\r"); | |
delay(100); | |
BTSerial.print("AT+INIT\r\n"); | |
delay(100); | |
BTSerial.print("AT+IAC=9e8b33\r\n"); | |
delay(100); | |
BTSerial.print("AT+CLASS=0\r\n"); | |
delay(100); | |
BTSerial.print("AT+ROLE=1\r\n"); | |
delay(100); | |
BTSerial.print("AT+INQM=1,9,48\r\n"); | |
delay(100); | |
} | |
void loop() | |
{ | |
//searching bluetooth devices in range | |
delay(100); | |
BTSerial.print("AT+INIT\r\n"); | |
delay(100); | |
BTSerial.print("AT+IAC=9e8b33\r\n"); | |
delay(100); | |
BTSerial.print("AT+CLASS=0\r\n"); | |
delay(100); | |
BTSerial.print("AT+INQM=1,9,48\r\n"); | |
delay(100); | |
BTSerial.print("AT+INQ\r\n"); | |
delay(100); | |
h=BTSerial.available(); | |
if(h>=42){ //if it detects any bluetooth device, | |
id = readSerial(h); // read the signal. | |
Serial.write(id); | |
test=strstr(id,"24DB:ED:F9789F"); // extract the bluetooth ID. | |
if(test) | |
{ | |
analogWrite(REDPin, 255); | |
analogWrite(GREENPin, 0); | |
analogWrite(BLUEPin, 0); | |
digitalWrite(motorPin, LOW); | |
Serial.println("DEVICE CONNECTED"); | |
} | |
} | |
else if(h <= 21) //if it doesn't find any bluetooth signal | |
{ | |
test2 = strstr(id,"ERROR"); //see if it sends "ERROR" message. | |
if(test2) | |
{ | |
analogWrite(REDPin, 0); | |
analogWrite(GREENPin, 255); | |
analogWrite(BLUEPin, 255); | |
digitalWrite(motorPin, HIGH); | |
Serial.write(test2); | |
} | |
} | |
// Serial.println(count); | |
clearAll(); //Clear the remaining info | |
} | |
void clearAll() { | |
for(int i = 0; i < BTSerial.available(); i++) { | |
BTSerial.read(); | |
} | |
} | |
char* readSerial(int h) { | |
char input; | |
char buffer[100]; | |
if (BTSerial.available() > 0) { | |
for (int i = 0; i < h; i++) { // the id's start at char 21, se we copy from there | |
input = (char)BTSerial.read(); | |
buffer[i] = input; | |
buffer[i+1] = '\0'; | |
} | |
return buffer; | |
} else { | |
return "No Data"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment