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