Como agrego 2 códigos a 1 programa?

Buenas! estoy teniendo un problema al agregar 2 códigos a 1 Nano Arduino, ambos los hice de manera separada pero quiero que ambos parámetros salgan al ejecutar en 1 solo programa. Les explico mi proyecto, que seria un medidor de pH y un sensor ultrasónico sumergible, estos son los códigos si alguien puede ayudarme rápido porfavor!

// Define Trig and Echo pin:
#define trigPin 2
#define echoPin 3

// Define variables:
long duration;
int distance;

void setup() {
  // Define inputs and outputs
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  // Begin Serial communication at a baudrate of 9600:
  Serial.begin(9600);
}

void loop() {
  // Clear the trigPin by setting it LOW:
  digitalWrite(trigPin, LOW);
  
  delayMicroseconds(5);

 // Trigger the sensor by setting the trigPin high for 10 microseconds:
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Read the echoPin. pulseIn() returns the duration (length of the pulse) in microseconds:
  duration = pulseIn(echoPin, HIGH);
  
  // Calculate the distance:
  distance = duration*0.034/2;
  
  // Print the distance on the Serial Monitor (Ctrl+Shift+M):
  Serial.print("La distancia del suelo es = ");
  Serial.print(distance);
  Serial.println(" cm");
  
  delay(100);
}
// pHRead.ino

// Constants:-
const byte pHpin = A0;// Connect the sensor's Po output to analogue pin 0.

// Variables:-
float Po;

void setup()
{
Serial.begin(9600);
}

void loop()
{
Po = (1023 - analogRead(pHpin)) / 73.07; // Read and reverse the analogue input value from the pH sensor then scale 0-14.
 Serial.print("El pH es = ");
Serial.println(Po, 2);// Print the result in the serial monitor.
delay(1 000);// Take 1 reading per second.
}

El primero responde 10 veces por segundo, el segundo, solo 1 vez. ¿Con qué frecuencia debe hacer el programa con ambos códigos?

Para unir dos códigos siempre tienes que hacer lo mismo:

  • Lo que tengas antes del setup en ambos códigos lo colocas antes del setup del nuevo código
  • Lo que tengas en los dos setup lo colocas en el setup del núevo código

Evitando en estos dos casos duplicidad de librerias, declaraciones, números de pines o variables duplicadas...etc. Tendras que cambiar lo necesario.
Para el loop cambia en consecuencia con los posibles cambios realizado antes, los números de pines variables..etc
El resto es lo complicado has de saber que quieres hacer y como hacerlo.
Para tu caso creo que esto es lo que querías.

// variales temporales
unsigned long marca1, marca2;

// Define Trig and Echo pin:
#define trigPin 2
#define echoPin 3

// Define variables:
long duration;
int distance;
float Po;

// Constantes:-
const byte pHpin = A0;// Connect the sensor's Po output to analogue pin 0.



void setup() {
  // Define inputs and outputs
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  // Begin Serial communication at a baudrate of 9600:
  Serial.begin(9600);
}

void loop() {
  if (millis() - marca1 >= 100) {// Take 10 reading per second.
    marca1 = millis();
    // Clear the trigPin by setting it LOW:
    digitalWrite(trigPin, LOW);

    delayMicroseconds(5);

    // Trigger the sensor by setting the trigPin high for 10 microseconds:
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

    // Read the echoPin. pulseIn() returns the duration (length of the pulse) in microseconds:
    duration = pulseIn(echoPin, HIGH);

    // Calculate the distance:
    distance = duration * 0.034 / 2;

    // Print the distance on the Serial Monitor (Ctrl+Shift+M):
    Serial.print("La distancia del suelo es = ");
    Serial.print(distance);
    Serial.println(" cm");
  }
  if (millis() - marca2 >= 1000) {// Take 1 reading per second.
    marca2 = millis();
    Po = (1023 - analogRead(pHpin)) / 73.07; // Read and reverse the analogue input value from the pH sensor then scale 0-14.
    Serial.print("El pH es = ");
    Serial.println(Po, 2);// Print the result in the serial monitor.
  }
}

1 Like

ambos pueden responder a la misma frecuencia, en el código solo puse de ejemplo ese numero...

muchas gracias! entendí perfectamente

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.