0

I am working on a small project where I have to read data from different hardware and display it on android app. I am using arduino Uno to get the values from a sensor and a motor and I use a bluetooth module to send the data to the android device. I know how to read data from motor only but not from motor AND sensor. The data displayed on the arduino can be assumed as follows:

Motor: 132

Sensor: 258

Motor: 259

Sensor: 992

So first, we will read the value of the motor then the sensor then the motor and so on. Here is part of the code where I used handler and string builder to read data from the motor. How to modify the code in order to read from BOTH the motor and sensor and keep the updating the values?

Code:

Handler mHandler = new Handler()
    {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 1:                                                 
                    byte[] readBuf = (byte[]) msg.obj;
                    String strIncom = new String(readBuf, 0, msg.arg1);                 
                    sb.append(strIncom);                                                
                    int endOfLineIndex = sb.indexOf("\r\n");                            
                    if (endOfLineIndex > 0) {                                           
                        String sbprint = sb.substring(0, endOfLineIndex);               
                        sb.delete(0, sb.length());                                      
                        motor.setText(sbprint);             // update TextView
                    }

                    break;
            }
        };
    };

1 Answer 1

1

You can convert each value to string before sending to bluetooth using string.contain("string"), like this:

if (sbprint.contains("C"){
    temptv.setText(sbprint);}
if (sbprint.contains("H")){
    humidtytv.setText(sbprint);} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.