如何通过蓝牙将数据从Arduino发送到Android(App发明者)

时间:2018-04-19 01:54:07

标签: android arduino

我正在为我的大学制作一个项目。这是关于控制房子的锁定以及Android手机的照明。这是Arduino代码。

#include <Servo.h> //Includes servo motor library
char data = 0; //Variable data of type char we will receive these characters from Android device

Servo myservo; //declaring a servo motor
int pos = 0; //default position of lock
int led = 13; //attaching red led for locked position on pin 13
int led2 = 12; //attaching green led for unlocked position on pin 12

int led3 = 7; //attaching lighting of room1 to digital pin 7
int led4 = 4; //attaching lighting of room2 to digital pin 4
int led5 = 8; //attaching lighting of room3 to digital pin 8
int led6 = 2; //attaching lighting of room4 to digital pin 2

void setup() {
  myservo.attach(9); //attaching servo motor to pin 9
  Serial.begin(9600); //Sets the data rate in bits per second (baud) for serial data transmission.
  pinMode(led, OUTPUT); //The code makes the digital pin 13 OUTPUT and toggles it HIGH and LOW.
  pinMode(led2, OUTPUT); //The code makes the digital pin 12 OUTPUT and toggles it HIGH and LOW.
  pinMode(led3, OUTPUT); //The code makes the digital pin 7 OUTPUT and toggles it HIGH and LOW.
  pinMode(led4, OUTPUT); //The code makes the digital pin 4 OUTPUT and toggles it HIGH and LOW.
  pinMode(led5, OUTPUT); //The code makes the digital pin 8 OUTPUT and toggles it HIGH and LOW.
  pinMode(led6, OUTPUT); //The code makes the digital pin 2 OUTPUT and toggles it HIGH and LOW.
}

void loop() {
  if (Serial.available() > 0) {
    // Get the number of bytes (characters) available for reading from the serial port.
    data = Serial.read(); //Reads incoming serial data
    Serial.print(data); //Prints data to the serial port as human-readable ASCII text.
    Serial.print("\n"); //new line
    if (data == '1') {
      myservo.write(45);
      digitalWrite(led, HIGH);
      //When data = 1 rotate the motor and turn off green light and turn on red light (LOCK the door)
      digitalWrite(led2, LOW);
      pos = 1;
    }
    if (data == '0') {
      myservo.write(90);
      digitalWrite(led, LOW);
      //When data = 0 rotate the motor and turn on green light and turn off red light (UNLOCK the door)
      digitalWrite(led2, HIGH);
      pos = 0;
    }
    if (data == 'G')
      digitalWrite(led3, HIGH); //When data = G turn on lighting for room 1
    else  if (data == 'R')
      digitalWrite(led3, LOW); //When data = R turn off lighting for room 1
    if (data == 'K')
      digitalWrite(led4, HIGH); //When data = K turn on lighting for room 2
    else if (data == 'L')
      digitalWrite(led4, LOW);  //When data = L turn off lighting for room 2
    if (data == 'M')
      digitalWrite(led5, HIGH); //When data = M turn on lighting for room 3
    else if (data == 'N')
      digitalWrite(led5, LOW); //When data = N turn off lighting for room 3
    if (data == 'O')
      digitalWrite(led6, HIGH); //When data = O turn on lighting for room 4
    else if (data == 'P')
      digitalWrite(led6, LOW);  //When data = P turn off lighting for room 4
  }
}

Android应用程序是使用App Inventor构建的,它可以正常工作,但有一件事......我希望Arduino将pos发送到Android应用程序然后更改标签以锁定或解锁。

1 个答案:

答案 0 :(得分:0)

使用蓝牙将数据从Arduino发送到Android的代码

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println(pos);
  delay(20);
}

MIT App发明人阻止通过蓝牙从Arduino接收数据

MIT App inventor blocks to receive data from Arduino through Bluetooth