Arduino Communicate Pi 3. How do you code this?

I got programmer’s block. I need help…I’m looking do something like this.

How do you code this?

This is Arduino Uno

#define trigPin 10
#define echoPin 11
#define RedLed 13
#define greenLed 12

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(RedLed, OUTPUT);
  pinMode(greenLed, OUTPUT);

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW); 
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance < 4) {  // This is where the RED LED On/Off happens
       Serial.print("Mars Landed");
      digitalWrite(RedLed,HIGH); // When the Red condition is met, the Green LED should turn off
  else {
  if (distance >= 200 || distance <= 0){
      Serial.println("Out of range");
  else {
        // Send distance to Raspberry Pi 3
        Serial.println(" cm");

///// Pi 3 (Particle WebIDE) ////////
Arduino Uno RX and Tx to Pi 3 attached GPIO 2 & 3

0 RX = GPIO 2
1 TX = GPIO 3

Opening Raspberry Pi 3 Terminal

void setup()                    // run once, when the sketch starts
  Serial.begin(9600);           // set up Serial library at 9600 bps

void loop()                       // run over and over again
   // Receive Arduino Uno Ping distance

   Serial.println(" cm");

Landing a Rover on Mars :sweat_smile:?

Have you tried contacting NASA?

In all seriousness, what exactly are you trying to achieve? Try explaining that as concisely as possible without having us watch a 2 minute video and still have us guessing.

1 Like

What am I trying to achieve? make my own fake mars landing simulation for fun.
I want to make rocket landing X position without using parachute.

Landing a Rover on Mars :sweat_smile:?
Actually, no… But somewhere on earth. Baseball field soil and blue screen.
Small model like Godzilla toys.

Have you tried contacting NASA?

I need a Uno to commicate Pi. How do I get the ping sensor reading on pi3?

There are several things going on in that video. One is computer vision to recognize location. That alone is non-trivial, and, fortunately, it doesn’t look like that’s what you’re trying to do, based on your Fritzing diagram. There’s logic there to control the rocket’s landing location, which is based on an internal map it has, the flight plan that is set, and the results of the computer vision process that determines where the rocket actually is at a given moment. That code would then direct the rocket to change its location however it should be done for that rocket. It looks like nozzle deflection which is yet another non-trivial thing in model rocketry (as I understand it.)

To simplify all of that, I think (again, going by your Fritzing diagram) that you’re looking to control something based on a distance measurement.

It also seems like English isn’t your first language, so that may be confusing things a little.

The Raspberry Pi doesn’t have analog input; is that why you’re using the Arduino?

If so, you need to write a sketch on the Arduino that reads the analog value from the sensor and sends it to the Pi digitally. Serial, SPI, I2C, or your own digital protocol will all work there. Explaining how to do that is kind of outside the scope of the help this forum is meant to provide.

On the ParticleBerry Pi side, you’ll need to read that value coming in on whatever interface you’ve chosen, then make decisions and control whatever you’re going to control based on that data.

Does this help? I hope it helps, because that’s about all I can offer.

I guess another thing to mention would be that a Particle Photon would probably be a better choice for this application, because it has analog input and you can really tighten down the feedback loop when you’re only using a single microcontroller to do the sensing and the control. With two microcontrollers, you’re going to have communication latency which could cause issues depending on how good of a developer you are.