简体   繁体   中英

USB Serial for Android - problems sending/receiving data with Arduino Uno R3

I'm trying to send a command such as "c" or "s" to the Arduino from my Android applicaton. The device is recognized, but I'm unsure if I'm sending/receiving the data correctly. I'm using the USBSerialforAndroid library.

When I tap my buttons to start and stop the measurements, the toast notifications pop up to signify that they are going through, but when data is "received" I'm getting back either "7.00" or "0.00", which I know are wrong. I feel like it may be within my Arduino code, or how I'm sending bytes from the app.

Here is the relevant Android code:

startMeasurement.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
      if (selectedSensorType.equals("grade")) {
         //do nothing
      } else if (selectedSensorType.equals("height")) {
          startCode = "c";

          heightStart = serialUser(startCode); //get heightFloor from Arduino            
      } else if (selectedSensorType.equals("distance")) {
          startCode ="s";

          distanceStart = 0;            
      } 
  }
});

saveMeasurement.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
      if (selectedSensorType.equals("grade")) {
         check.setText(String.format("%.2f", outputGrade));
      } else if (selectedSensorType.equals("height")) {
         endCode = "c";
         heightEnd = serialUser(endCode); //get heightCurb from Arduino

         double measuredHeight = heightEnd - heightStart;
         check.setText(String.format("%.2f", measuredHeight));
      } else if (selectedSensorType.equals("distance")) {
         endCode = "e";
         distanceEnd = serialUser(endCode); //get distanceEnc from Arduino

         double measuredDistance = distanceEnd - distanceStart;
         check.setText(String.format("%.2f", measuredDistance));
      } 
   }
});

Here is my serialUser() method:

private double serialUser(String command) {
        double output = 0;
        if (mSerialDevice != null) {
            byte[] commandArray = command.getBytes();
            byte[] returnArray = new byte[8];

            try {
                mSerialDevice.setBaudRate(115200);
                mSerialDevice.write(commandArray, 1000);
                output = mSerialDevice.read(returnArray, 2500);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return output;
    }

And here is my Arduino code:

void setup()
{
  Serial.begin(115200);
  Dist.begin(2,3);
}

const double Pi = 3.14159265359; // 3.141593, set to 1 for testing purposes
double encoderPosition  = -999;

void loop()
{
  double newPosition = abs(myEnc.read()); // gets raw data, 1440 ticks = 1 rev
  double revs = newPosition/(double)960; // calculates number of revs
  double diameter = 15; // specify wheel diameter
  double distanceEnc = diameter*Pi*revs; // calculates distance 
  double heightDiff;
  int timesum = 0;

  distanceEnc = round(distanceEnc*1000.0l)/1000.0l; // round distance to 3 decimal places

  if (newPosition != encoderPosition) { // write new position to serial
    encoderPosition = newPosition;     
    //Serial.println(distanceEnc,3);
    //Serial.println(revs,3);
  }

  while (Serial.available()) { // if a character is sent to the device, the encoder reading resets to zero

    byte incomingCommand = Serial.read();
    if (incomingCommand == 's'){         
      myEnc.write(0);
      revs = 0;
    }
    if (incomingCommand == 'e'){           
      Serial.println(distanceEnc,3);   
    }
    if (incomingCommand == 'f'){
      time = Dist.getDistanceTime();
      heightFloor = (float) time/(148.00);     
      Serial.println(heightFloor);  
    }
    if(incomingCommand =='c'){    
      for(int i=1; i<11; i++){
       times[i] = Dist.getDistanceTime();
       delay(100); 
       timesum = timesum+times[i];}

     time = timesum/(int)10;
      heightCurb = (float) time/(148.00);
      heightDiff = heightFloor - heightCurb;     
      Serial.println(heightCurb);  
    }
  }
}

I used the example from the developer's example folder to build a working app.

https://github.com/mik3y/usb-serial-for-android/tree/master/usbSerialExamples

Check these out, and see what you can make of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM