joi, 16 ianuarie 2014

Smartphone ca telecomanda prin bluetooth pentru Arduino

   Dupa ce am intrat in posesia unui smartphone (telefon cu Android), am zis sa comand un Arduino conectat la un modul bluetooth cu ajutorul acestuia.
   In articolul Un modul bluetooth si Arduino am comandat placa Arduino sa aprinda un LED multicolor de la tastatura calculatorului, care avea conectat un "dongle" bluetooth.
   Telefonul meu, Allview A4ALL, are instalat un sistem de operare mai vechi, Android 2.3.6 si, de aceea, nu pot instala toate aplicatiile gasite.
   Dupa ceva teste, am reusit sa gasesc aplicatia Arduino Bluetooth Controller scrisa de cei de la http://estacado-s-ltd.android.informer.com/
 
   Modulul bluetooth HC-06 l-am conectat ca si in cazul anterior:
   Dupa conectarea modulului bluetooth si a celui cu LED-ul multicolor, am activat modul de transmisie bluetoth la telefon, am deschis aplicatia de comanda, am realizat conexiunea cu Arduino si apoi am ales partea cu "Terminal Mode", deoarece am nevoie sa trimit cifre, cum aveam eu in sketch.
 
 
 
 
 - am apasat "tasta" 1 si s-a aprins LED-ul in rosu:
 - am apasat "tasta" 2 si s-a aprins LED-ul in albastru:
  - am apasat "tasta" 3 si s-a aprins LED-ul in verde:
   Sketch-ul folosit de mine este:
// adapted sketch from http://english.cxem.net/arduino/arduino4.php
// by niq_ro from http://www.tehnic.go.ro &
// http://nicuflorica.blogspot.com
// version 4.0 by Nicu FLORICA - 04.2013, Craiova - ROMANIA
// version 4.1 by Nicu FLORICA - 05.2013, Craiova - ROMANIA
// version 4.1.1 by Nicu FLORICA - 01.2014, Craiova - ROMANIA

char incomingByte;  // incoming data
int  LED1 = 11;      // red LED pin 
int  LED2 = 10;      // blue LED pin 
int  LED3 = 9;      // green LED pin 
 
void setup() {
  Serial.begin(9600); // initialization
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  Serial.println("Press 1 to 7 or 0 to RGB LED ON or OFF...");
  Serial.println("adapted sketch by niq_ro from http://nicuflorica.blogspot.com");
delay(500);


}
 
void loop() {
  
  if (Serial.available() > 0) {  // if the data came
  
    Serial.println("--------------------------------------------");  // print message
     Serial.println("press 0 tp LEDs OFF.");  // print message
       Serial.println("Press 1 to RED LED ON!");
         Serial.println("Press 2 to BLUE LED ON!");
           Serial.println("Press 3 to GREEN LED ON!");
             Serial.println("Press 4 to RED & BLUE LEDs ON!");
               Serial.println("Press 5 to RED & GREEN LEDs ON!");
                 Serial.println("Press 6 to BLUE & GREEN LEDs ON!");
                   Serial.println("Press 7 to RED & BLUE & GREEN LEDs ON!");
Serial.println("--------------------------------------------");  // print message
Serial.println("  ");  // print message


    incomingByte = Serial.read(); // read byte
    if(incomingByte == '0') {
       digitalWrite(LED1, HIGH); // if 0, switch LED on
       digitalWrite(LED2, HIGH); // if 0, switch LED on 
       digitalWrite(LED3, HIGH); // if 0, switch LED on 
       delay (100);
       digitalWrite(LED1, LOW);  // if 1, switch LED Off
       digitalWrite(LED2, LOW);  // if 1, switch LED Off
       digitalWrite(LED3, LOW);  // if 1, switch LED Off
Serial.println("--------------------------------------------");  // print message      
       Serial.println("All LEDs are OFF.");  // print message
       Serial.println("Press 1 to RED LED ON!");
         Serial.println("Press 2 to BLUE LED ON!");
           Serial.println("Press 3 to GREEN LED ON!");
             Serial.println("Press 4 to RED & BLUE LEDs ON!");
               Serial.println("Press 5 to RED & GREEN LEDs ON!");
                 Serial.println("Press 6 to BLUE & GREEN LEDs ON!");
                   Serial.println("Press 7 to RED & BLUE & GREEN LEDs ON!");
       }
    if(incomingByte == '1') {
      // first all LEDs will be OFF 
       digitalWrite(LED1, LOW);  // if 1, switch LED Off
       digitalWrite(LED2, LOW);  // if 1, switch LED Off
       digitalWrite(LED3, LOW);  // if 1, switch LED Off
       digitalWrite(LED1, HIGH); // if 0, switch LED on
       Serial.println("RED LED is now ON. Press 0 to have all LED OFF!");
    }
    if(incomingByte == '2') {
       digitalWrite(LED1, LOW);  // if 1, switch LED Off
       digitalWrite(LED2, LOW);  // if 1, switch LED Off
       digitalWrite(LED3, LOW);  // if 1, switch LED Off
       digitalWrite(LED2, HIGH); // if 0, switch LED on
       Serial.println("BLUE LED is now ON. Press 0 to have all LED OFF!");
    }
    if(incomingByte == '3') {
       digitalWrite(LED1, LOW);  // if 1, switch LED Off
       digitalWrite(LED2, LOW);  // if 1, switch LED Off
       digitalWrite(LED3, LOW);  // if 1, switch LED Off
       digitalWrite(LED3, HIGH); // if 0, switch LED on
       Serial.println("GREEN LED is now ON. Press 0 to have all LED OFF!");
    }
    if(incomingByte == '4') {
       digitalWrite(LED1, LOW);  // if 1, switch LED Off
       digitalWrite(LED2, LOW);  // if 1, switch LED Off
       digitalWrite(LED3, LOW);  // if 1, switch LED Off
       digitalWrite(LED1, HIGH); // if 0, switch LED on
       digitalWrite(LED2, HIGH); // if 0, switch LED on 
       Serial.println("RED & BLUE LEDs is now ON. Press 0 to have all LED OFF!");
    }
    if(incomingByte == '5') {
       digitalWrite(LED1, LOW);  // if 1, switch LED Off
       digitalWrite(LED2, LOW);  // if 1, switch LED Off
       digitalWrite(LED3, LOW);  // if 1, switch LED Off
       digitalWrite(LED1, HIGH); // if 0, switch LED on
       digitalWrite(LED3, HIGH); // if 0, switch LED on 
       Serial.println("RED & GREEN LEDs is now ON. Press 0 to have all LED OFF!");
    }
    if(incomingByte == '6') {
       digitalWrite(LED1, LOW);  // if 1, switch LED Off
       digitalWrite(LED2, LOW);  // if 1, switch LED Off
       digitalWrite(LED3, LOW);  // if 1, switch LED Off
       digitalWrite(LED2, HIGH); // if 0, switch LED on
       digitalWrite(LED3, HIGH); // if 0, switch LED on 
       Serial.println("BLUE & GREEN LEDs is now ON. Press 0 to have all LED OFF!");
    }
    if(incomingByte == '7') {
       digitalWrite(LED1, LOW);  // if 1, switch LED Off
       digitalWrite(LED2, LOW);  // if 1, switch LED Off
       digitalWrite(LED3, LOW);  // if 1, switch LED Off
       digitalWrite(LED1, HIGH); // if 0, switch LED on
       digitalWrite(LED2, HIGH); // if 0, switch LED on 
       digitalWrite(LED3, HIGH); // if 0, switch LED on 
       Serial.println("RED & BLUE & GREEN LEDs is now ON. Press 0 to have all LED OFF!");
    }
   Serial.println("--------------------------------------------");  // print message
  }
}
   Am facut si 2 filmulete:
smartphone ca telecomanda prin bluetooth pentru Arduino
smartphone as bluetooth remote for Arduino

19.01.2013
   Am modifiat sketch-ul un pic sa "reactioneze" si la litere, adica daca transmit "q" sau "1" sa aprinda LED-ul rosu, daca transmit "w" sau "2" sa aprinda LED-ul albastru, daca transmit "e" sau "3" sa aprinda LED-ul verde, iar la "p" sau "0" sa se stinga toate, vedeti mai bine in filmuletul smartphone ca telecomanda prin bluetooth pentru Arduino (II)

Un comentariu: