cant delete the first hehe
hoping someone can compile my 3 ino code, thank youuu
The way things work is you go first!
combine multiple sketches arduino
start reading and get busy.
Usually combining two and more sketches requires a certain level of looking into each separately and developing some kind of understanding of how they work.
a7
That's not the way things go up here, sorry dude.
We're here to help others but you have to do your part too, no one here does the work for you. Give it a try and show us the results, and we'll be happy to help you.
You can break out your wallet and post your question here:
Or attempt it yourself and ask specific questions here.
If they are nicely written, then it is fairly straightforward. Here's some guidance on how it is done and a few working examples:
If the programs are not written to cooperate with other programs, you have to fix that issue first.
if you have a system with multiple devices you test each individually in a separate program
if all work OK you can start to create a single program with the combined functionality
- combine the separate programs into a single source file - can get very large making it difficult to find specific problems and easy when editing one section to corrupt others
- create a program where multiple source files are are compiled and linked into a single executable
consider these two programs which could represent test files for a pair of sensors
file test1.ino
// test1.ino
int test1_data=1; // data global to program
static int test1_local_data=2; // data local to test
// perform any initialization of sensors, IO, etc
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("\ntest 1 initialized");
}
// read sensors, perform IO, etc
void loop() {
Serial.print("test 1 loop test1_data ");
Serial.println(test1_data);
test1_data+=test1_local_data;
delay(1000);
}
a run gives
test 1 initialized
test 1 loop test1_data 1
test 1 loop test1_data 3
test 1 loop test1_data 5
test 1 loop test1_data 7
test 1 loop test1_data 9
test 1 loop test1_data 11
test 1 loop test1_data 13
test 1 loop test1_data 15
test 1 loop test1_data 17
and test2.ino
// test2.ino
int test2_data=25; // data global to program
static int test2_local_data=10; // data local to test
// perform any initialization of sensors, IO, etc
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("\ntest 2 initialized");
}
// read sensors, perform IO, etc
void loop() {
Serial.print("test 2 loop test2_data ");
Serial.println(test2_data);
test2_data+=test2_local_data;
delay(1000);
}
a run gives
test 2 initialized
test 2 loop test2_data 25
test 2 loop test2_data 35
test 2 loop test2_data 45
test 2 loop test2_data 55
test 2 loop test2_data 65
test 2 loop test2_data 75
test 2 loop test2_data 85
test 2 loop test2_data 95
the next task is to implement a single program which includes both test files
using File>New Sketch create and save a file to hold the overall test program, e.g. MainTest.ino in a directory MainTest
copy the files test1.ino and test2.ino into the directory MainTest – so it looks like
when MainTest.ino is opened in the Arduino IDE it displays tabs for MainTest.ino, test1.ino and test2.ino, e.g.
files MainTest.ino, test1.ino and test2.ino contain setup() and loop() functions which would cause multiple definition errors at link time
click on the tabs to edit the files to rename the setup() and loop() functions and do any other edits required, e.g. remove any delays(), unnecessary print statements, etc
test1.ino may now look like (header files covered in next section)
// test1.ino - test for multi test program
#include "test1.h" // include associated header file
int test1_data = 1; // data global to program
static int test1_local_data = 2; // data local to test
// perform any initialization of sensors, IO, etc
void setuptest1() {
Serial.begin(115200);
delay(1000);
Serial.println("\ntest 1 initialized");
}
// read sensors, perform IO, etc
void looptest1() {
test1_data += test1_local_data; // update test data
Serial.print("test 1 loop() test1_data "); // display it locally
Serial.println(test1_data);
//delay(1000);
}
and test2.ino
// test2.ino - test for multi test program
int test2_data = 25; // data global to program
static int test2_local_data = 10; // data local to test
// perform any initialization of sensors, IO, etc
void setuptest2() {
Serial.begin(115200);
delay(1000);
Serial.println("\ntest 2 initialized");
}
// read sensors, perform IO, etc
void looptest2() {
test2_data += test2_local_data; // update test data
Serial.print("test 2 loop() test2_data "); // display it locally
Serial.println(test2_data);
//delay(1000);
}
Header files – to pass information from the test1.ino and test2.ino files to MainTest.ino header files are used which contain function prototypes, external declarations, etc
e.g. test1.h (created in same directory MainTest.ino)
// test1.h - header file for source file test1.ino
// declare extern any variables in test1.ino to make global
extern int test1_data; // data global to program
// function prototype - perform any initialization of sensors, IO, etc
void setuptest1();
// function prototype - read sensors, perform IO, etc
void looptest1();
// add prototypes to other useful functions in test1.ino
and test2.h
// test2.h - header file for source file test2.ino
#include "test2.h" // include associated header file
// declare extern any variables in test2.ino to make global
extern int test2_data; // data global to program
// function prototype - perform any initialization of sensors, IO, etc
void setuptest2();
// function prototype - read sensors, perform IO, etc
void looptest2();
// add prototypes to other useful functions in test2.ino
MainTest.ino setup() can now be edited to initialize the test files and loop() to call to functions to get and display data, e.g.
// MainTest.ino - main file of multi test program
// header files contain function prototypes, declarations of external data, etc
#include "test1.h"
#include "test2.h"
// initialize program and call initialization functions
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("\n\nMainTest initialize");
setuptest1(); // initialize test1 and test2
setuptest2();
}
void loop() {
Serial.println("\nMainTest loop()");
// call functions to check for updated data
looptest1();
looptest2();
// display external data
Serial.print("MainTest test1_data ");
Serial.print(test1_data);
Serial.print(" test2_data ");
Serial.println(test2_data);
// add code to process data, upload to cloud, etc
delay(1000);
}
a run of the complete program gives (in effect running the original test programs in sequence within MainTest.ino)
MainTest initialize
test 1 initialized
test 2 initialized
MainTest loop()
test 1 loop() test1_data 3
test 2 loop() test2_data 35
MainTest test1_data 3 test2_data 35
MainTest loop()
test 1 loop() test1_data 5
test 2 loop() test2_data 45
MainTest test1_data 5 test2_data 45
MainTest loop()
test 1 loop() test1_data 7
test 2 loop() test2_data 55
MainTest test1_data 7 test2_data 55
MainTest loop()
test 1 loop() test1_data 9
test 2 loop() test2_data 65
MainTest test1_data 9 test2_data 65
MainTest loop()
test 1 loop() test1_data 11
test 2 loop() test2_data 75
MainTest test1_data 11 test2_data 75
MainTest loop()
test 1 loop() test1_data 13
test 2 loop() test2_data 85
MainTest test1_data 13 test2_data 85
MainTest loop()
test 1 loop() test1_data 15
test 2 loop() test2_data 95
MainTest test1_data 15 test2_data 95
MainTest loop()
test 1 loop() test1_data 17
test 2 loop() test2_data 105
the Arduino IDE displays tabs for the .ino and .h files
once the tests run OK in a single program the files can be edited to suit the system specification, e.g. functions in test1.ino could call functions in test2.ino, etc