• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Timer and outputStream don't work stable together

Hello everyone i try to do a button for get commands from txt file and i want these commands to run for a certain period of time in my Arduino.There is no trouble getting commands in the txt file but commands do not work during the time I want to enter into the switch case.I also tried these code with countdown timer it did same.

This is my txt file:
Code:
Ileri,2,2
Sol,3,2  ---->3 second is working time 2 is repeat number(Repeat this 4 commands 2 times)
Sag,4,2
Geri,5,2

This my button to do commands:
Code:
start_btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        timer=new Timer();
        ///data/data/com.example.emrecan.myapplication/files/komutlar.txt
        String yon;
        String saniye1;
        String tekrar;
        int i=0,j=0,c=0,d=0;

        ArrayList<String> listS=new ArrayList<String>();

       try {
            Scanner s=new Scanner(new File("/data/data/com.example.emrecan.myapplication/files/komutlar.txt"));

                while(s.hasNextLine())
                {
                    listS.add(s.nextLine());
                }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

          ///data/data/com.example.emrecan.myapplication
            String[] line2=new String[100];
            for(String str:listS)
        {
                   String[] line=str.split(",");
                    line2[c]=line[i];
                    line2[c+1]=line[i+1];
                    line2[c+2]=line[i+2];
                    c=c+3;
        }
          
            String [] line3=new String[c+2];
            while(d<c)
            {
                line3[d]=line2[d];
                d=d+1;
            }
            d=0;
            tekrar=line3[2];
            int tekrar1=Integer.parseInt(tekrar);

        while(d<tekrar1)
            {
                while(j<=c-2)
                {
                    yon = line3[j];
                    saniye1 = line3[j + 1];
                    sure1 = Integer.parseInt(saniye1);
                    System.out.println(yon);
                    System.out.println(sure1);
                    System.out.println(tekrar1);
                    e=0;
                    switch (yon)
                    {
                    case "Ileri":

                        TimerTask timerTask = new TimerTask() {
                            @Override
                            public void run() {
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        System.out.println("İleride");
                                        if (e < sure1)
                                        {
                                            try {
                                                outputStream.write(1);
                                                Toast.makeText(getApplicationContext(), "İleri Komutunun İçinde!", Toast.LENGTH_LONG).show();
                                            } catch (IOException e) {
                                                e.printStackTrace();
                                            }
                                        e = e + 1;
                                    }
                                    else
                                        {
                                            timer.cancel();
                                            timer.purge();
                                        }
                                    }
                                });
                            }
                        };
                        timer.schedule(timerTask,0,1000);
                        Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
                        break;


                    case "Sol":


                        TimerTask timerTask2 = new TimerTask() {
                            @Override
                            public void run() {
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        System.out.println("Solda");
                                        if (e < sure1)
                                        {
                                            try {
                                                outputStream.write(2);
                                                Toast.makeText(getApplicationContext(), "İleri Komutunun İçinde!", Toast.LENGTH_LONG).show();
                                            } catch (IOException e) {
                                                e.printStackTrace();
                                            }
                                            e = e + 1;
                                        }
                                        else
                                        {
                                            timer.cancel();
                                            timer.purge();
                                        }
                                    }
                                });
                            }
                        };
                        timer.schedule(timerTask2,0,1000);
                        Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
                        break;


                    case "Sag":

                        TimerTask timerTask3 = new TimerTask() {
                            @Override
                            public void run() {
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        System.out.println("Solda");
                                        if (e < sure1)
                                        {
                                            try {
                                                outputStream.write(3);
                                                Toast.makeText(getApplicationContext(), "İleri Komutunun İçinde!", Toast.LENGTH_LONG).show();
                                            } catch (IOException e) {
                                                e.printStackTrace();
                                            }
                                            e = e + 1;
                                        }
                                        else
                                        {
                                            timer.cancel();
                                            timer.purge();
                                        }
                                    }
                                });
                            }
                        };
                        timer.schedule(timerTask3,0,1000);
                        Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
                        break;


                    case "Geri":

                        TimerTask timerTask4 = new TimerTask() {
                            @Override
                            public void run() {
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        System.out.println("Geride");
                                        if (e < sure1)
                                        {
                                            try {
                                                outputStream.write(4);
                                                Toast.makeText(getApplicationContext(), "İleri Komutunun İçinde!", Toast.LENGTH_LONG).show();
                                            } catch (IOException e) {
                                                e.printStackTrace();
                                            }
                                            e = e + 1;
                                        }
                                        else
                                        {
                                            timer.cancel();
                                            timer.purge();
                                        }
                                    }
                                });
                            }
                        };
                        timer.schedule(timerTask4,0,1000);
                        Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
                        break;

                    default:
                        Toast.makeText(getApplicationContext(), "Sıkıntı var!", Toast.LENGTH_LONG).show();
                        break;

                }

                    j=j+3;
                }
                j=0;
                d=d+1;
                //System.out.println('A');
            }

    }
});
Here is my app output:
Code:
I/System.out: Ileri
I/System.out: 2
    2
I/System.out: Sol
    3
    2
I/System.out: Sag
    4
    2
I/System.out: Geri
    5
    2
I/System.out: A
    Ileri
    2
    2
I/System.out: Sol
    3
    2
I/System.out: Sag
    4
    2
I/System.out: Geri
    5
    2
I/System.out: A
    İleride
D/ViewRootImpl: #1 mView = android.widget.LinearLayout{60501b7 V.E...... ......I. 0,0-0,0 #10203a4 android:id/toast_layout_root}
I/System.out: Solda
I/System.out: Sagda
I/System.out: Geride
I/System.out: İleride
I/System.out: Solda
    Sagda
    Geride

This is my arduino code:
Code:
void setup()
{
 Serial.begin(9600);
 pinMode(M1, OUTPUT);
 pinMode(M2, OUTPUT);
 Serial.println("Basladı");
}

void loop() {
 if(Serial.available() > 0)
  {
    int data;
    data = Serial.read();

    Serial.print(data);
}
}

This is arduino's output:
Code:
Basladı
12341
 
Last edited:
I solved my problem by using thread.sleep(); and i'm writing here because someone might need my code.
Code:
start_btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        timer=new Timer();
        ///data/data/com.example.emrecan.myapplication/files/komutlar.txt
        String yon;
        String saniye1;
        String tekrar;
        int i=0,j=0,c=0,d=0;

        ArrayList<String> listS=new ArrayList<String>();

       try {
            Scanner s=new Scanner(new File("/data/data/com.example.emrecan.myapplication/files/komutlar.txt"));

                while(s.hasNextLine())
                {
                    listS.add(s.nextLine());
                }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

            String[] line2=new String[100];
            for(String str:listS)
        {
                   String[] line=str.split(",");
                    line2[c]=line[i];
                    line2[c+1]=line[i+1];
                    line2[c+2]=line[i+2];
                    c=c+3;
        }

            String [] line3=new String[c+2];
            while(d<c)
            {
                line3[d]=line2[d];
                d=d+1;
            }
            d=0;
            tekrar=line3[2];
            int tekrar1=Integer.parseInt(tekrar);

        while(d<tekrar1)
            {

                while(j<=c-2) {

                    yon = line3[j];
                    saniye1 = line3[j + 1];
                    sure1 = Long.parseLong(saniye1);
                    sure1 = sure1 * 1000;
                    //sure1 = sure1 + 1000;
                    System.out.println(yon);
                    System.out.println(sure1);
                    System.out.println(tekrar1);
                    e = 0;

                    while (sure1>0)
                    {
                        switch (yon) {
                            case "Ileri":
                                try {
                                    outputStream.write(1);
                                } catch (IOException e1) {
                                    e1.printStackTrace();
                                }
                                Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
                                try {
                                    Thread.sleep(1000);                 //1000 milliseconds is one second.
                                } catch(InterruptedException ex) {
                                    Thread.currentThread().interrupt();
                                }
                                break;


                            case "Sol":
                                try {
                                    outputStream.write(2);
                                } catch (IOException e1) {
                                    e1.printStackTrace();
                                }

                                Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
                                try {
                                    Thread.sleep(1000);                 //1000 milliseconds is one second.
                                } catch(InterruptedException ex) {
                                    Thread.currentThread().interrupt();
                                }
                                break;


                            case "Sag":
                                try {
                                    outputStream.write(3);
                                } catch (IOException e1) {
                                    e1.printStackTrace();
                                }
                                Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
                                try {
                                    Thread.sleep(1000);                 //1000 milliseconds is one second.
                                } catch(InterruptedException ex) {
                                    Thread.currentThread().interrupt();
                                }
                                break;


                            case "Geri":
                                mp4.start();
                                try {
                                    outputStream.write(4);
                                } catch (IOException e1) {
                                    e1.printStackTrace();
                                }

                                Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
                                try {
                                    Thread.sleep(1000);                 //1000 milliseconds is one second.
                                } catch(InterruptedException ex) {
                                    Thread.currentThread().interrupt();
                                }
                                break;

                            default:
                                Toast.makeText(getApplicationContext(), "Sıkıntı var!", Toast.LENGTH_LONG).show();
                                break;

                        }
                        sure1=sure1-1000;
                }

                    j=j+3;
                }
                j=0;
                d=d+1;
                System.out.println('A');
            }
        try {
            outputStream.write(10);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
});
 
Back
Top Bottom