From ac8866ff73c0cc3c974089120b5600e3dde525a7 Mon Sep 17 00:00:00 2001 From: estevez Date: Mon, 25 Dec 2017 21:27:27 +0200 Subject: [PATCH] New way to send and read commands to avoid lags. Only 2 speed level for every motor --- Sketches/bluetooth_motor/bluetooth_motor.ino | 66 ++++++++++++------- .../yehor/vialov/vaporizr2/MainActivity.java | 47 ++++++------- 2 files changed, 68 insertions(+), 45 deletions(-) diff --git a/Sketches/bluetooth_motor/bluetooth_motor.ino b/Sketches/bluetooth_motor/bluetooth_motor.ino index 1d6defc..6ec06e4 100644 --- a/Sketches/bluetooth_motor/bluetooth_motor.ino +++ b/Sketches/bluetooth_motor/bluetooth_motor.ino @@ -15,40 +15,60 @@ const int offsetB = -1; SoftwareSerial BTSerial(3, 2); // RX | TX String reader; -int d = 600; Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY); Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY); void setup() { - Serial.begin(9600); - Serial.println("Ready:"); + //Serial.begin(9600); + //Serial.println("Ready:"); BTSerial.begin(9600); // HC-05 default speed in AT command more brake(motor1, motor2); } void loop() { - reader=""; - while (BTSerial.available()) { - delay(3); - if (BTSerial.available()>0) { - char c = BTSerial.read(); - reader += c; - - } - } - if (reader.length() > 0) { - Serial.println("Receved: "+reader+""); - d = reader.toInt(); + char m1 = '9'; + char m2 = '9'; + //Serial.println("There is "+String(BTSerial.available())+" available"); + if (BTSerial.available()==2) { + m1 = BTSerial.read(); + m2 = BTSerial.read(); + //Serial.println("Receved: "+String(m1)+";"+String(m2)); + switch (m1) { + case '0': + motor1.drive(-255); + break; + case '1': + motor1.drive(-170); + break; + case '2': + motor1.drive(0); + break; + case '3': + motor1.drive(170); + break; + case '4': + motor1.drive(255); + break; + } + switch (m2) { + case '0': + motor2.drive(-255); + break; + case '1': + motor2.drive(-170); + break; + case '2': + motor2.drive(0); + break; + case '3': + motor2.drive(170); + break; + case '4': + motor2.drive(255); + break; + } } - if (d <= 500) { //0 - full back, 255 - stop, 500 - full forward - int v1 = d - 255; - motor1.drive(v1); - } else if (d >= 1000) { // 1000 - full back, 1255 - stop, 1500 - full forward - int v2 = d - 1255; - motor2.drive(v2); - } - } diff --git a/app/src/main/java/yehor/vialov/vaporizr2/MainActivity.java b/app/src/main/java/yehor/vialov/vaporizr2/MainActivity.java index 718d74d..517142c 100644 --- a/app/src/main/java/yehor/vialov/vaporizr2/MainActivity.java +++ b/app/src/main/java/yehor/vialov/vaporizr2/MainActivity.java @@ -28,6 +28,8 @@ public class MainActivity extends AppCompatActivity { SeekBar motorB; TextView txtA; TextView txtB; + int motorACommand = 2; + int motorBCommand = 2; @Override protected void onCreate(Bundle savedInstanceState) { @@ -37,11 +39,11 @@ public class MainActivity extends AppCompatActivity { btnConnect = (Button)findViewById(R.id.btnConnect); motorA = (SeekBar)findViewById(R.id.motorA); - motorA.setProgress(3); - motorA.setMax(6); + motorA.setProgress(2); + motorA.setMax(4); motorB = (SeekBar)findViewById(R.id.motorB); - motorB.setProgress(3); - motorB.setMax(6); + motorB.setProgress(2); + motorB.setMax(4); txtA = (TextView)findViewById(R.id.txtA); txtB = (TextView)findViewById(R.id.txtB); @@ -52,14 +54,8 @@ public class MainActivity extends AppCompatActivity { @Override public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) { - progress = Integer.toString(progresValue*85+1000); - txtA.setText(progress); - try { - if (btOutStream != null) - btOutStream.write(progress.getBytes()); - } catch (IOException e) { - //e.printStackTrace(); - } + motorBCommand = progresValue; + sendCommand(); } @Override @@ -71,7 +67,7 @@ public class MainActivity extends AppCompatActivity { @Override public void onStopTrackingTouch(SeekBar seekBar) { - seekBar.setProgress(3); + seekBar.setProgress(2); } }); @@ -82,14 +78,8 @@ public class MainActivity extends AppCompatActivity { @Override public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) { - progress = Integer.toString(progresValue*85); - txtB.setText(progress); - try { - if (btOutStream != null) - btOutStream.write(progress.getBytes()); - } catch (IOException e) { - //e.printStackTrace(); - } + motorACommand = progresValue; + sendCommand(); } @Override @@ -101,7 +91,7 @@ public class MainActivity extends AppCompatActivity { @Override public void onStopTrackingTouch(SeekBar seekBar) { - seekBar.setProgress(3); + seekBar.setProgress(2); } }); @@ -161,4 +151,17 @@ public class MainActivity extends AppCompatActivity { } }); } + + private void sendCommand() { + String m1 = Integer.toString(motorACommand); + String m2 = Integer.toString(motorBCommand); + txtA.setText(m1); + txtB.setText(m2); + try { + if (btOutStream != null) + btOutStream.write((m1+m2).getBytes()); + } catch (IOException e) { + //e.printStackTrace(); + } + } }