I find a way to remove the delay from the arduino code. Im now sending
a start and a stop flag from android to arduino in order to start and
stop ardunio sending data. It works almost ok without crashing but its
very slow, i am getting new data every 300-400ms..
Am i doing something wrong? Can anybody please help me? Here is the
code for the android and for the arduino.
#include <MeetAndroid.h>
MeetAndroid meetAndroid;
char startFlag = 18;
char ack = 19;
int FLAG = 0;
void setup()
{
Serial.begin(57600);
meetAndroid.registerFunction(stop1, 'S');
meetAndroid.registerFunction(run, 'R');
Serial.print(startFlag);
Serial.print(5);
Serial.print(ack);
}
void loop(){
meetAndroid.receive();
int randNumber1 = random(10, 20);
int randNumber2 = random(10, 20);
int randNumber3 = random(10, 20);
int randNumber4 = random(10, 20);
int randNumber5 = random(10, 20);
int randNumber6 = random(10, 20);
if(FLAG == 1){
Serial.print(startFlag);
Serial.print(randNumber1);
Serial.print(",");
Serial.print(randNumber2);
Serial.print(",");
Serial.print(randNumber3);
Serial.print(",");
Serial.print(randNumber4);
Serial.print(",");
Serial.print(randNumber5);
Serial.print(",");
Serial.print(randNumber6);
Serial.print(ack);
FLAG = 0;
}
}
void stop1(byte flag, byte numOfValues)
{
if(meetAndroid.getInt() == 0)
{
FLAG = 0;
digitalWrite(13,HIGH);
}
}
void run(byte flag, byte numOfValues)
{
if(meetAndroid.getInt() == 1)
{
FLAG = 1;
digitalWrite(13,LOW);
}
}
public class Am_test extends Activity {
/** Called when the activity is first created. */
private static final String DEVICE_ADDRESS = "00:06:66:42:1F:C8";
private ArduinoReceiver arduinoReceiver = new ArduinoReceiver();
TextView testview;
String num;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected void onStart() {
super.onStart();
// in order to receive broadcasted intents we need to register our
receiver
registerReceiver(arduinoReceiver, new
IntentFilter(AmarinoIntent.ACTION_RECEIVED));
// this is how you tell Amarino to connect to a specific BT device
from within your own code
Amarino.connect(this, DEVICE_ADDRESS);
}
@Override
protected void onStop() {
super.onStop();
// if you connect in onStart() you must not forget to disconnect
when your app is closed
Amarino.disconnect(this, DEVICE_ADDRESS);
// do never forget to unregister a registered receiver
unregisterReceiver(arduinoReceiver);
}
public class ArduinoReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
String data = null;
float[] var= new float[20];
final String address =
intent.getStringExtra(AmarinoIntent.EXTRA_DEVICE_ADDRESS);
final int dataType =
intent.getIntExtra(AmarinoIntent.EXTRA_DATA_TYPE, -1);
if (dataType == AmarinoIntent.STRING_EXTRA)
{
data = intent.getStringExtra(AmarinoIntent.EXTRA_DATA);
sendflagstop();
testview = (TextView)findViewById(R.id.result);
testview.setText(data);
Scanner s = null;
try
{
s = new Scanner(data);
// s.useDelimiter(",\\s*");
s.useDelimiter(",|\\r\\n");
int i=1;
while (s.hasNext())
{
if (s.hasNextFloat())
{
var[i]= s.nextFloat();
}
else
{
s.next();
}
i++;
}
}
finally
{
s.close();
sendflagstart();
testview = (TextView)findViewById(R.id.textView1);
testview.setText(Float.toString(var[1]));
testview = (TextView)findViewById(R.id.textView2);
testview.setText(Float.toString(var[2]));
testview = (TextView)findViewById(R.id.textView3);
testview.setText(Float.toString(var[3]));
testview = (TextView)findViewById(R.id.textView4);
testview.setText(Float.toString(var[4]));
testview = (TextView)findViewById(R.id.textView5);
testview.setText(Float.toString(var[5]));
}
}
}
}
private void sendflagstop(){
Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'S', 0);
}
private void sendflagstart(){
Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'R', 1);
}
On 1 Σεπτ, 18:37, manorAstroman <
telefthera...@gmail.com> wrote:
> Thanx for your answer. The serial.print(18) is the startFlag and the
> Serial.print(19) is the ack. I just found what was wrong, 18 and 19 is
> char's.
> so this is the correct
> char startFlag = 18;
> char ack = 19;
>
> Serial.print(startFlag);
> Serial.print(1);
> Serial.print(",");
> Serial.print(2);
> Serial.print(",");
> Serial.print(3);
> Serial.print(",");
> Serial.print(4);
> Serial.print(",");
> Serial.print(5);
> Serial.print(ack);
>
> Another problem that i have now is that after a little time the
> android program crashes.. =\
> If i put a larger than 400ms delay in the arduino loop it is working
> ok, but with smaller delay crashes! Is there anyway to obtain a rate?
>