Skip to content

Instantly share code, notes, and snippets.

@fede1608
Last active July 6, 2016 02:22
Show Gist options
  • Save fede1608/1cea6553745a96ee9080b03a0ac0eecf to your computer and use it in GitHub Desktop.
Save fede1608/1cea6553745a96ee9080b03a0ac0eecf to your computer and use it in GitHub Desktop.
Arduino Bluetooth
#include <NewPing.h>
#include <Servo.h>
#include <SoftwareSerial.h>
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 53
#define RST_PIN 5
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
SoftwareSerial BT1(11, 10); // RX | TX
#define START_CMD_CHAR '*'
#define END_CMD_CHAR '#'
#define DIV_CMD_CHAR '|'
#define CMD_DIGITALWRITE 10
#define CMD_ANALOGWRITE 11
#define CMD_TEXT 12
#define CMD_READ_ARDUDROID 13
#define MAX_COMMAND 20 // max command number code. used for error checking.
#define MIN_COMMAND 10 // minimum command number code. used for error checking.
#define IN_STRING_LENGHT 40
#define MAX_ANALOGWRITE 255
#define PIN_HIGH 3
#define PIN_LOW 2
String inText;
Servo myservo;
NewPing sonar1(32, 3, 40);
NewPing sonar2(22, 2, 40);
NewPing sonarFloor(42, 4, 15);
bool barrierOpened = true;
bool lastFloorCheck = false;
void setup()
{
Serial.begin(9600);
BT1.begin(9600);
myservo.attach(9);
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
}
void loop() {
/*
if (BT1.available())
Serial.write(BT1.read());
if (Serial.available())
BT1.write(Serial.read());
return; */
// ............................................
// Serial.flush();
int ard_command = 0;
int pin_num = 0;
int pin_value = 0;
char get_char = ' '; // Para leer BT1
if (BT1.available()){
get_char = BT1.read();
// Serial.println(get_char);
delay(25);
if (get_char != START_CMD_CHAR)
return; // Si no hay comando, vuelta a empezar
ard_command = BT1.parseInt(); // Leemos la orden
pin_num = BT1.parseInt(); // Leemos el pin
pin_value = BT1.parseInt(); // Leemos valor
Serial.print("Comando disponible "); // Hay comandos disponibles
Serial.println(ard_command);
if (ard_command == CMD_TEXT) // Si el comando es de texto:
{ //processInText();
String s = GetLine();
Serial.println(s);
}
if (ard_command == CMD_READ_ARDUDROID) {
// BT1.print("{\"est1\":");
BT1.print("1: ");
BT1.print(readUltrasonic(sonar1));
BT1.print(" 2: ");
// BT1.print(", \"est2\":");
BT1.println(readUltrasonic(sonar2));
// BT1.println("}");
return; // Done. return to loop();
}
if (ard_command == CMD_DIGITALWRITE) {
Serial.println(pin_value);
// processDW(pin_num, pin_value);
bool prevState = barrierOpened;
if(pin_value == 2){
// barrierOpened=true;
closeBarrier();
}else{
// barrierOpened=false;
openBarrier();
}
barrierOpened = prevState;
return;
}
if (ard_command == CMD_ANALOGWRITE)
{
// analogWrite( pin_num, pin_value );
// Serial.println((((double)pin_value / 255) * 180));
// BT1.println((((double)pin_value / 255) * 180));
// myservo.write((int)(((double)pin_value / 255) * 180));
// add your code here
return; // De vuelta alloop();
}
}
int floorPing = sonarFloor.ping_median(10);
Serial.print("Floor ping: ");
Serial.println(floorPing);
if(floorPing > 0 && floorPing < 600){
if(lastFloorCheck){
openBarrier();
}
lastFloorCheck = true;
}else{
lastFloorCheck = false;
closeBarrier();
}
if ( rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()){
Serial.print("RFID Card read");
printHex(rfid.uid.uidByte, rfid.uid.size);
Serial.println();
openBarrier();
}
delay(100);
}
void processDW(int pin_num, int pin_value) {
if (pin_value == PIN_LOW)
pin_value = LOW;
else if (pin_value == PIN_HIGH)
pin_value = HIGH;
else
return; // error in pin value. return.
Serial.println(pin_value);
Serial.println(pin_num);
digitalWrite( pin_num, pin_value);
return;
}
String readUltrasonic(NewPing sonar) {
if (sonar.ping_cm() > 7) {
return "Libre";
} else if (sonar.ping_cm() > 0){
return "Ocup.";
}
return "Libre";
}
void closeBarrier(){
if(!barrierOpened) return;
barrierOpened = false;
delay(800);
for (int pos = 95; pos <= 180; pos += 2) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(10); // waits 15ms for the servo to reach the position
}
}
void openBarrier(){
if(barrierOpened) return;
barrierOpened = true;
for (int pos = 180; pos >= 95; pos -= 2) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(10); // waits 15ms for the servo to reach the position
}
}
String GetLine() {
String S = "" ;
if (BT1.available())
{ char c = BT1.read(); ;
while ( c != END_CMD_CHAR) //Hasta que el caracter sea END_CMD_CHAR
{ S = S + c ;
delay(25) ;
c = BT1.read();
}
return ( S ) ;
}
}
void printHex(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment