从Nano通过蓝牙延迟到Android设备的串行通信太高

时间:2014-04-10 19:21:32

标签: android bluetooth delay

我很难从我的Arduino nano向我的Android应用程序发送串行数据。无论使用何种延迟,串行监视器都会显示正确的输出。然而,Android设备将不会收到 - 一致性 - 任何数据,至少延迟800.任何人都可以向我解释这一点,因为我不知所措。

该应用程序是在麻省理工学院的应用程序发明人开发的,工作正常。

非常感谢提前。这是我的代码。

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 


const int mcbpos  = 3;        //the pin that will sense the position of the MCB
const int tamper  = 4;        // the number of the tamper button pin
const int reset   = 5;        // the pin number of the reset button
const int emstop  = 6;        //the pin that will sense the emergency stop button

const int onled   = 11;      // the number of the on led pin
const int offled  = 12;      // the number of the off led pin
const int tamled  = 13;      // the numbr of the Tamper input

int i   = 0;
int pos = 0;              // variable to store the servo position 
int tam = 0;              // variable to store tamper switch status
int res = 0;              // variable to store reset switch status
int ems = 0;              // variable to store emergency stop status
int mcb = 0;
byte serialA;             //sets up the serial connection to the App

void setup() {

  Serial.begin(9600); //Setting up the Baud rate for the Bluetooth connection

  pinMode(mcbpos,INPUT);  //initialise mcb position sensor
  pinMode(tamper,INPUT);  //initialize the tamper switch as an input
  pinMode(reset,INPUT);    // initialise the reset button
  pinMode(emstop, INPUT); //  initialize the emergency stop pin as an intput

  pinMode(tamled, OUTPUT); // initialize the tamper LED pin as an output     
  pinMode(onled, OUTPUT); // initialize the ON LED pin as an output:   
  pinMode(offled, OUTPUT); //initialise the OFFLED as an output
  myservo.attach(9);      // attaches the servo on pin 9 to the servo object 
}

int onsub()                              //sets up the subroutine for moving the servo       to the on position
{
  for(pos = 90; pos > 50; pos -= 1)  // goes from 110 degrees to 135 degrees 
 {                                  // in steps of 1 degree 
  myservo.write(pos);              // tell servo to go to position in variable 'pos' 
 delay(15);                       // waits 15ms for the servo to reach the position 
 } 
 for(pos = 50; pos < 120; pos += 1)  // goes from 110 degrees to 135 degrees 
 {                                  // in steps of 1 degree 
  myservo.write(pos);              // tell servo to go to position in variable 'pos' 
  delay(15);                       // waits 15ms for the servo to reach the position 
} 
for(pos = 120; pos>=90; pos-=1)     // goes from 135 degrees to 110 degrees 
{                                
myservo.write(pos);              // tell servo to go to position in variable 'pos' 
delay(15);                       // waits 15ms for the servo to reach the position 
} 
}

int offsub()                          //sets up the subroutine for moving the servo to         the off position
{
for(pos = 80; pos>=50; pos-=1)     // goes from 180 degrees to 0 degrees 
{                                
myservo.write(pos);              // tell servo to go to position in variable 'pos' 
delay(15);                       // waits 15ms for the servo to reach the position 
} 
for(pos = 50; pos < 80; pos += 1)  // goes from 0 degrees to 180 degrees 
{                                  // in steps of 1 degree 
myservo.write(pos);              // tell servo to go to position in variable 'pos' 
delay(15);                       // waits 15ms for the servo to reach the position 
} 
}

void loop()
{

mcb = digitalRead(mcbpos);  //assign mcb position to variable mcb
tam = digitalRead(tamper);  //assign tamper signal position to variable tam
res = digitalRead(reset);  //assign reset switch position to variable res
ems = digitalRead(emstop);  //assign emergency stop switch position to variable ems

 digitalWrite(mcbpos, HIGH);    //Setting the pull up resistors for the swithches
digitalWrite(tamper, HIGH);    //Setting the pull up resistors for the swithches
digitalWrite(reset, HIGH);     //Setting the pull up resistors for the swithches
digitalWrite(emstop, HIGH);    //Setting the pull up resistors for the swithches


if (Serial.available() > 0) 
{
serialA = Serial.read();
Serial.println(serialA);
}


if (mcb == LOW)         //if the mcb is in the off position
{
digitalWrite(offled, HIGH);   //turn off the OFF LED
digitalWrite(onled, LOW);     //turns off the ON LED
Serial.println('0');          //send MCB position off to android
delay(800);  
}  
else
{
digitalWrite(offled, LOW);    //turn off the OFF LED
digitalWrite(onled, HIGH);    //turns off the ON LED
Serial.println('1');          //sends MCB position on to android
delay(800); 
} 


if (tam == HIGH)         //if Tamper is button is released-This is a safe to operate       feature                 HIGH is open switch
{
int y;                        //sets up a variable y
y = offsub();                //Calls the subroutine offsub  
while(digitalRead(reset) == HIGH) //Waits for the reset to be operated
{
  digitalWrite(tamled,HIGH);   //Tamper indicator is on 

  if (mcb == LOW)            //if the mcb is safe then
  {
    digitalWrite(offled, HIGH);   //turn on the OFF LED
    digitalWrite(onled, LOW);      //turns off the ON LED
    Serial.println('2');          //tampering evident
    delay(800);
  }
  else
  {
    digitalWrite(offled, LOW);   //turn on the OFF LED
    digitalWrite(onled, HIGH);      //turns off the ON LED
    Serial.println('3');      //sends 'Tampering evidence- Danger-unable to isolate' message
    delay(800);  
   }
 }
 digitalWrite(tamled, LOW);      //Tamper indicator turns off, return to loop
 }  


 if (ems == LOW)         //if Emergency stop button is pressed                 
 {
 int y;                        //sets up a variable
 y = offsub();                 //Calls the subroutine offsub  

  while(digitalRead(reset) == HIGH) //Waits for the reset to be operated
 {
  digitalWrite(tamled, HIGH);   //turn on the Tamper LED

  if (mcb == LOW)            //if the mcb is safe then
  {
    digitalWrite(offled, HIGH);   //turn on the OFF LED
    digitalWrite(onled, LOW);      //turns off the ON LED
    Serial.println('4');        //sends emergency stop operated 
    delay(800);  
  }
  else
  {
    digitalWrite(offled, LOW);   //turn on the OFF LED
    digitalWrite(onled, HIGH);      //turns off the ON LED
    Serial.println('5');        //sends 'emergency stop operated- Danger-unable to isolate' message
    delay(800); 
  }
 }
} 
digitalWrite(tamled, LOW);      //Tamper indicator turns off, return to loop 

if (serialA==9)         //if on button is pushed on App
{
digitalWrite(tamled, LOW);     //turn off the tamper LED
digitalWrite(onled, HIGH);    //turn on the LED
digitalWrite(offled, LOW);    //turn off the off led
int x;                        //sets up a variable
x=onsub();     //Calls the subroutine onsub
serialA==0;
}  

if (serialA==8)                 //check the status of the off button from App, if high then
{ 
digitalWrite(tamled, LOW);    //turn off the tamper led
digitalWrite(offled, HIGH);   //turn on the LED
digitalWrite(onled, LOW);     //turn off the on LED
int y;                        //sets up a variable
y = offsub();                 //Calls the subroutine offsub
serialA==0;
} 

}

0 个答案:

没有答案