You are on page 1of 7

#include <SoftwareSerial.

h>
#include <TimerOne.h>
#define Rx 2
#define Tx 3
#define seconds 4
char serialData; // Ser
ial data memory
int counter1; // Ove
rflow memory
int action; // Act
ion trigger
int once = 0; // Run
only once
SoftwareSerial Bluetooth(Rx, Tx);
void setup(){
pinMode(Tx, OUTPUT); // Confi
gure Tx as OUTPUT (Transmitter)
pinMode(Rx, INPUT);
delay(1000);
Bluetooth.begin(9600);
Serial.begin(1200);
delay(1000);
Serial.print("Bluetooth ready");
Bluetooth.flush();
TimerOneSetup(); // Run
the setup for Timer One
for(int i = 5; i <= 8; i++){ // Mak
e pins 5, 6, 7 and 8 OUTPUT
pinMode(i, OUTPUT);
}
}
void interrupt1(){ // Tim
er One loop
counter1++; // Cou
nt the amount of seconds has passed
if (counter1 == seconds){ // Tri
gger the next action after a several amount of seconds (Default: 4 seconds)
action++;
counter1 = 0;
}
if (action > 3){ // Res
et action trigger when after all actions were runned
action = 0;
}
}
void loop(){ // End
less loop
if (Serial.available()){ // Wai
t for data recieved from Local device
serialData = Serial.read(); // Put
recieved data in memory
Serial.print("Data recieved from Local device: ");
Serial.println(serialData);
}
if (Bluetooth.available()){ // Wai
t for data recieved from Bluetooth device
serialData = Bluetooth.read(); // Put
recieved data in memory
Serial.print("Data recieved from Bluetooth device: ");
Serial.print(serialData);
}
if (once == 0){ // Thi
s script will be run only once
serialData = 'e'; // Put
switch on automatic on startup
once++; // Get
into the next stage which may be run only once
}
switch(serialData){ // Per
form action on state of the switch
case 'a':
fourtyLeds(); // Sho
w the 40 Leds version of the Luma Mini
break;
case 'b':
thirtyLeds(); // Sho
w the 30 Leds version of the Luma Mini
break;
case 'c':
twentyLeds(); // Sho
w the 20 Leds version of the Luma Mini
break;
case 'd':
twelveLeds(); // Sho
w the 12 Leds version of the Luma Mini
break;
case 'e':
while(serialData == 'e'){ // Kee
p changing different Led versions of the Luma Mini automatically
switch(action){
case 0: // Wai
t for the action trigger to hit the first action
fourtyLeds(); // Sho
w the 40 Leds version of the Luma Mini
break;
case 1: // Wai
t for the action trigger to hit the second action
twelveLeds(); // Sho
w the 12 Leds version of the Luma Mini
break;
case 2: // Wai
t for the action trigger to hit the third action
twentyLeds(); // Sho
w the 20 Leds version of the Luma Mini
break;
case 4: // Wai
t for the action trigger to hit the fourth action
thirtyLeds(); // Sho
w the 30 Leds version of the Luma Mini
break;}
if (Serial.read() == 'f'){ // Wai
t for data recieved from Local device
serialData = Serial.read(); // Put
recieved data in memory
Serial.print("Data recieved from Local device: ");
Serial.println(serialData);
}
if (Bluetooth.read() == 'f'){ // Wai
t for data recieved from Bluetooth device
serialData = Bluetooth.read(); // Put
recieved data in memory
Serial.print("Data recieved from Bluetooth device: ");
Serial.println(serialData);
}
break;
}
}
}
void BluetoothSetup(){
pinMode(Tx, OUTPUT); // Con
figure Tx as OUTPUT (Transmitter)
pinMode(Rx, INPUT); // Con
figure Rx as INPUT (Reciever)
Bluetooth.begin(9600); // Set
Bluetooth baud rate to default baud rate 38400
Bluetooth.print("\r\n+STWMOD=0\r\n"); // Set
the Bluetooth to work in slave mode
Bluetooth.print("\r\n+STNA=Luma Mini\r\n"); // Set
Bluetooth name to Luma Mini
Bluetooth.print("\r\n+STOAUT=1\r\n"); // Per
mit paired device to connect
Bluetooth.print("\r\n+STAUTO=0\r\n"); // Aut
o-connection should be forbidden here
delay(2000);
Bluetooth.print("\r\n+INQ=1\r\n"); // Mak
e this Bluetooth Slave inquirable
Serial.println("The slave Bluetooth is inquirable!");
delay(2000);
Bluetooth.flush();
}
void TimerOneSetup(){
Timer1.initialize(1000000); // Ini
tialize Timer One for an overflow exactly every 1 second
Timer1.attachInterrupt(interrupt1); // Ope
n the Timer One loop
}
void fourtyLeds(){ // Sho
w the 40 Leds version of the Luma Mini
for(int i = 5; i <= 8; i++){
digitalWrite(i, LOW);
}
}
void thirtyLeds(){ // Sho
w the 30 Leds version of the Luma Mini
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
}
void twentyLeds(){ // Sho
w the 20 Leds version of the Luma Mini
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
}
void twelveLeds(){ // Sho
w the 12 Leds version of the Luma Mini
for (int i = 5; i <= 8; i++){
digitalWrite(i, HIGH);
}
}
RECIBIR DATOS
#include <SoftwareSerial.h>
char rec;
SoftwareSerial blue(2,3);
void setup(){
pinMode(13,OUTPUT);
blue.begin(9600);
blue.println("Conectado");
}
void loop() {
if(blue.available()){
rec=blue.read();
switch(rec){
case '0':
digitalWrite(13,LOW);
delay(500);
blue.println("Led off");
break;
case '1':
digitalWrite(13,HIGH);
delay(500);
blue.println("Led on");
break;
default:
delay(500);
blue.print(rec);
blue.println(" no es una orden valida. Introduzca 0 o 1");
}
}
}
ENVIAR DATOS
//Enviar DATOS con modulo Bluetooth para Arduino http://zygzax.com/
#include <SoftwareSerial.h>
SoftwareSerial blue(2, 3);
int pot=0;
void setup(){
blue.begin(9600);
blue.println("Conectado");
}
void loop(){
pot=analogRead(0);
blue.println(pot);
delay(1000);
}
SENSOR PING
/*
HC-SR04 Ping distance sensor:
VCC to arduino 5v
GND to arduino GND
Echo to Arduino pin 7
Trig to Arduino pin 8

This sketch originates from Virtualmix: http://goo.gl/kJ8Gl
Has been modified by Winkle ink here: http://winkleink.blogspot.com.au/2012/05/
arduino-hc-sr04-ultrasonic-distance.html
And modified further by ScottC here: http://arduinobasics.blogspot.com.au/2012/
11/arduinobasics-hc-sr04-ultrasonic-sensor.html
on 10 Nov 2012.
*/
#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin 13 // Onboard LED
int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}
void loop() {
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);

//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;

if (distance >= maximumRange || distance <= minimumRange){
/* Send a negative number to computer and Turn LED ON
to indicate "out of range" */
Serial.println("-1");
digitalWrite(LEDPin, HIGH);
}
else {
/* Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
Serial.println(distance);
digitalWrite(LEDPin, LOW);
}

//Delay 50ms before next reading.
delay(50);
}
OTRO..... PING
#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop() {
long duration, distance, alert;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
alert = (distance*4);
if (distance < 150) { // This is where the LED On/Off happens
digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should t
urn off
digitalWrite(led2,LOW);
delay(alert/4);
digitalWrite(led,LOW);
digitalWrite(led2,HIGH);
}
else {
digitalWrite(led,LOW);
digitalWrite(led2,HIGH);
}
if (distance < 50) { // This is where the LED On/Off happens
digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should t
urn off
digitalWrite(led2,LOW);
}
if (distance >= 0 || distance <= 150){
Serial.println("Out of RANGE");
}
else {
Serial.print(distance);
Serial.println(" cm");
Serial.print("\t Alert time ");
Serial.print(alert);
Serial.println(" ms");
}
delay(alert/2);
}

You might also like