2016-10-21 18 views
0

私は、USBケーブルでArduinoとラズベリーPiを接続しようとしました。 Arduinoボードは超音波センサーに接続され、ある距離(非常に簡単なコード)で障壁が見つかったかどうかに基づいて、0または1のシリアルメッセージを送信します。問題はこれです:私はArduinoのコードを読んで、同時にMP3ファイルを再生するためにラズベリーパイを取得しようとしていますが、何らかの理由で動作していないようです!私は問題がコーディングにあるのか、PiがArduinoからシリアルモニタに送られたメッセージに応答するのが不可能なのかどうかは分かりません(本当に悲しいでしょう)。ラズベリーパイをArduinoコードにUSB接続で対応させる方法

/* 
HC-SR04 Ping distance sensor: 

VCC to Arduino 

Vin GND to Arduino GND 

Echo to Arduino pin 12 

Trig to Arduino pin 11 */ 

#include <NewPing.h> //downloaded from the internet & unzipped in libraries folder in Arduino Directory 

#define TRIGGER_PIN 11 // Arduino pin tied to trigger pin on the ultrasonic sensor. 

#define ECHO_PIN 12 // Arduino pin tied to echo pin on the ultrasonic sensor. 

int maximumRange = 70; // Maximum range needed 

int minimumRange = 35; // Minimum range needed 

long duration, distance; // Duration used to calculate distance 

void setup() { 

Serial.begin (9600); 

pinMode(TRIGGER_PIN, OUTPUT); 

pinMode(ECHO_PIN, INPUT); 

} 

void loop() { 

/* The following trigPin/echoPin cycle is used to determine the distance of the nearest object through reflecting soundwaves off of it */ 

digitalWrite(TRIGGER_PIN, LOW); 

delayMicroseconds(2); 

digitalWrite(TRIGGER_PIN, HIGH); 

delayMicroseconds(10); 

digitalWrite(TRIGGER_PIN, LOW); 

duration = pulseIn(ECHO_PIN, HIGH); 

distance = (duration/2)/29.1; //formula to convert the value measured by the ultrasonic sensor into centimeters 

if (distance >= maximumRange || distance <= minimumRange) 

{ 

Serial.println("0"); //means the path is clear 

} 

else { 

Serial.println("1"); //means there is an obstacle in front of the ultrasonic sensor ! 

} 

delay(50); //Delay 50ms before next reading. 

} 

そして、これは私が(私はラズベリーパイ2を持っている)私のパイで使用されるPythonコードです:すべてのヘルプは非常にこれは、Arduinoのコード(私はUNOボードを使用している)である

をいただければ幸いです: 注:私は

まず
import serial 
import RPi.GPIO as GPIO 
import sys 
import os 
from subprocess import Popen 
from subprocess import call 
import time 
import multiprocessing 

GPIO.setmode(GPIO.BOARD) 
GPIO.setwarnings(False) 

arduinoSerialData = serial.Serial('/dev/ttyACM0', 9600) 

while True: 



time.sleep(0.01) 
if(arduinoSerialData.inWaiting()>0): 
myData = arduinoSerialData.readline() 
print(myData) 

if myData == '1': #THIS IS WHERE THE PROBLEMS START 
#os.system('omxplayer sound.mp3') #tried this didn't work 
#os.system('python player.py') #which is basically a python program with the previous line in it, also not working! 
# I even tried enclosing that part (after if myData == '1') in a while loop and also didn't work ! 
+2

インデントはPythonのコードです。その欠如は、あなたのコードを理解するのが難しいだけでなく、誤解にもつながります。あなたの質問を編集してください。 – TisteAndii

答えて

0

の下に示し、多くの異なるコードの組み合わせを試してみましたので、私は動作しない部分をコメントしている、あなたのIF条件は、右を見ていません。私はどのように表示されませんdistance <= minimumRangeは、パスが明確であることを意味します。

次に、シリアルポートに回線を書きます。行は0\r\nまたは1\r\nのいずれかになります。その後、あなたはArduinoから1つの行を読み、2つの前述の可能性のうちの1つを返します。読んだ行と1を比較しています。 0\r\n1\r\n1と同じではないため、条件が真でないことは驚きではありません。あなたはいくつかの方法でこの問題を解決することができます

  • 変更Serial.println()
  • Serial.print()に変更 arduinoSerialData.readline()は覚えておくべきもう一つは、 read()戻っある if 1 in myData:

に条件変更

  • arduinoSerialData.readline().rstrip()にPython 3の bytesオブジェクトはPython 2のような文字列ではありません。したがって、リテラルを含む任意の比較では、必要なものが含まれていることを確認してください。 b''封筒。同様に、読み込みデータからCRLFを取り除いた場合、条件は if myData == b'1':でなければなりません。

    +0

    ありがとう私の友人。それは本当に役に立ちました。 –

    +0

    @ AhmedMo'nisようこそ。あなたがそれが役に立つと思ったので、アップアップし、答えを受け入れる。 – TisteAndii

    関連する問題