Tuesday, July 23, 2013

Playing with Bluetooth

Baby steps in development of bluetooth limit switches for CNC.

Materials used:
Arduino Uno
BlueSMiRF Gold
Breadboard & 4 connection wires
Android phone with Android Bluetooth Terminal installed



Code:
// basic Arduino sketch to demonstrate output based on serial data received from bluetooth
// the Arduino does not know this is Bluetooth, only that it is serial comms
// connect bluesmirf to VCC, GND, TX and RX

void setup(){
  //configure serial at 115200 baudrate
  Serial.begin(115200);
  //set arduino led on pin 13
  pinMode(13, OUTPUT);
}
void loop(){
  if(Serial.available()){
    //if serial comms are established, read characer received
    unsigned char charreceived = Serial.read();
    
    //determine operation based on character received
    switch(charreceived){
      case 'a':
        digitalWrite(13, HIGH);
        Serial.println("Pin 13 Led On");
        break;
      case 'b':
        digitalWrite(13, LOW);
        Serial.println("Pin 13 LED Off");
        break;
      case 'c':
        digitalWrite(13, HIGH);
        delay(1000);
        digitalWrite(13, LOW);
        Serial.println("Pin 13 LED Blink 1 second");
        break;
      default:
        break;
    }
    //we only want first character so flush remaining characters
    Serial.flush();
  }
  delay(10);
}

After pairing the phone with the bluesmirf, entering either text "a", "b" or "c" will affect the LED on Pin 13 of the Arduino, and will provide feedback to the phone.



No comments:

Post a Comment