This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

  • 0 voto(s) - 0 Media
  • 1
  • 2
  • 3
  • 4
  • 5
DUDA Como ejecutar dos sketches a la vez?
#1
Hola, soy nuevo en esto y no tengo ni remota idea de como ejecutar dos sketches. Alguien sabría decirme si hay alguna manera sencilla de hacer_lo?

Tengo de unir estos dos:

Primero:

#include "ph_grav.h"                                  //header file for Atlas Scientific gravity pH sensor
#include "LiquidCrystal.h"                            //header file for liquid crystal display (lcd)

String inputstring = "";                              //a string to hold incoming data from the PC
boolean input_string_complete = false;                //a flag to indicate have we received all the data from the PC
char inputstring_array[10];                          //a char array needed for string parsing
Gravity_pH pH = A0;                                  //assign analog pin A0 of Arduino to class Gravity_pH. connect output of pH sensor to pin A0
LiquidCrystal pH_lcd(2, 3, 4, 5, 6, 7);              //make a variable pH_lcd and assign arduino digital pins to lcd pins (2 -> RS, 3 -> E, 4 to 7 -> D4 to D7)


void setup() {
  Serial.begin(9600);                                //enable serial port
  pH_lcd.begin(16, 2);                                //start lcd interface and define lcd size (20 columns and 4 rows)
 
  pH_lcd.setCursor(0,2);                              //place cursor on screen at column 1, row 4
  pH_lcd.print("--------------------");              //display characters
  pH_lcd.setCursor(5, 1);                            //place cursor on screen at column 6, row 2
  pH_lcd.print("pH ");                        //display "pH Reading"
  if (pH.begin()) { Serial.println("Loaded EEPROM");}
  Serial.println(F("Use commands \"CAL,4\", \"CAL,7\", and \"CAL,10\" to calibrate the circuit to those respective values"));
  Serial.println(F("Use command \"CAL,CLEAR\" to clear the calibration"));
}


void serialEvent() {                                  //if the hardware serial port_0 receives a char
  inputstring = Serial.readStringUntil(13);          //read the string until we see a <CR>
  input_string_complete = true;                      //set the flag used to tell if we have received a completed string from the PC
}


void loop() {

  if (input_string_complete == true) {                //check if data received
    inputstring.toCharArray(inputstring_array, 30);  //convert the string to a char array
    parse_cmd(inputstring_array);                    //send data to pars_cmd function
    input_string_complete = false;                    //reset the flag used to tell if we have received a completed string from the PC
    inputstring = "";                                //clear the string
  }
  Serial.println(pH.read_ph());                      //output pH reading to serial monitor
  pH_lcd.setCursor(8, 2);                            //place cursor on screen at column 9, row 3
  pH_lcd.print(pH.read_ph());                        //output pH to lcd
  delay(1000);
}


void parse_cmd(char* string) {                      //For calling calibration functions
  strupr(string);                                  //convert input string to uppercase

  if (strcmp(string, "CAL,4") == 0) {              //compare user input string with CAL,4 and if they match, proceed
    pH.cal_low();                                  //call function for low point calibration
    Serial.println("LOW CALIBRATED");
  }
  else if (strcmp(string, "CAL,7") == 0) {          //compare user input string with CAL,7 and if they match, proceed
    pH.cal_mid();                                  //call function for midpoint calibration
    Serial.println("MID CALIBRATED");
  }
  else if (strcmp(string, "CAL,10") == 0) {        //compare user input string with CAL,10 and if they match, proceed
    pH.cal_high();                                  //call function for highpoint calibration
    Serial.println("HIGH CALIBRATED");
  }
  else if (strcmp(string, "CAL,CLEAR") == 0) {      //compare user input string with CAL,CLEAR and if they match, proceed





    pH.cal_clear();                                //call function for clearing calibration
    Serial.println("CALIBRATION CLEARED");
  }
}





Segundo:

#include <OneWire.h>

#include <LiquidCrystal.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 9

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

float tempC = 0;
float tempF = 0;

LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

void setup() {
  sensors.begin();
  lcd.begin(16,2);
  lcd.clear();
  pinMode(3, OUTPUT);
  analogWrite(3, 0);
  Serial.begin(9600);
 
}

void loop()  {
sensors.requestTemperatures();
  tempC = sensors.getTempCByIndex(0);
  tempF = sensors.toFahrenheit(tempC);
  delay(1000);
 
  Serial.println(tempC);
  lcd.setCursor(0,0);
  lcd.print(" grados: ");
  lcd.print(tempC);
  lcd.setCursor(0,1);
}



Gracias por vuestra atencion

Encontrado este método pero sigue sin responder

#include <EEPROM.h>
#define numSKETCH 3

void setup()
{
delay(500);
byte sketch = EEPROM.read(0);
sketch = (sketch >= 1 && sketch < numSKETCH ? sketch+1 : 1);
EEPROM.write(0, sketch);

switch(sketch)
{
case 1: sketch1(); break;
case 2: sketch2(); break;

}
}
void loop();





/**
* SKETCH 1
* pH
*/


#include "ph_grav.h" //header file for Atlas Scientific gravity pH sensor
#include "LiquidCrystal.h" //header file for liquid crystal display (lcd)
String inputstring = ""; //a string to hold incoming data from the PC
boolean input_string_complete = false; //a flag to indicate have we received all the data from the PC
char inputstring_array[10]; //a char array needed for string parsing
Gravity_pH pH = A0; //assign analog pin A0 of Arduino to class Gravity_pH. connect output of pH sensor to pin A0
LiquidCrystal pH_lcd(2, 3, 4, 5, 6, 7); //make a variable pH_lcd and assign arduino digital pins to lcd pins (2 -> RS, 3 -> E, 4 to 7 -> D4 toD7)

void sketch1()

{
//Setup:

{
Serial.begin(9600); //enable serial port
pH_lcd.begin(16, 2); //start lcd interface and define lcd size (20 columns and 4 rows)

pH_lcd.setCursor(0,2); //place cursor on screen at column 1, row 4
pH_lcd.print("--------------------"); //display characters
pH_lcd.setCursor(5, 1); //place cursor on screen at column 6, row 2
pH_lcd.print("pH "); //display "pH Reading"
if (pH.begin()) { Serial.println("Loaded EEPROM");}
Serial.println(F("Use commands \"CAL,4\", \"CAL,7\", and \"CAL,10\" to calibrate the circuit to those respective values"));
Serial.println(F("Use command \"CAL,CLEAR\" to clear the calibration"));
}


//Main loop:
while(true)
if (input_string_complete == true) { //check if data received
inputstring.toCharArray(inputstring_array, 30); //convert the string to a char array
parse_cmd(inputstring_array); //send data to pars_cmd function
input_string_complete = false; //reset the flag used to tell if we have received a completed string from the PC
inputstring = ""; //clear the string
}
Serial.println(pH.read_ph()); //output pH reading to serial monitor
pH_lcd.setCursor(8, 2); //place cursor on screen at column 9, row 3
pH_lcd.print(pH.read_ph()); //output pH to lcd
delay(1000);
}


void parse_cmd(char* string) { //For calling calibration functions
strupr(string); //convert input string to uppercase

if (strcmp(string, "CAL,4") == 0) { //compare user input string with CAL,4 and if they match, proceed
pH.cal_low(); //call function for low point calibration
Serial.println("LOW CALIBRATED");
}
else if (strcmp(string, "CAL,7") == 0) { //compare user input string with CAL,7 and if they match, proceed
pH.cal_mid(); //call function for midpoint calibration
Serial.println("MID CALIBRATED");
}
else if (strcmp(string, "CAL,10") == 0) { //compare user input string with CAL,10 and if they match, proceed
pH.cal_high(); //call function for highpoint calibration
Serial.println("HIGH CALIBRATED");
}
else if (strcmp(string, "CAL,CLEAR") == 0) { //compare user input string with CAL,CLEAR and if they match, proceed
pH.cal_clear(); //call function for clearing calibration
Serial.println("CALIBRATION CLEARED");
}
}



/**
* SKETCH 2
* Temperatura
*/
#include <OneWire.h>

#include <LiquidCrystal.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 9

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

float tempC = 0;
float tempF = 0;

LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

void sketch2()
{
//Setup:
{
sensors.begin();
lcd.begin(16,2);
lcd.clear();
pinMode(3, OUTPUT);
analogWrite(3, 0);
Serial.begin(9600);

}


//Main loop:
while(true)
{
sensors.requestTemperatures();
tempC = sensors.getTempCByIndex(0);
tempF = sensors.toFahrenheit(tempC);
delay(1000);

Serial.println(tempC);
lcd.setCursor(0,0);
lcd.print(" grados: ");
lcd.print(tempC);
lcd.setCursor(0,1);

}
}

Encontrado este método pero sigue sin responder

#include <EEPROM.h>
#define numSKETCH 3

void setup()
{
delay(500);
byte sketch = EEPROM.read(0);
sketch = (sketch >= 1 && sketch < numSKETCH ? sketch+1 : 1);
EEPROM.write(0, sketch);

switch(sketch)
{
case 1: sketch1(); break;
case 2: sketch2(); break;

}
}
void loop();





/**
* SKETCH 1
* pH
*/


#include "ph_grav.h" //header file for Atlas Scientific gravity pH sensor
#include "LiquidCrystal.h" //header file for liquid crystal display (lcd)
String inputstring = ""; //a string to hold incoming data from the PC
boolean input_string_complete = false; //a flag to indicate have we received all the data from the PC
char inputstring_array[10]; //a char array needed for string parsing
Gravity_pH pH = A0; //assign analog pin A0 of Arduino to class Gravity_pH. connect output of pH sensor to pin A0
LiquidCrystal pH_lcd(2, 3, 4, 5, 6, 7); //make a variable pH_lcd and assign arduino digital pins to lcd pins (2 -> RS, 3 -> E, 4 to 7 -> D4 toD7)

void sketch1()

{
//Setup:

{
Serial.begin(9600); //enable serial port
pH_lcd.begin(16, 2); //start lcd interface and define lcd size (20 columns and 4 rows)

pH_lcd.setCursor(0,2); //place cursor on screen at column 1, row 4
pH_lcd.print("--------------------"); //display characters
pH_lcd.setCursor(5, 1); //place cursor on screen at column 6, row 2
pH_lcd.print("pH "); //display "pH Reading"
if (pH.begin()) { Serial.println("Loaded EEPROM");}
Serial.println(F("Use commands \"CAL,4\", \"CAL,7\", and \"CAL,10\" to calibrate the circuit to those respective values"));
Serial.println(F("Use command \"CAL,CLEAR\" to clear the calibration"));
}


//Main loop:
while(true)
if (input_string_complete == true) { //check if data received
inputstring.toCharArray(inputstring_array, 30); //convert the string to a char array
parse_cmd(inputstring_array); //send data to pars_cmd function
input_string_complete = false; //reset the flag used to tell if we have received a completed string from the PC
inputstring = ""; //clear the string
}
Serial.println(pH.read_ph()); //output pH reading to serial monitor
pH_lcd.setCursor(8, 2); //place cursor on screen at column 9, row 3
pH_lcd.print(pH.read_ph()); //output pH to lcd
delay(1000);
}


void parse_cmd(char* string) { //For calling calibration functions
strupr(string); //convert input string to uppercase

if (strcmp(string, "CAL,4") == 0) { //compare user input string with CAL,4 and if they match, proceed
pH.cal_low(); //call function for low point calibration
Serial.println("LOW CALIBRATED");
}
else if (strcmp(string, "CAL,7") == 0) { //compare user input string with CAL,7 and if they match, proceed
pH.cal_mid(); //call function for midpoint calibration
Serial.println("MID CALIBRATED");
}
else if (strcmp(string, "CAL,10") == 0) { //compare user input string with CAL,10 and if they match, proceed
pH.cal_high(); //call function for highpoint calibration
Serial.println("HIGH CALIBRATED");
}
else if (strcmp(string, "CAL,CLEAR") == 0) { //compare user input string with CAL,CLEAR and if they match, proceed
pH.cal_clear(); //call function for clearing calibration
Serial.println("CALIBRATION CLEARED");
}
}



/**
* SKETCH 2
* Temperatura
*/
#include <OneWire.h>

#include <LiquidCrystal.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 9

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

float tempC = 0;
float tempF = 0;

LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

void sketch2()
{
//Setup:
{
sensors.begin();
lcd.begin(16,2);
lcd.clear();
pinMode(3, OUTPUT);
analogWrite(3, 0);
Serial.begin(9600);

}


//Main loop:
while(true)
{
sensors.requestTemperatures();
tempC = sensors.getTempCByIndex(0);
tempF = sensors.toFahrenheit(tempC);
delay(1000);

Serial.println(tempC);
lcd.setCursor(0,0);
lcd.print(" grados: ");
lcd.print(tempC);
lcd.setCursor(0,1);

}
}
  Responder
#2
Buenas tardes y Felices Pascuas.
Prueba con las instrucciones que se dan en este enlace

https://forum.arduino.cc/index.php?topic=242641.0

Un saludo.
  Responder
#3
(10-01-2021, 08:27 PM)asesorplaza1 escribió: Buenas tardes y Felices Pascuas.
Prueba con las instrucciones que se dan en este enlace

https://forum.arduino.cc/index.php?topic=242641.0

Un saludo.

Muchas Gracias

Lo he intentado varia veces pero sigue sin cuadrarme me podrías ayudar??

Código:
// Temperatura

#include <OneWire.h>

#include <LiquidCrystal.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 9

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

float tempC = 0;
float tempF = 0;

LiquidCrystal lcd(2, 3, 4, 5, 6, 7);;

// PHSENSOR

#include "ph_grav.h"                                  //header file for Atlas Scientific gravity pH sensor
#include "LiquidCrystal.h"                            //header file for liquid crystal display (lcd)

String inputstring = "";                              //a string to hold incoming data from the PC
boolean input_string_complete = false;                //a flag to indicate have we received all the data from the PC
char inputstring_array[10];                          //a char array needed for string parsing
Gravity_pH pH = A0;                                  //assign analog pin A0 of Arduino to class Gravity_pH. connect output of pH sensor to pin A0
LiquidCrystal pH_lcd(2, 3, 4, 5, 6, 7);              //make a variable pH_lcd and assign arduino digital pins to lcd pins (2 -> RS, 3 -> E, 4 to 7 -> D4 to D7)


void setup()
{
  // Temperatura
  {
    sensors.begin();
    lcd.begin(16, 2);
    lcd.clear();
    pinMode(3, OUTPUT);
    analogWrite(3, 0);
    Serial.begin(9600);

  }

  // PHSENSOR
  {
    Serial.begin(9600);                                //enable serial port
    pH_lcd.begin(16, 2);                                //start lcd interface and define lcd size (20 columns and 4 rows)

    pH_lcd.setCursor(0, 2);                            //place cursor on screen at column 1, row 4
    pH_lcd.print("--------------------");              //display characters
    pH_lcd.setCursor(5, 1);                            //place cursor on screen at column 6, row 2
    pH_lcd.print("pH ");                        //display "pH Reading"
    if (pH.begin()) {
      Serial.println("Loaded EEPROM");
    }
    Serial.println(F("Use commands \"CAL,4\", \"CAL,7\", and \"CAL,10\" to calibrate the circuit to those respective values"));
    Serial.println(F("Use command \"CAL,CLEAR\" to clear the calibration"));

    void serialEvent() {                                  //if the hardware serial port_0 receives a char
  inputstring = Serial.readStringUntil(13);          //read the string until we see a <CR>
  input_string_complete = true;                      //set the flag used to tell if we have received a completed string from the PC
}
  }

  void loop()

  // Temperatura


  sensors.requestTemperatures();
  tempC = sensors.getTempCByIndex(0);
  tempF = sensors.toFahrenheit(tempC);
  delay(1000);

  Serial.println(tempC);
  lcd.setCursor(0, 0);
  lcd.print(" grados: ");
  lcd.print(tempC);
  lcd.setCursor(0, 1);




  // PHSENSOR
  if (input_string_complete == true) {                //check if data received
    inputstring.toCharArray(inputstring_array, 30);  //convert the string to a char array
    parse_cmd(inputstring_array);                    //send data to pars_cmd function
    input_string_complete = false;                    //reset the flag used to tell if we have received a completed string from the PC
    inputstring = "";                                //clear the string
  }
  Serial.println(pH.read_ph());                      //output pH reading to serial monitor
  pH_lcd.setCursor(8, 2);                            //place cursor on screen at column 9, row 3
  pH_lcd.print(pH.read_ph());                        //output pH to lcd
  delay(1000);
}


void parse_cmd(char* string) //For calling calibration functions
}
strupr(string);                                  //convert input string to uppercase

if (strcmp(string, "CAL,4") == 0) {              //compare user input string with CAL,4 and if they match, proceed
  pH.cal_low();                                  //call function for low point calibration
  Serial.println("LOW CALIBRATED");
}
else if (strcmp(string, "CAL,7") == 0) {          //compare user input string with CAL,7 and if they match, proceed
  pH.cal_mid();                                  //call function for midpoint calibration
  Serial.println("MID CALIBRATED");
}
else if (strcmp(string, "CAL,10") == 0) {        //compare user input string with CAL,10 and if they match, proceed
  pH.cal_high();                                  //call function for highpoint calibration
  Serial.println("HIGH CALIBRATED");
}
else if (strcmp(string, "CAL,CLEAR") == 0) {      //compare user input string with CAL,CLEAR and if they match, proceed
  pH.cal_clear();                                //call function for clearing calibration
  Serial.println("CALIBRATION CLEARED");
}

}
  Responder
#4
Buenas tardes.

Ahora que he vuelto a leer tu pregunta, creo que no quieres ejecutar dos códigos a la vez, si no utilizar los dos códigos de dos sensores distintos en un solo Arduino, eso no se llama "ejecutar", ejecutar es el Arduino realice las instrucciones correspondientes, lo que quieres es "juntar" o "unir" los dos programas para leer los dos sensores en un Arduino solo.

Si no te explicas bien, no te puedo ayudar.

Mira este otro enlace, que no tienen nada que ver con lo que te he puesto antes

Para unir dos códigos

https://forum.arduino.cc/index.php?topic=416850.0

Para unir 4 códigos

https://forum.arduino.cc/index.php?topic=242641.0

Un saludo.
  Responder
#5
Muchas Gracias por tu ayuda, me has solucionado un gran problema que llevaba tiempo intentando solucionar
  Responder


Posibles temas similares…
Tema Autor Respuestas Vistas Último mensaje
  CONSULTA ¿como uno dos codigos en un solo codigo? laurangcard 1 943 18-11-2020, 10:03 PM
Último mensaje: asesorplaza1
  Controlar dos o tres cartuchos calefactores Shikva 1 1,338 23-09-2019, 07:23 AM
Último mensaje: bertonic
  CONSULTA El arduino compila una vez y despues saca error Jose Eugenio 0 1,054 20-10-2018, 09:26 PM
Último mensaje: Jose Eugenio
  CONSULTA ROBOT DOS MOTORES CON SERVO PARA DIRECCION Miguelmetro 1 1,497 23-04-2018, 06:30 PM
Último mensaje: Iván
  Conectar dos Arduinos entre si Electromecánico 4 7,415 02-01-2014, 10:30 PM
Último mensaje: giltesa