简体   繁体   中英

How to make Raspberry pi respond to Arduino code via USB connection

I have tried connecting Arduino to the raspberry Pi through USB cable. The Arduino board is connected to an Ultrasonic sensor and sends a serial message of either 0 or 1 based on whether it finds a barrier in a certain distance (very simple code). The problem is this : I'm trying to get the Raspberry Pi to read the Arduino code AND play a mp3 file at the same time but for some reason that doesn't seem to work ! I'm not sure if the problem lies in coding or If maybe it's impossible for Pi to respond to a message sent from Arduino to the serial monitor(It would be really sad if that's the case). Any help would be very appreciated

This is the Arduino Code (I'm using UNO board) :

    /*
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.

}

And this is the python code that I used in my Pi (I have Raspberry Pi 2): NOTE : I have commented the parts that don't work since I tried many different code combinations shown below

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 !

Firstly, your IF condition doesn't look right. I don't see how distance <= minimumRange means the path is clear.

Next, you are writing a line to the serial port; a line that could be either 0\\r\\n or 1\\r\\n . Then you're reading a line from the Arduino, returning one of the two aforementioned possibilities. You're then comparing the line you've read, to 1 . Neither 0\\r\\n nor 1\\r\\n is equal to 1 , so its no surprise that the condition is never true. You can fix this in a number of ways:

  • Change the Serial.println() to Serial.print()
  • Change arduinoSerialData.readline() to arduinoSerialData.readline().rstrip()
  • Change the condition to if 1 in myData:

Another thing to remember is, read() returns a bytes object in Python 3 not a string as in Python 2. So any comparison involving literals should make sure to include the requisite b'' envelope. Like, if you strip the CRLF from the read data, your condition should instead be if myData == b'1': .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM