Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm using a bluetooth connection to send serial data from Arduino to my Android app. The code I'm using to connect the Android device is BluetoothChat.java which is sample code you can find if googled.

The sample code displays the data in a ListView and I want to store it in an array.

Here is the code for reading the serial print from the Arduino...

case MESSAGE_READ:

            byte[] readBuf = (byte[]) msg.obj;
            String readMessage = new String(readBuf);
            mConversationArrayAdapter.add(readMessage);
            int a = readBuf.length;

            //coordinates[] is the exact length necessary as given by double[a], and contains no more and
            //no fewer positions than required.

            double coordinates[] = new double[a];

            //Check the values of the data in readBuf[] and make the necessary conversions to store in the
            //variables.
            for(int i=0; i<a; i++){

                int val=readBuf[i];

                /*
                *(int) val/8 will divide val and round down to the nearest integer.
                *val%8 divides by 8 but takes the remainder which is the value of
                *all variables above x1, though the math works with x1.
                */

                coordinates[(int) val/8] = val%8;

            }

I think I stored the data correctly. But when I use coordinates[] it doesn't change the text in the TextView I have setup. Below is the code to set the text.

if(readBuf.length==4){
                double x1 = coordinates[0];
                double x2 = coordinates[1];
                double y1 = coordinates[2];
                double y2 = coordinates[3];

                r = new Regression(x1, x2, y1, y2);
                r.computation();

                leftLung.setText("Left Lung "+ r.leftLungDamage() +"% hit");
                rightLung.setText("Right Lung "+ r.rightLungDamage() +"% hit");
                heart.setText("Heart "+ r.heartDamage() +"% hit");
                liver.setText("Liver "+ r.liverDamage() +"% hit");
                smallIntestine.setText("Small Intestine "+ r.smIntestineDamage() +"% hit");
                largeIntestine.setText("Large Intestine "+ r.lgIntestineDamage() +"% hit");
                stomach.setText("Stomach "+ r.stomachDamage() +"% hit");
                spleen.setText("Spleen "+ r.spleenDamage() +"% hit");
                gallbladder.setText("Gallbladder "+ r.gallbladderDamage() +"% hit");
                lKidney.setText("Left Kidney "+ r.lKidneyDamage() +"% hit");
                rKidney.setText("Right Kidney "+ r.rKidneyDamage() +"% hit");
                pancreas.setText("Pancreas "+ r.pancreasDamage() +"% hit");
                venaCava.setText("Vena Cava "+ r.venaCavaDamage() +"% hit");
                dAorta.setText("Descending Aorta "+ r.dAortaDamage() +"% hit");
                aAorta.setText("Ascending Aorta "+ r.aAortaDamage() +"% hit");
            }

Regression works on its own as a standalone Java application.

share|improve this question

1 Answer 1

coordinates[(int) val/8] = val%8;

This line strikes me as weird array asignment. You create an array with length of readBuf. So why use something like (int) val/8 to store values in the array.

Probably

coordinates[i] = val%8;

is what you want. Or at least coordinates[i] = something is the proper way to store values in an array using a for loop.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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