How do i run two sketches simultaneously. HELP

Hi
im just starting out in the Arduino universe and im working on a cosplay project, ive got two sketch that i would like to run on the same board can anyone help please.

Sketch one it using a PWM module for some servos.

#define pot1 A1
#define pot2 A2
int valPot1;
int valPot2;
void setup() {

HCPCA9685.Init(SERVO_MODE); // Set to Servo Mode

HCPCA9685.Sleep(false); // Wake up PCA9685 module

}

void loop() {

int valPot1 = analogRead(pot1);
int valPot2 = analogRead(pot2);

valPot1 = map(valPot1, 0, 1023, 20, 400);
valPot2 = map (valPot2, 0, 1023, 10, 400);

HCPCA9685.Servo(0,valPot1);
HCPCA9685.Servo(1,valPot2);
delay(1);

}

sketch two is just a simple multiple led candle effect.

void setup() {
// put your setup code here, to run once:
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
analogWrite(9, random(120)+135);
analogWrite(10, random(120)+135);
analogWrite(11, random(120)+135);
delay(random(100));
}

Help please

Simply said, you can't :wink:

More into detail, you learn to program and plan a project. And to make it even possible to do two things at the "same time" you may not use blocking functions of which delay() is the biggest.

See this:
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

.

use two controllers.....

LandonW:
use two controllers.....

Great answer if you are an idiot. Their is no need for two controllers if you learn how to code.

Grumpy_Mike:
Great answer if you are an idiot. Their is no need for two controllers if you learn how to code.

This forum is not for comments like this, if you are going to post something please make it something helpful.
Constructive criticism is also acceptable. However this was neither. If you would like to try again that's fine but choose from the following.

A) Helpful comments
B) Constructive comments
C) A link of a helpful comment or instructions
D) NO comments

Thanks MIKE

Beginners often think a solution to a problem is to have another processor. It almost never is.

This is a quality forum with quality answers. If you post an answer that is crap expect to be attacked for it. If you post a question however simple you will get a quality answer.

This thread already contained quality answers then you came up with an idiotic answer. That is why you were quite rightly shot down.

C) A link of a helpful comment or instructions

If you bothered to follow the link given in larryd's answer you will find that I wrote that page. So in what way am I not being helpful?

Your post adds little to the main problem of answering the OP's question.

That's how things work around here. It is not a blind leading the blind forum it is a forum for learning and we try and ensure that any answer to a thread is correct.

Hence you are doing two relatively simple tasks, it is possible to run those two sketches "in parallel" on one controller. However, this doesn't work like you might imagine. On a computer, it is pretty simple to run two programs at the same time, because the operating system has a so-called multi-threading. This even works on old single-core machines.

However, a single processor can NEVER do two things at a time. The whole multi-threading is a clever trick. In fact, the processor switches between both programs very fast, so for the user, it looks like both programs are running at the same time.

You need to try a similar approach. At first, you must get rid of the delay() command. This will block the processor for the given amount of time. You have to replace it with hardware or software timers. You can find a lot about hardware timers on the internet. Because I don 't think, that they are really necessary in your scenario, software timers should be much more appropriate, and they are much easier to understand.

The principle is pretty easy: Just store the time, when a part of your code should execute, in a variable. Then, enclose this code in an if statement and check, if the time is correct.

Arduino already brings a pretty simple millis() function, which gives you the milliseconds since the Arduino has been powered on. So if you want to execute a code every second (1000 milliseconds), you have to do the following steps:

  1. Store the current time in the head of your code (above setup and loop): unsigned long timeLastExecuted = millis();
  2. In your loop, check, if at least 1000 ms have passed since the last execution: if(millis() - timeLastExecuted >= 1000)
  3. If the if clause is true, update the timeLastExecuted variable to the current time and execute your code: timeLastExecuted = millis(); doSomeStuff();

Now, the code inside this loop will execute every 1000 ms. You can add many more timers to do more stuff after a given time. Just place all the ifs in your loop().

But you have to keep one thing in mind: With the software solution, there is no guarantee, that the code will execute exactly every 1.000000000s. It might always be off by a few ms – but it will never be shorter than 1000 ms. But if you are just turning a few LEDs on and off, it won't matter.

With this subtractive solution, you might also get a drift over time, because the delays will sum up eventually. There's also an additive solution, where you would store the time, when it should execute next instead. With that, you could just increase the timeNextExecute by 1000 each run. With that, you might still have delays for each execution, but the 1000st loop will still run after roughly 1000 seconds, and not maybe after 1000.01 ms.

However, with that additive approach, you have to keep in mind, that the millis() counter will overflow after roughly 50 days of uninterrupted operation and will reset to 0 then. However, as you are talking about a cosplay, I guess that you will power it off after a few hours.

I hope that this'll help you a little bit.