It works!
This commit is contained in:
		
							
								
								
									
										26
									
								
								.idea/misc.xml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										26
									
								
								.idea/misc.xml
									
									
									
										generated
									
									
									
								
							| @@ -1,6 +1,30 @@ | |||||||
| <?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||||||
| <project version="4"> | <project version="4"> | ||||||
|   <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7"> |   <component name="NullableNotNullManager"> | ||||||
|  |     <option name="myDefaultNullable" value="android.support.annotation.Nullable" /> | ||||||
|  |     <option name="myDefaultNotNull" value="android.support.annotation.NonNull" /> | ||||||
|  |     <option name="myNullables"> | ||||||
|  |       <value> | ||||||
|  |         <list size="4"> | ||||||
|  |           <item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.Nullable" /> | ||||||
|  |           <item index="1" class="java.lang.String" itemvalue="javax.annotation.Nullable" /> | ||||||
|  |           <item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.Nullable" /> | ||||||
|  |           <item index="3" class="java.lang.String" itemvalue="android.support.annotation.Nullable" /> | ||||||
|  |         </list> | ||||||
|  |       </value> | ||||||
|  |     </option> | ||||||
|  |     <option name="myNotNulls"> | ||||||
|  |       <value> | ||||||
|  |         <list size="4"> | ||||||
|  |           <item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.NotNull" /> | ||||||
|  |           <item index="1" class="java.lang.String" itemvalue="javax.annotation.Nonnull" /> | ||||||
|  |           <item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.NonNull" /> | ||||||
|  |           <item index="3" class="java.lang.String" itemvalue="android.support.annotation.NonNull" /> | ||||||
|  |         </list> | ||||||
|  |       </value> | ||||||
|  |     </option> | ||||||
|  |   </component> | ||||||
|  |   <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK"> | ||||||
|     <output url="file://$PROJECT_DIR$/build/classes" /> |     <output url="file://$PROJECT_DIR$/build/classes" /> | ||||||
|   </component> |   </component> | ||||||
|   <component name="ProjectType"> |   <component name="ProjectType"> | ||||||
|   | |||||||
							
								
								
									
										9
									
								
								.idea/modules.xml
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								.idea/modules.xml
									
									
									
										generated
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | |||||||
|  | <?xml version="1.0" encoding="UTF-8"?> | ||||||
|  | <project version="4"> | ||||||
|  |   <component name="ProjectModuleManager"> | ||||||
|  |     <modules> | ||||||
|  |       <module fileurl="file://$PROJECT_DIR$/VaporizR2.iml" filepath="$PROJECT_DIR$/VaporizR2.iml" /> | ||||||
|  |       <module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" /> | ||||||
|  |     </modules> | ||||||
|  |   </component> | ||||||
|  | </project> | ||||||
							
								
								
									
										6
									
								
								.idea/vcs.xml
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								.idea/vcs.xml
									
									
									
										generated
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | |||||||
|  | <?xml version="1.0" encoding="UTF-8"?> | ||||||
|  | <project version="4"> | ||||||
|  |   <component name="VcsDirectoryMappings"> | ||||||
|  |     <mapping directory="$PROJECT_DIR$" vcs="Git" /> | ||||||
|  |   </component> | ||||||
|  | </project> | ||||||
| @@ -5,17 +5,17 @@ | |||||||
| #define AIN1 4 | #define AIN1 4 | ||||||
| #define BIN1 8 | #define BIN1 8 | ||||||
| #define AIN2 5 | #define AIN2 5 | ||||||
| #define BIN2 9 | #define BIN2 7 | ||||||
| #define PWMA 7 | #define PWMA 11 | ||||||
| #define PWMB 10 | #define PWMB 10 | ||||||
| #define STBY 6 | #define STBY 6 | ||||||
|  |  | ||||||
| const int offsetA = 1; | const int offsetA = 1; | ||||||
| const int offsetB = 1; | const int offsetB = -1; | ||||||
|  |  | ||||||
| SoftwareSerial BTSerial(3, 2); // RX | TX | SoftwareSerial BTSerial(3, 2); // RX | TX | ||||||
| String reader; | String reader; | ||||||
| int d = 0; | int d = 600; | ||||||
| Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY); | Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY); | ||||||
| Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY); | Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY); | ||||||
|  |  | ||||||
| @@ -24,6 +24,7 @@ void setup() | |||||||
|   Serial.begin(9600); |   Serial.begin(9600); | ||||||
|   Serial.println("Ready:"); |   Serial.println("Ready:"); | ||||||
|   BTSerial.begin(9600);  // HC-05 default speed in AT command more |   BTSerial.begin(9600);  // HC-05 default speed in AT command more | ||||||
|  |   brake(motor1, motor2); | ||||||
| } | } | ||||||
|  |  | ||||||
| void loop() | void loop() | ||||||
| @@ -42,10 +43,12 @@ void loop() | |||||||
|     d = reader.toInt();     |     d = reader.toInt();     | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   if (d == 11) { |   if (d <= 500) { //0 - full back, 255 - stop, 500 - full forward | ||||||
|     motor1.drive(255); |     int v1 = d - 255; | ||||||
|   } else if (d == 12) { |     motor1.drive(v1);   | ||||||
|     motor1.drive(-255); |   } else if (d >= 1000) { // 1000 - full back, 1255 - stop, 1500 - full forward | ||||||
|  |     int v2 = d - 1255;  | ||||||
|  |     motor2.drive(v2);   | ||||||
|   } |   } | ||||||
|   |   | ||||||
| } | } | ||||||
|   | |||||||
| @@ -2,6 +2,8 @@ | |||||||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | <manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||||||
|     package="yehor.vialov.vaporizr2"> |     package="yehor.vialov.vaporizr2"> | ||||||
|  |  | ||||||
|  |     <uses-permission android:name="android.permission.BLUETOOTH" /> | ||||||
|  |  | ||||||
|     <application |     <application | ||||||
|         android:allowBackup="true" |         android:allowBackup="true" | ||||||
|         android:icon="@mipmap/ic_launcher" |         android:icon="@mipmap/ic_launcher" | ||||||
| @@ -9,7 +11,8 @@ | |||||||
|         android:roundIcon="@mipmap/ic_launcher_round" |         android:roundIcon="@mipmap/ic_launcher_round" | ||||||
|         android:supportsRtl="true" |         android:supportsRtl="true" | ||||||
|         android:theme="@style/AppTheme"> |         android:theme="@style/AppTheme"> | ||||||
|         <activity android:name=".MainActivity"> |         <activity android:name=".MainActivity" android:configChanges="orientation" | ||||||
|  |             android:screenOrientation="landscape"> | ||||||
|             <intent-filter> |             <intent-filter> | ||||||
|                 <action android:name="android.intent.action.MAIN" /> |                 <action android:name="android.intent.action.MAIN" /> | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,13 +1,164 @@ | |||||||
| package yehor.vialov.vaporizr2; | 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.support.v7.app.AppCompatActivity; | ||||||
| import android.os.Bundle; | 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 { | 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 |     @Override | ||||||
|     protected void onCreate(Bundle savedInstanceState) { |     protected void onCreate(Bundle savedInstanceState) { | ||||||
|         super.onCreate(savedInstanceState); |         super.onCreate(savedInstanceState); | ||||||
|         setContentView(R.layout.activity_main); |         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"); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -6,13 +6,70 @@ | |||||||
|     android:layout_height="match_parent" |     android:layout_height="match_parent" | ||||||
|     tools:context="yehor.vialov.vaporizr2.MainActivity"> |     tools:context="yehor.vialov.vaporizr2.MainActivity"> | ||||||
|  |  | ||||||
|     <TextView |     <SeekBar | ||||||
|         android:layout_width="wrap_content" |         android:id="@+id/motorA" | ||||||
|         android:layout_height="wrap_content" |         style="@style/Widget.AppCompat.SeekBar.Discrete" | ||||||
|         android:text="Hello World!" |         android:layout_width="182dp" | ||||||
|  |         android:layout_height="0dp" | ||||||
|  |         android:layout_marginBottom="8dp" | ||||||
|  |         android:layout_marginStart="8dp" | ||||||
|  |         android:layout_marginTop="8dp" | ||||||
|  |         android:max="500" | ||||||
|  |         android:min="0" | ||||||
|  |         android:progress="255" | ||||||
|  |         android:rotation="270" | ||||||
|  |         android:thumb="@android:drawable/ic_notification_overlay" | ||||||
|         app:layout_constraintBottom_toBottomOf="parent" |         app:layout_constraintBottom_toBottomOf="parent" | ||||||
|         app:layout_constraintLeft_toLeftOf="parent" |         app:layout_constraintStart_toStartOf="parent" | ||||||
|         app:layout_constraintRight_toRightOf="parent" |  | ||||||
|         app:layout_constraintTop_toTopOf="parent" /> |         app:layout_constraintTop_toTopOf="parent" /> | ||||||
|  |  | ||||||
|  |     <SeekBar | ||||||
|  |         android:id="@+id/motorB" | ||||||
|  |         style="@style/Widget.AppCompat.SeekBar.Discrete" | ||||||
|  |         android:layout_width="182dp" | ||||||
|  |         android:layout_height="0dp" | ||||||
|  |         android:layout_marginBottom="8dp" | ||||||
|  |         android:layout_marginEnd="8dp" | ||||||
|  |         android:layout_marginTop="8dp" | ||||||
|  |         android:max="500" | ||||||
|  |         android:min="0" | ||||||
|  |         android:progress="255" | ||||||
|  |         android:rotation="270" | ||||||
|  |         android:thumb="@android:drawable/ic_notification_overlay" | ||||||
|  |         app:layout_constraintBottom_toBottomOf="parent" | ||||||
|  |         app:layout_constraintEnd_toEndOf="parent" | ||||||
|  |         app:layout_constraintTop_toTopOf="parent" /> | ||||||
|  |  | ||||||
|  |     <Button | ||||||
|  |         android:id="@+id/btnConnect" | ||||||
|  |         android:layout_width="wrap_content" | ||||||
|  |         android:layout_height="wrap_content" | ||||||
|  |         android:layout_marginEnd="8dp" | ||||||
|  |         android:layout_marginStart="8dp" | ||||||
|  |         android:layout_marginTop="8dp" | ||||||
|  |         android:text="Connect" | ||||||
|  |         app:layout_constraintEnd_toEndOf="parent" | ||||||
|  |         app:layout_constraintStart_toStartOf="parent" | ||||||
|  |         app:layout_constraintTop_toTopOf="parent" /> | ||||||
|  |  | ||||||
|  |     <TextView | ||||||
|  |         android:id="@+id/txtA" | ||||||
|  |         android:layout_width="wrap_content" | ||||||
|  |         android:layout_height="wrap_content" | ||||||
|  |         android:layout_marginBottom="8dp" | ||||||
|  |         android:layout_marginStart="8dp" | ||||||
|  |         android:text="TextView" | ||||||
|  |         app:layout_constraintBottom_toBottomOf="parent" | ||||||
|  |         app:layout_constraintStart_toEndOf="@+id/motorA" /> | ||||||
|  |  | ||||||
|  |     <TextView | ||||||
|  |         android:id="@+id/txtB" | ||||||
|  |         android:layout_width="wrap_content" | ||||||
|  |         android:layout_height="wrap_content" | ||||||
|  |         android:layout_marginBottom="8dp" | ||||||
|  |         android:layout_marginEnd="8dp" | ||||||
|  |         android:text="TextView" | ||||||
|  |         app:layout_constraintBottom_toBottomOf="parent" | ||||||
|  |         app:layout_constraintEnd_toStartOf="@+id/motorB" /> | ||||||
|  |  | ||||||
| </android.support.constraint.ConstraintLayout> | </android.support.constraint.ConstraintLayout> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user