2016-10-21 5 views
0

나는 USB 케이블을 통해 라즈베리 파이에 Arduino를 연결하려고 시도했다. Arduino 보드는 초음파 센서에 연결되어 특정 거리 (매우 간단한 코드)의 장벽을 찾았는지 여부에 따라 0 또는 1 중 하나의 연속 메시지를 전송합니다. 문제는 이것입니다 : 저는 Arduino 코드를 읽고 동시에 MP3 파일을 재생할 수있는 Raspberry Pi를 얻으려고 노력하고 있지만 어떤 이유로 작동하지 않는 것 같습니다! 문제가 코딩에 있는지 또는 Pi가 Arduino에서 직렬 모니터로 보낸 메시지에 응답하는 것이 불가능한 지 여부는 확실하지 않습니다 (이 경우 정말 슬플 것입니다). 어떤 도움은 매우라즈베리 파이를 USB 연결을 통해 Arduino 코드에 응답하는 방법

이는 아두 이노 (나는 UNO 보드를 사용하고 있습니다) 코드입니다 주시면 감사하겠습니다 :

/* 
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가) : 참고 : 나는 첫째

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

들여 쓰기는 파이썬의 코드입니다. 그것의 부족은 당신의 코드를 이해하기 어렵게 만들뿐 아니라 오해를 불러 일으 킵니다. 질문을 수정하십시오. – TisteAndii

답변

0

아래와 같이 여러 가지 코드 조합을 시도하기 때문에 작동하지 않는 부분을 주석 한 조건은 바로 보이지 않는 경우. distance <= minimumRange 경로가 명확하지 않다는 것을 알지 못합니다.

다음으로 직렬 포트에 회선을 쓰고 있습니다. 라인은 0\r\n 또는 1\r\n 일 수 있습니다. 그런 다음 Arduino에서 선을 읽고 두 가지 가능성 중 하나를 반환합니다. 그러면 읽은 행을 1과 비교할 것입니다. 0\r\n1\r\n1과 같지 않으므로 조건이 절대로 맞지 않습니다. 이다 read() 반환을 기억 if 1 in myData:

또 다른 한가지에

  • 변경 Serial.println()arduinoSerialData.readline() 조건 변경
  • arduinoSerialData.readline().rstrip()로 변경
  • Serial.print()에 : 당신은 여러 가지 방법으로이 문제를 해결할 수 있습니다 Python 3에서 bytes 객체는 Python 2 에서처럼 문자열이 아닙니다. 따라서 리터럴을 포함하는 모든 비교에는 반드시 b'' 봉투. 마찬가지로 CRLF를 읽은 데이터에서 제거하면 조건은 if myData == b'1':이되어야합니다.

+0

고마워요. 내 친구. 정말 도움이되었습니다. –

+0

@ AhmedMo'nis 여러분을 환영합니다. 당신이 도움이 되었기 때문에 Upvote와 대답을 받아 들인다. – TisteAndii

관련 문제