communication with Pure Data and a relay

Hi! I’m new in Arduino’s world and I have a lot of problems that I’d like you to help me.

The thing is, I’m working with 4 ultrasonic sensors and a relay. I need to send the numbers of the sensors to pure data to make some audio process. Now about the relay, I need it to be activated when the 4 sensors are giving numbers under 10, but just for 800milliseconds because I’m using 220v and the components may break.

I have tried with so many codes, but as I don’t understand the logic of Arduino, I can’t change anything without making the code fall.

A code that was almost useful is one that I got from this forum, I’ll put it in this question if you want some guide to know what I’m looking for.

#include <Boards.h>
#include <Firmata.h>
#include <FirmataConstants.h>
#include <FirmataDefines.h>
#include <FirmataMarshaller.h>
#include <FirmataParser.h>
#include <NewPing.h> //
#define SONAR_NUM 2 // Number or sensors.
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).
#define TRIGGER_PIN1 1 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define TRIGGER_PIN2 3 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN1 2 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define ECHO_PIN2 4 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#define RELE 9 // Salida para el relevador

unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
unsigned long tstart;
unsigned int cm[SONAR_NUM]; // Where the ping distances are stored.
unsigned int Menor;
uint8_t currentSensor = 0; // Keeps track of which sensor is active.
bool Verifico5S = false;
bool activoRele = false;

NewPing sonar[SONAR_NUM] = { // Sensor object array.
NewPing(TRIGGER_PIN1, ECHO_PIN1, MAX_DISTANCE), // Each sensor’s trigger pin, echo pin, and max distance to ping.

void setup() {
Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
pingTimer[0] = millis() + 75; // First ping starts at 75ms, gives time for the Arduino to chill before starting.
for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.
pingTimer = pingTimer[i - 1] + PING_INTERVAL;
/* 1. Cuando estén midiendo la distancia y la distancia medida en alguno de los 2 sensores sea menor a 10 cm

  • durante un tiempo de 5 segundos, poner en alto la señal del relevador y*
    1. Si la distancia en alguno de los 2 es mayor a 10 poner en bajo la señal,*
      void loop() {
  • for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.*
    _ if (millis() >= pingTimer*) { // Is it this sensor’s time to ping?_
    pingTimer += PING_INTERVAL * SONAR_NUM; // Set next time this sensor will be pinged.
    if (i == 0 && currentSensor == SONAR_NUM - 1)
    _ oneSensorCycle(); // Sensor ping cycle complete, do something with the results._
    sonar[currentSensor].timer_stop(); // Make sure previous timer is canceled before starting a new ping (insurance).
    _ currentSensor = i; // Sensor being accessed.
    cm[currentSensor] = 0; // Make distance zero in case there’s no ping echo for this sensor._
    sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
    _ }
    if (Menor < 10 && !Verifico5S) { // debo iniciar timer por 5 seg*
    * Verifico5S = true;
    tstart = millis()+ 5000UL;
    if (Menor > 10) {
    activoRele = false;
    Verifico5S = false;
    if (Verifico5S) // si estamos a menos de 10 cm verificar 5 segundos*
    * if (millis() >= tstart) { // pasaron 5 seg?
    activoRele = true;
    if (activoRele)
    digitalWrite(RELE, HIGH);
    * digitalWrite(RELE, LOW);
    void echoCheck() { // If ping received, set the sensor distance to array._

    if (sonar[currentSensor].check_timer())
    cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
    void oneSensorCycle() { // Sensor ping cycle complete, do something with the results._

    for (uint8_t i = 0; i < SONAR_NUM; i++) {
    _ if (i == 0)
    Menor = cm[0]; // fijo a Menor con el primer dato;
    if (cm < Menor)
    Menor = cm; // Me quedo con el menor;
    Serial.print("cm ");

Why are you #including all the Firmata libraries?

Why did you fail to read the stickies, and post your code PROPERLY? Your code isn't in italics, is it?