Multiple Sketches on one UNO

My interest is using the Arduino units for visual effects and possibly some control projects for a model railway layout.

I should also say that I am a raw beginner.

There are quite a few small projects available for downloading but I would like to install multiple sketches on one UNO or similar rather than have numerous arduinos with only a fraction of their capabilities utilsed dotted around my layout.

I have attached a doc file showing two small sketches (by Geoff Bunza) that I would like to start with. I have tried googling the problem but got no-ware ( that was within my understanding) as to when a program finishes and the next one starts. I realize that I have to change some pin numbers but it is the program structure that is holding me back.

Gareth

LED_FIRE_effect_2.doc (16 KB)

I would like to install multiple sketches on one UNO or similar rather than have numerous arduinos with only a fraction of their capabilities utilsed dotted around my layout.

I'd like to win the lottery without having to waste money on a lottery ticket that has the wrong numbers. Neither of us is going to get what we want.

You CAN have an Arduino doing more than one thing, one after another, so that the things appear to be happening "at the same time", but those things need to all be in one sketch.

(deleted)

Do a search for merging codes.

+1 do not attach doc files.
+1 merging code is what you want.

mcnaugg:
There are quite a few small projects available for downloading but I would like to install multiple sketches on one UNO or similar rather than have numerous arduinos with only a fraction of their capabilities utilsed dotted around my layout.

You can have several functions on one Arduino that each do different things which can give the same result as having multiple sketches.

Have a look at how the code is organised in Several Things at a Time and in Planning and Implementing a Program

...R

Thanks for the feedback. Point taken about not downloading a .doc file, I did check what files were permissible since this was my first post to the forum.

It seems that what I should be doing is to do a search on "Merging Codes". Problem is that I can't seem to find the "search box". On other forums (model railway etc) that I subscribe to there is normally a box in the top right corner where one can enter the search parameters but I can't find one here. Am I just being stupid?

Gareth

Have you tried clicking on the magnifying glass icon top/right of the forum screen ?

Hi there

I tried that before I posted the reply but nothing happened. The shopping basket activated and also the profile buttons but not the magnifying glass. Now it does open. I think its maybe time to put me out to grass.

Gareth

You can, of course, use google to do your searching. Add site:arduino.cc to the search phrase if you wish to limit where google searches to just this site.

Forgive the delay but I have spent some time reading the earlier posts regarding “Merging Programs” and have come to the realization that I do not want to merge the programs but rather to run two separate programs in sequence. The programs (Sketches) do not interact with each other. Given the simple tasks I want to use them for I don’t think that any delays in running sequentially would be noticeable. As suggested I have included the two program examples. I have set the hardware up on a breadboard and individually they both work fine.

First Program

// MRH Welder Indirect View with Control Pin
// G. Bunza 2016
//
#define welderpinWH 12 // WHITE LED (Anode/Plus side) attaches to this pin
#define welderpinBL 11 // BLUE LED (Anode/Plus side) attaches to this pin
#define control_pin 14 //Control Pin is Pin 14 (A0) Low/Ground is OFF
// OPEN (Unattachd) Pin is ON
void setup()
{
pinMode(welderpinWH, OUTPUT);
pinMode(welderpinBL, OUTPUT);
digitalWrite(welderpinWH, LOW);
digitalWrite(welderpinBL, LOW);
pinMode (control_pin, INPUT_PULLUP);
}
void loop()
{
int weld_tim;
weld_tim = random (21,45);
if (digitalRead(control_pin) == HIGH) {
for (int i=1; i<= weld_tim; i++) run_welder();
delay (3300);
weld_tim = random (33,75);
for (int i=1; i<= weld_tim; i++) run_welder();
delay(10000+random(4000,9000));
}
}
void run_welder () {
digitalWrite(welderpinWH, HIGH);
digitalWrite(welderpinBL, HIGH);
delay (random (10,120));
digitalWrite(welderpinWH, LOW);
digitalWrite(welderpinBL, HIGH);
delay (random (20,150));
digitalWrite(welderpinWH, HIGH);
digitalWrite(welderpinBL, LOW);
delay (random (30,180));
digitalWrite(welderpinWH, HIGH);
digitalWrite(welderpinBL, LOW);
delay (random (10,114));
digitalWrite(welderpinWH, LOW);
digitalWrite(welderpinBL, HIGH);
delay (random (20,155));
digitalWrite(welderpinWH, HIGH);
digitalWrite(welderpinBL, HIGH);
delay (random (10,140));
digitalWrite(welderpinWH, LOW);
digitalWrite(welderpinBL, LOW);
}

Second Program

// Fire Light with Control Pin
// G. Bunza 2016
//
#define FireRED 12 // RED LED (Anode/Plus side) attaches to this pin
#define FireYEL 11 // YELLOW LED (Anode/Plus side) attaches to this pin
#define FireWHT 10 // WHITE LED (Anode/Plus side) attaches to this pin
#define change_delay 60
int control_pin = 14; //Control Pin is Pin 14 (A0) Low/Ground is OFF
// OPEN (Unattachd) Pin is ON
void setup()
{
pinMode(FireRED, OUTPUT);
pinMode(FireYEL, OUTPUT);
pinMode(FireWHT, OUTPUT);
digitalWrite(FireRED, LOW);
digitalWrite(FireYEL, LOW);
digitalWrite(FireWHT, LOW);
pinMode (control_pin, INPUT_PULLUP);
}
void loop()
{
digitalWrite(FireRED, tvtuning(84));
digitalWrite(FireYEL, tvtuning(77));
digitalWrite(FireWHT, tvtuning(5));
delay (change_delay);
}
byte tvtuning (int time_on) {
if (digitalRead(control_pin) == LOW) return 0; //Eventually all OFF
if (random (0,100)< time_on) return 1; // ON time_on% of the time
else return 0;
}

I do not want to merge the programs

You may not want to but you have to.

Start a new sketch and copy the setup() function from each of them into it with a new name, perhaps setup1() and setup2(). Create a setup() function in the new program that calls setup1() and setup2()

Copy the global variable declarations from both programs into the new one and resolve any conflicts of variable names and use of pins by changing names and/or pin assignments.

Copy the loop() functions from each program into the new one giving them new names, perhaps loop1() and loop2(). Create a loop() function in the new program that calls loop1() and loop2().

If you have resolved any conflicts of variable names and use of pins the program will compile. It is likely, however, that there will still be errors which the compiler will report and which need fixing.

mcnaugg:
and have come to the realization that I do not want to merge the programs but rather to run two separate programs in sequence.

That is not possible unless you stop the Arduino to upload the second program when you are finished with the first program.

However, as I said in Reply #5 you can organize a single program to an Arduino which has two separate functions which could be organized to operate one after the other.

Looking at the code in your Reply #10 it seems that one part is about welding and the other is about TV tuning. Strange bedfellows!

Perhaps you can give a better description of what you are trying to achieve - what the programs are for, why the should be on the same Arduino etc.

...R

PS... please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum

Sorry for the delay but I have spent some time reading and re-reading the replies. I now see the reason why I will have to merge the two programs if I want to run them on one Arduino. Before I went on Pension (long time ago) I used an Air-conditioning Management system that was a modified version of BASIC and this utilised sub-routines and this is where I got the idea of running two separate programs on the one unit i.e. one controller doing several tasks.

The reason for wanting to run multiple programs on the one unit was to reduce the number of Arduinos (although very cheap) mounted under my track layout and also reduce the amount of wiring that I would have to run to the units.

As regards the names of the sketches these are to be used merely for lighting effects on the layout. The welder sketch is to simulate an electric welder flash in a model garage and the second is to simulate a coal or wood fire either in a building or outside. These are just a few that I would like to incorporate into my layout (not all on the same Arduino) as I build the layout.

As I said at the start I am a raw beginner and more of an end user rather than a programmer. The two sketches are taken from a USA magazine(I think) called Model Rail Hobbyist (Dec 2016 edition) and were included in an article written by Geoff Bunza.

I will take the advice given and try working through the sketches to combine the two and resolve any conflicts that arise.

Many thanks for the feedback

G

mcnaugg:
a modified version of BASIC and this utilised sub-routines

Functions are the same as sub-routines

...R

Ah-Ah

Thanks

G

Hi there.

I am a beginner too so I know what you are going through here.

I have had some similar issues- How could i use something as complex as an arduino for just one little task?

Then I realised that nanos are like $3 off ali express! they are compact and cheap enough that just using a second (or whatever) is well worth the expense due to the lesser brain power/stress required, HAHA

flyingbrick88:
they are compact and cheap enough that just using a second

That's fine if all the activities are completely separate. But if it is necessary to co-ordinate the activities then the extra effort to communicate between 2 or more Arduinos is probably more trouble than putting all the code on one of them.

...R

oh of course- however if its for something as basic as lighting on a train set and you just want to achieve the end goal with the most minimum of head scratching......