How to put multiple programs in one sketch

Hello everyone viewing this. I recently encountered a problem with programming. How do you put many different programs in one Arduino Sketch NEATLY, and run the programs all at once on the arduino!?

1 Like

Also sorry for putting this in General Electronics I thought I put it in Programming Questions

well what i would do is copy the set up and put it in the set up mof the outher program then copy the loop and put it in the loop but us //comments to lable them ( puting a // before text makes the computer ignore it

amriechert:
Hello everyone viewing this. I recently encountered a problem with programming. How do you put many different programs in one Arduino Sketch NEATLY, and run the programs all at once on the arduino!?

You don't put multiple programs in one Arduino as such. The idea is to study the code of the various programs until you thoroughly understand it, then re-write it all into one program so that it performs how you want.

You could separate individual operations and write them into separate functions, then call those functions as needed.
It's all about learning how to write code, not simply copying and pasting snippets from other people's programs.

Temper temper. You don't need to yell.
And following up with a "Hey You" PM is a great way to win friends and influence people. Lighten up.

I gave you a good suggestion, and tried to help.

You could separate individual operations and write them into separate functions, then call those functions as needed.

And if you're "very knowledgable", you shouldn't need to ask this.
Good luck and goodbye.

(And perhaps you should have posted this in "Programming Questions", not "General Electronics".)

Split your code into files. Click the little ‘down arrow’ icon in the right top of the editor pane and click ‘new tab’.
Specify a sensible name (e.g. SerialOperations.cpp). Repeat and create a matching include file (e.g. SerialOperations.h).

Place all your serial functions in SerialOperations.cpp and place function prototypes for those functions in SerialOperations.h. You can add other definitions to SerialOperations.h as well (e.g. the size of the serial input buffer).

SerialOperations.cpp

#include "SerialOperations.h"

void SerialInit(int baudrate)
{
  Serial.begin(baudrate);
}

byte SerialWrite(byte *buffer, byte numbytes)
{
  return Serial.write(buffer, numbytes);
}

SerialOperations.h

#include <Arduino.h>

#define SERIALBUFFERSIZE 64

void SerialInit(int baudrate);
byte SerialWrite(byte *buffer, byte numbytes);

And your ino file

#include "SerialOperations.h"

byte inputbuffer[SERIALBUFFERSIZE];

void setup()
{
  SerialInit(9600);
}

void loop()
{
  SerialWrite(inputbuffer, SERIALBUFFERSIZE)
}

Create more files, e.g. for your logic.

PS
Next time, please don’t behave like a child that does not get its way. Screaming is not appreciated.

1 Like