Вы находитесь на странице: 1из 9

void setup()

{
//Pin donde conectamos el led para ver el correcto funcionamiento
del modulo
pinMode(13,OUTPUT);
//Configuracion de la velocidad del modulo 9600 por defecto, se
puede cambiar
//mediante comandos AT
Serial.begin(9600);
}

void loop()
{
//Mientras el puerto serie del modulo bluetooth esta disponible
while (Serial.available())
{
//Guardamos en la variable dato el valor leido por el modulo
bluetooth
char dato= Serial.read();
//Comprobamos el dato
switch(dato)
{
//Si recibimos una 'w' encendemos el led 13 y enviamos para
mostrar
//en Blueterm Led encendido
case 'w':
{
digitalWrite(13,HIGH);
Serial.println("Led encendido");
break;
}
//Si recibimos una 'e' apagamos el led 13 y enviamos para
mostrar
//en Blueterm Led apagado
case 'e':
{
digitalWrite(13,LOW);
Serial.println("Led apagado");
break;
}
//Si recibimos una 'r' encendemos y apagamos el led mediante
la secuencia
//programa y mostramos en Blueterm Led intermitente
case 'r':
{
digitalWrite(13,HIGH);
delay(200);
digitalWrite(13,LOW);
delay(200);
digitalWrite(13,HIGH);
delay(100);
digitalWrite(13,LOW);
delay(100);
Serial.println("Led intermitente");
break;
}
}
}
}
#include <SoftwareSerial.h>

SoftwareSerial bt(10, 11); // RX, TX se declara un objeto Bluetooth en esos pines

void setup(){
Serial.begin(9600); //Baud rate que debe estar el puerto serial del IDE de arduino
bt.begin(38400); //BT default baud rate
Serial.println("Configuracion");
}

void loop(){
if (bt.available())
Serial.write(bt.read());
if (Serial.available())
bt.write(Serial.read());
}
/*
Incluimos las libreria SoftwareSerial.h para crear dos puertos virtuales que
seran el 7 y 8
creado el 19 de feb de 2014
modificado el 09 de oct de 2015 by Cas6678
*/

#include <SoftwareSerial.h>
SoftwareSerial Serial3(7,8); // Pin 7 y 8 para TX y RX

void setup()
{
Serial3.begin(9600); //
Serial.begin(9600);
Serial.println("Empieza delay");
delay(10000);
Serial.println("Termina delay");
Serial3.print("AT");
//Espera de 1 segundo segn datasheet entre envio de comandos AT
delay(1000);
//Cambio de nombre donde se envia AT+NAME y seguido el nombre que deseemos
Serial3.print("AT+NAMEMIBTHC06"); // para llamarlo MIBTHC06
//Espera de 1 segundo segn datasheet entre envio de comandos AT
delay(1000);
/*Cambio de la velocidad del modulo en baudios
Se envia AT+BAUD y seguido el numero correspondiente:

1 -- 1200 baudios
2 -- 2400 baudios
3 -- 4800 baudios
4 -- 9600 baudios (por defecto)
5 -- 19200 baudios
6 -- 38400 baudios
7 -- 57600 baudios
8 -- 115200 baudios

*/
Serial3.print("AT+BAUD6"); // para subirle la velocidad a 38400
//Espera de 1 segundo segn datasheet entre envio de comandos AT
delay(1000);
//Configuracion Password, se envia AT+PIN y seguido password que queremos
Serial3.print("AT+PIN1111"); // para ponerle el pin 1111
//Espera de 1 segundo segn datasheet entre envio de comandos AT
delay(1000);
//Mostramos tanto por puerto serial y por led la finalizacion de la
//configuracion AT del modulo bluetooth
Serial.println("TERMINADO");

}
void loop()
{ }
Circuit magic

#include <SoftwareSerial.h>

int bluetoothTx = 2; // TX-O pin of bluetooth mate, Arduino D2

int bluetoothRx = 3; // RX-I pin of bluetooth mate, Arduino D3

int led = 13;

int buttonPin1 = 7;

int buttonPin2 = 8;

int button1State = 0;

int button2State = 0;

int dataFromBt;

boolean lightBlink = false;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()

Serial.begin(9600); // Begin the serial monitor at 9600bps

bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps

bluetooth.print("$"); // Print three times individually

bluetooth.print("$");

bluetooth.print("$"); // Enter command mode

delay(100); // Short delay, wait for the Mate to send back CMD

bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity

// 115200 can be too fast at times for NewSoftSerial to relay the data reliably

bluetooth.begin(9600); // Start bluetooth serial at 9600


pinMode(led, OUTPUT);

pinMode(buttonPin1, INPUT);

pinMode(buttonPin2, INPUT);

void loop()

if (bluetooth.available()) // If the bluetooth sent any characters

// Send any characters the bluetooth prints to the serial monitor

Serial.println((char)bluetooth.read());

dataFromBt = bluetooth.read();

//Serial.println(dataFromBt);

if (dataFromBt == '1') {

Serial.println("led on");

digitalWrite(led, HIGH);

bluetooth.print("1");

if (dataFromBt == '0') {

Serial.println("led off");

digitalWrite(led, LOW);

bluetooth.print("0");

if (dataFromBt == 'b') {

Serial.println("a");

lightBlink = true;

} else {

lightBlink = false;
}

if (Serial.available()) // If stuff was typed in the serial monitor

// Send any characters the Serial monitor prints to the bluetooth

//String myStr = (String)Serial.read();

//char myStr1[] = "hello this is testing!";

// uint8_t payload[myStr.length() + 1];

// myStr.getBytes(payload, myStr.length()+1);

int bytes=Serial.available();

//Serial.readBytes(buffer, startPosition, bytes);

bluetooth.print((char)Serial.read());

// and loop forever and ever!

if (lightBlink) {

digitalWrite(led, HIGH);

bluetooth.print("1");

Serial.println("HIGH");

delay(500);

digitalWrite(led, LOW);

bluetooth.print("0");

Serial.println("LOW");

delay(500);

}
//------arduino push button code----------------

button1State = digitalRead(buttonPin1);

button2State = digitalRead(buttonPin2);

if (button1State == HIGH) {

digitalWrite(led, HIGH);

bluetooth.print("1");

Serial.println("on");

if (button2State == HIGH) {

digitalWrite(led, LOW);

Serial.println("off");

bluetooth.print("0");

}
Para varios led

char data = 0; //Variable for storing received data


void setup()
{
Serial.begin(9600); //Sets the baud for serial data
transmission
pinMode(13, OUTPUT); //Sets digital pin 13 as output pin
}
void loop()
{
if(Serial.available() > 0) // Send data only when you receive
data:
{
data = Serial.read(); //Read the incoming data & store
into data
Serial.print(data); //Print Value inside data in Serial
monitor
Serial.print("\n");
if(data == '1') // Checks whether value of data is
equal to 1
digitalWrite(3, HIGH); //If value is 1 then LED turns ON
else if(data == '0') // Checks whether value of data is
equal to 0
digitalWrite(3, LOW); //If value is 0 then LED turns OFF

if(data == '3')
digitalWrite(4, HIGH);
else if(data == '2')
digitalWrite(4, LOW);

if(data == '5')
digitalWrite(5, HIGH);
else if(data == '4')
digitalWrite(5, LOW);

if(data == '7')
digitalWrite(6, HIGH);
else if(data == '6')
digitalWrite(6, LOW);

if(data == '9')
digitalWrite(7, HIGH);
else if(data == '8')
digitalWrite(7, LOW);

if(data == 'a')
digitalWrite(8, HIGH);
else if(data == 'b')
digitalWrite(8, LOW);

if(data == 'c')
digitalWrite(9, HIGH);
else if(data == 'd')
digitalWrite(9, LOW);

if(data == 'e')
digitalWrite(10, HIGH);
else if(data == 'f')
digitalWrite(10, LOW);

if(data == 'g')
digitalWrite(11, HIGH);
else if(data == 'h')
digitalWrite(11, LOW);

if(data == 'i')
digitalWrite(12, HIGH);
else if(data == 'j')
digitalWrite(12, LOW);

if(data == 'k')
digitalWrite(13, HIGH);
else if(data == 'l')
digitalWrite(13, LOW);
}
}

Вам также может понравиться