diff --git a/.idea/misc.xml b/.idea/misc.xml index aed02e1..3963879 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,30 @@ - + + + + + diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..410ae68 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Sketches/bluetooth_motor/bluetooth_motor.ino b/Sketches/bluetooth_motor/bluetooth_motor.ino index 6a77950..1d6defc 100644 --- a/Sketches/bluetooth_motor/bluetooth_motor.ino +++ b/Sketches/bluetooth_motor/bluetooth_motor.ino @@ -5,17 +5,17 @@ #define AIN1 4 #define BIN1 8 #define AIN2 5 -#define BIN2 9 -#define PWMA 7 +#define BIN2 7 +#define PWMA 11 #define PWMB 10 #define STBY 6 const int offsetA = 1; -const int offsetB = 1; +const int offsetB = -1; SoftwareSerial BTSerial(3, 2); // RX | TX String reader; -int d = 0; +int d = 600; Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY); Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY); @@ -24,6 +24,7 @@ void setup() Serial.begin(9600); Serial.println("Ready:"); BTSerial.begin(9600); // HC-05 default speed in AT command more + brake(motor1, motor2); } void loop() @@ -41,11 +42,13 @@ void loop() Serial.println("Receved: "+reader+""); d = reader.toInt(); } - - if (d == 11) { - motor1.drive(255); - } else if (d == 12) { - motor1.drive(-255); + + 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/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 04aa688..d71eee8 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,8 @@ + + - + diff --git a/app/src/main/java/yehor/vialov/vaporizr2/MainActivity.java b/app/src/main/java/yehor/vialov/vaporizr2/MainActivity.java index d72d356..718d74d 100644 --- a/app/src/main/java/yehor/vialov/vaporizr2/MainActivity.java +++ b/app/src/main/java/yehor/vialov/vaporizr2/MainActivity.java @@ -1,13 +1,164 @@ package yehor.vialov.vaporizr2; +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothDevice; +import android.bluetooth.BluetoothSocket; +import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.SeekBar; +import android.widget.TextView; + +import org.w3c.dom.Text; + +import java.io.IOException; +import java.io.OutputStream; +import java.util.UUID; public class MainActivity extends AppCompatActivity { + private final static int REQUEST_ENABLE_BT = 1; + Button btnConnect; + OutputStream btOutStream; + Boolean btConnected = false; + BluetoothSocket btSocket = null; + SeekBar motorA; + SeekBar motorB; + TextView txtA; + TextView txtB; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + + btnConnect = (Button)findViewById(R.id.btnConnect); + + motorA = (SeekBar)findViewById(R.id.motorA); + motorA.setProgress(3); + motorA.setMax(6); + motorB = (SeekBar)findViewById(R.id.motorB); + motorB.setProgress(3); + motorB.setMax(6); + + txtA = (TextView)findViewById(R.id.txtA); + txtB = (TextView)findViewById(R.id.txtB); + + motorA.setOnSeekBarChangeListener( + new SeekBar.OnSeekBarChangeListener() { + String progress = ""; + @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(); + } + } + + @Override + public void onStartTrackingTouch(SeekBar seekBar) { + // Do something here, + //if you want to do anything at the start of + // touching the seekbar + } + + @Override + public void onStopTrackingTouch(SeekBar seekBar) { + seekBar.setProgress(3); + + } + }); + + motorB.setOnSeekBarChangeListener( + new SeekBar.OnSeekBarChangeListener() { + String progress = ""; + @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(); + } + } + + @Override + public void onStartTrackingTouch(SeekBar seekBar) { + // Do something here, + //if you want to do anything at the start of + // touching the seekbar + } + + @Override + public void onStopTrackingTouch(SeekBar seekBar) { + seekBar.setProgress(3); + + } + }); + + btnConnect.setOnClickListener(new View.OnClickListener() { + public void onClick(View v) { + if (!btConnected) { + BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); + if (adapter == null) { + // Device does not support Bluetooth + finish(); //exit + } + + if (!adapter.isEnabled()) { + //make sure the device's bluetooth is enabled + Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); + startActivityForResult(enableBluetooth, REQUEST_ENABLE_BT); + } + + final UUID SERIAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //UUID for serial connection + String mac = "98:D3:31:F5:2D:2F"; //my laptop's mac adress + BluetoothDevice device = adapter.getRemoteDevice(mac); //get remote device by mac, we assume these two devices are already paired + + + // Get a BluetoothSocket to connect with the given BluetoothDevice + btSocket = null; + btOutStream = null; + try { + btSocket = device.createRfcommSocketToServiceRecord(SERIAL_UUID); + } catch (IOException e) { + e.printStackTrace(); + } + + try { + btSocket.connect(); + btOutStream = btSocket.getOutputStream(); + btConnected = true; + btnConnect.setText("Disconnect"); + //now you can use out to send output via out.write + } catch (IOException e) { + e.printStackTrace(); + } + } else { + try { + btOutStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + try { + btSocket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + btConnected = false; + btnConnect.setText("Connect"); + } + } + }); } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 9c4563c..c179e1a 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -6,13 +6,70 @@ android:layout_height="match_parent" tools:context="yehor.vialov.vaporizr2.MainActivity"> - + + +