1

I already asked a question regarding communication between a Arduino and RPi through a USB serial connection and was able to solve major issues. Now my RPi and Arduino are talking to each other and getting along swimmingly... well almost.

I am using the following code for a simple task that could be almost completed without a Raspberry Pi (or without the Arduino). I have a ultrasonic sensor and a led hooked up to the Arduino and I am feeding the results to the RPi. The RPi is taking the results of the ultrasonic sensor and depending on the distance telling the Arduino to turn on the led. I am currently using the RPi as a way to start the process.

I have a raw input with the RPi "To start the ultrasonic sensor, please type "Start":". This works but after the two start communicating there is approximately a five second delay from when an object is close enough to light the led and when the led actually turns on. I would like to eventually use this on a robot and need a quicker reaction time.

Here is the Arduino Code:

int led = 10;
const int pingPin = 2;
long duration, distanceInches, distanceCm;


void setup()
{
  pinMode(led, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delay(2);
  digitalWrite(pingPin, HIGH);
  delay(5);
  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  distanceInches = microsecondsToInches(duration);
  distanceCm = microsecondsToCentimeters(duration);
  Serial.print(distanceInches);
  Serial.println();

  delay(200);

  char ch = Serial.read();
  if (ch == 'a') digitalWrite(led, HIGH);
  else if (ch == 's') digitalWrite(led, LOW);

  delay(10);
}

long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

Here is the code for the Raspberry Pi in Python 2.7:

import serial
import time
import sys

chip = '/dev/ttyACM0'
baud = 9600

ser = serial.Serial(chip, baud)
ser.flush()

def run():
     while True:
         x = ser.readline()
         y = int(x)
         if y < 14 :
             ser.write('a')
         else :
             ser.write('s')
         time.sleep(0.010)

def start():
    while True:
        on = raw_input('To start the ultrasonic sensor, please type "Start": ')
        if on == "Start":
            run()
        else :
            print('Invalid command. Please try again')


start()

Thanks in advance for any help and let me know if you need any clarification.

Robb

  • In the Pi code add a print(y) after the y=int(x) and a print("a")/print("s") after the write('a')/write('s') to see if the code is acting as you expect. – joan Dec 28 '14 at 9:48
  • It is difficult to make any sensible comment, as it is unclear what the code is meant to do (do you ever insert comments). The Arduino code looks like it will send some value (what?) on serial and wait for a response. The Pi code looks like it will wait for a line on serial and send a response. There is no synchronisation. Any error will result in a hang, and it is unclear which will start first, so both may be waiting for the other. – Milliways Dec 28 '14 at 21:59
  • 2
    Milliways you response is somewhat rude. A nicer way to ask your first question is: "I am unclear what your code intends to accomplish. Have you thought about adding comments to your code?" – wilsorob Dec 29 '14 at 1:56
  • Have you found a solution to this problem? I am having the same exact issue. I am suspecting its the pi's serial communication because with the serial communication on a windows machine using the Arduino's IDE console I was able to get instant feedback. – Sooon_mitch Jan 16 '16 at 2:21
  • @Sooon_mitch I'm starting to mess around with the Pi's serial port and an Arduino. I might encounter this real soon, and maybe fix it. – Gene Jan 16 '16 at 16:13
1

I had a similar problem once and I believe it was due to the fact that the serial data was overflowing a buffer on the RPi side. You may want implement a simple 'handshaking'. For example the Rpi sends a character (say the integer '1') on the serial port, when the Arduino sees a '1' it sends back the data from the ping sensor.

The way your code is currently is a bit of a problem because the Arduino is constantly sending data when it doesn't need to. Try it out and let me know if it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.