Emre Can AYDIN
Newbie
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:
This my button to do commands:
Here is my app output:
This is my arduino code:
This is arduino's output:
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');
}
}
});
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: