volatile long cntr; boolean flip; boolean yes = false; int rev = 0; void setup() { Serial.begin(9600); pinMode(13, OUTPUT); pinMode(12, INPUT); TCCR2A = 0; TCCR2B = 2; TCNT2 = 59; TIMSK2 |= (1 << TOIE2); } ISR(TIMER2_OVF_vect) { TCNT2 = 59;//55; cntr++; if(cntr>9999) { flip = true; cntr = 0; } } void loop() { if (flip) { Serial.println(String(rev)+';'); rev = 0; flip = false; } else { if (digitalRead(12) == HIGH) { if (yes) { rev++; yes = false; } } else yes = true; } if (Serial.available() > 0){ char command = Serial.read(); switch(command){ case '0': digitalWrite(13, LOW); break; case '1': digitalWrite(13, HIGH); break; } } }
<uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
package com.example.admin.speedometer; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.util.UUID; import com.example.admin.speedometer.R; import android.os.Bundle; import android.os.Handler; import android.app.Activity; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import android.bluetooth.*; import android.content.Intent; public class FullscreenActivity extends Activity { private static final int REQUEST_ENABLE_BT = 1; final int ArduinoData = 1; final String LOG_TAG = "myLogs"; private BluetoothAdapter btAdapter = null; private BluetoothSocket btSocket = null; private StringBuilder sb = new StringBuilder(); private static String MacAddress = "20:13:05:07:01:97"; // MAC- private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); private ConnectedThred MyThred = null; public TextView spdtext, distext, fromarduino; public double Distance = 0; Button b1, b2; Handler h; /* Settings: */ private static double Radius = 3.0; // private static double spdUnit = 3.6; // : 3.6 /, 1.0 /c @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fullscreen); btAdapter = BluetoothAdapter.getDefaultAdapter(); spdtext = (TextView) findViewById(R.id.textView1); distext = (TextView) findViewById(R.id.textView2); fromarduino = (TextView) findViewById(R.id.textView5); if (btAdapter != null){ if (btAdapter.isEnabled()){ //mytext.setText("Bluetooth . ."); }else { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } }else { MyError("Fatal Error", "Bluetooth "); } b1 = (Button) findViewById(R.id.button1); b2 = (Button) findViewById(R.id.button2); b1.setOnClickListener(new OnClickListener() { public void onClick(View v) { MyThred.sendData("1"); } }); b2.setOnClickListener(new OnClickListener() { public void onClick(View v) { MyThred.sendData("0"); } }); h = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case ArduinoData: byte[] readBuf = (byte[]) msg.obj; String strIncom = new String(readBuf, 0, msg.arg1); sb.append(strIncom); int endOfLineIndex = sb.indexOf("\r\n"); if (endOfLineIndex > 0) { String sbprint = sb.substring(0, endOfLineIndex); sb.delete(0, sb.length()); String value = ""; byte channel = 0; // : 0;0;0; fromarduino.setText("Arduino: " + sbprint); for (int i = 0; i < sbprint.length(); i++) { if (sbprint.charAt(i) == ';') { if (!value.isEmpty()) { switch (channel) { case 0: double Dis = (Double.parseDouble(value) * (Radius * 6.28) ) / 100.0; double Speed = Dis * spdUnit; spdtext.setText(String.valueOf(Math.round(Speed))); Distance += Dis; distext.setText(String.valueOf(Math.round(Distance))); break; } } value = ""; channel++; } else value += sbprint.charAt(i); } } break; } }; }; } @Override public void onResume() { super.onResume(); BluetoothDevice device = btAdapter.getRemoteDevice(MacAddress); Log.d(LOG_TAG, "*** Device***"+device.getName()); try { btSocket = device.createRfcommSocketToServiceRecord(MY_UUID); Log.d(LOG_TAG, "... ..."); } catch (IOException e) { MyError("Fatal Error", " onResume() : " + e.getMessage() + "."); } btAdapter.cancelDiscovery(); Log.d(LOG_TAG, "*** ***"); Log.d(LOG_TAG, "***...***"); try { btSocket.connect(); Log.d(LOG_TAG, "*** ***"); } catch (IOException e) { try { btSocket.close(); } catch (IOException e2) { MyError("Fatal Error", " onResume() " + e2.getMessage() + "."); } } MyThred = new ConnectedThred(btSocket); MyThred.start(); } @Override public void onPause() { super.onPause(); Log.d(LOG_TAG, "...In onPause()..."); if (MyThred.status_OutStrem() != null) { MyThred.cancel(); } try { btSocket.close(); } catch (IOException e2) { MyError("Fatal Error", " onPause() " + e2.getMessage() + "."); } } private void MyError(String title, String message){ Toast.makeText(getBaseContext(), title + " - " + message, Toast.LENGTH_LONG).show(); finish(); } // private class ConnectedThred extends Thread{ private final BluetoothSocket copyBtSocket; private final OutputStream OutStrem; private final InputStream InStrem; public ConnectedThred(BluetoothSocket socket){ copyBtSocket = socket; OutputStream tmpOut = null; InputStream tmpIn = null; try{ tmpOut = socket.getOutputStream(); tmpIn = socket.getInputStream(); } catch (IOException e){} OutStrem = tmpOut; InStrem = tmpIn; } public void run() { byte[] buffer = new byte[1024]; int bytes; while(true){ try{ bytes = InStrem.read(buffer); h.obtainMessage(ArduinoData, bytes, -1, buffer).sendToTarget(); }catch(IOException e){break;} } } public void sendData(String message) { byte[] msgBuffer = message.getBytes(); Log.d(LOG_TAG, "*** : " + message + "***" ); try { OutStrem.write(msgBuffer); } catch (IOException e) {} } public void cancel(){ try { copyBtSocket.close(); }catch(IOException e){} } public Object status_OutStrem(){ if (OutStrem == null){return null; }else{return OutStrem;} } } }
/* Settings: */ private static double Radius = 3.0; // private static double spdUnit = 3.6; // : 3.6 /, 1.0 /c
double Dis = (Double.parseDouble(value) * (Radius * 6.28)) / 100.0;
double Speed = Dis * spdUnit;
Source: https://habr.com/ru/post/J231461/More articles:快適な環境でのエコツーリズム車を避難させますか? 所有者に伝えるHPのオールフラッシュアレイと3PARストレージシステムへの10の大きな変更(パート2)Western Digital My Passport Essential SE 750 GB USB3.0ハードドライブの修理誰が誰ですか?動的磁気ストリップの詳細Cloud Storage CDNの起動IE8b1-生成されたコンテンツのテストをサポート開発者向けのシンプルなクラウドバックアップと、Yandex Tolstoy Startup Campでの作業方法Support-Libraryの新機能All Articles