Merging Sketches - I'm missing something basic

Please bear with me as its 25 years since I last did programming. This seems like a truly basic question but I cannot find the explanation anywhere on-line. I have 2 sketches which work fine independently. One calculates values based on analogue inputs. The other is a webserver producing a basic web page. Both sketches include Setup and Loop statements. How do I combine the 2 sketches? I'm assuming cutting & pasting, essentially, but then I have 2 setups & 2 loops. Compiler doesn't like me renaming them setup1 & setup2 so what am I missing? Should I create a new setup that calls these setups? If so how?

Should I create a new setup that calls these setups? If so how?

You can’t just copy and past two sketches into one. There can only be one setup() and one loop() function in your combined sketch. So you need to combine the ‘content’ of the two setup() functions into one, making sure there are no duplicate variable names used or calling the duplicate functions twice when only once is needed. The same applies to the main loop() function, and you will have to determine if the structure of the two loop() functions will have to be modified to support the ‘content’ from the two separate loop() functions. So sorry, there is no automatic way to combine two sketches into one, you must do some analysis and decide how you will support both applications into one.

You must first understand what each sketch does in detail (how it does what it does) before you can combine them into a new single sketch.


Should I create a new setup that calls these setups? If so how?

As lefty says there is no substitute for understanding what the two do. But there is no misery in creating new functions you just write them. So suppose you name rename the setups and loops then you simply write:-

void setup(){
void loop(){

However, whether that does what you want it to depends entirely on the two sketches, most times it will not.