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