I'm attempting to send a single char
command from my Android app to an Arduino Uno R3, and then receive data from a sensor based on which command is read. I've successfully linked my Nexus 7 and the Arduino, the RX blinks when I send the command from the app. However, I have my Arduino code set up to blink the on-board LED 3 times when a valid command is received - this does not occur. The setup works fine when I use a terminal emulator app or Tera Term on my PC, so I'm fairly certain the problem lies within my Java code. The baud rate, data/stop bits, and parity are all set correctly within the app (115200, 8, 1, and none) so I don't see it being an issue with that.
I'm using Felhr's USB Serial library - http://felhr85.net/2014/11/11/usbserial-a-serial-port-driver-library-for-android-v2-0/
Here is the relevant Android code. I know the byte is sending, and when I have the hexInfo editText show the byte, it correctly shows as the ASCII value ([115] for 's', for example).
private double serialUser(char command) {
mUsbOutput = 0;
byte commandB[] = new byte[1];
commandB[0] = (byte) command;
hexInfo.setText(Arrays.toString(commandB));
if (command == 's') {
mSerialPort.write(commandB);
commandInfo.setText(String.valueOf(command));
} else if (command=='e' || command=='c' || command=='f') {
mSerialPort.write(commandB);
commandInfo.setText(String.valueOf(command));
} else {
Toast.makeText(getApplicationContext(), "invalid command", Toast.LENGTH_SHORT).show();
}
return mUsbOutput;
}
UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() {
@Override
public void onReceivedData(byte[] arg0) {
Toast.makeText(getApplicationContext(), "data received", Toast.LENGTH_SHORT).show();
//String data = new String(arg0);
//mUsbOutput = Double.valueOf(data);
//hexInfo.setText(String.valueOf(mUsbOutput));
}
};
Here is my Arduino code. I may have set up the Serial portions incorrectly, but I'm not sure.
#include <DistanceSRF04.h>
#include <Encoder.h>
#include <math.h>
#define rxPin 6
#define txPin 7
DistanceSRF04 Dist;
int time, times[10];
float heightFloor, heightCurb;
Encoder myEnc(4, 5);
int led = 13;
void setup()
{
Serial.begin(115200);
Dist.begin(2,3);
pinMode(led, OUTPUT);
}
const double Pi = 3.14159265359; // 3.141593, set to 1 for testing purposes
double encoderPosition = -999;
byte incomingByte;
char incomingCommand;
void loop(){
if(Serial.available()>0){
blinkled();
incomingByte = (byte) Serial.read();
incomingCommand = (char) incomingByte;
}
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);
}
if (incomingCommand == 's'){
myEnc.write(0);
revs = 0;
blinkled();
}
if (incomingCommand == 'e'){
Serial.print(distanceEnc);
blinkled();
}
if (incomingCommand == 'f'){
time = Dist.getDistanceTime();
heightFloor = (float) time/(148.00);
blinkled();
}
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.print(heightDiff);
blinkled();
}
}
void blinkled(){
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(250); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(250);
digitalWrite(led, HIGH);
delay(250);
digitalWrite(led, LOW);
delay(250);
digitalWrite(led, HIGH);
delay(250);
digitalWrite(led, LOW);
delay(250);
}