Hey Everyone, I'm currently working on a project where I need to merge two codes and I'm not quite sure how to do it. If anyone can offer guidance or show me how, that would be greatly appreciate so I can get over this hump and continue on with my project.
I just took a couple easy codes off the site to use as an ex:
BLINK CODE:
const int ledPin = 13;
int ledState = LOW;
long previousMillis = 0;
long interval = 1000;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop()
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(ledPin, ledState);
}
}
I would add the following to concept in @BillHo's link
Most short Arduino sketches are configured like this
void setup() {
// all the setup stuff for programA
}
void loop() {
// the meat of the programA
}
If you reorganize that structure like this
void setup() {
// all the setup stuff for programA
}
void loop() {
myFunctionA();
}
void myFuntionA() {
// the meat of the programA
}
Then most of the work of merging two projects can be reduced to
void setup() {
// all the setup stuff for programA
// all the setup stuff for programB
}
void loop() {
myFunctionA();
myFunctionB();
}
void myFuntionA() {
// the meat of the programA
}
void myFuntionB() {
// the meat of the programB
}
I was wondering if you had two or more different sketches if you could combine them or run them independently without interfering with the other and all on the one board? (Arduino Mega 2560)
Two programs cannot run independently on the Arduino as it is not a multi-tasking environment. You can have two things appear to happen at once by making them happen very soon after one another and/or you can do things like monitoring the output of two (or more) sensors sequentially and get the same effect.
What two programs did you have in mind to use at the same time ?
I was wanting to use a sketch to run 2 motors at random speeds about 2 sec delay from each in one direction and at the same time do other things such as turn on and blink a led depending on if a sensor is reading I/O. Thanks for the help so far
You DO understand that the processor can only EXECTUTE ONE INSTRUCTION AT A TIME, right ?. All the previous posts are help to
run different instructions sequentially , without using delays in between but NOTHING HAPPENS EXACTLY AT THE SAME TIME.
Each task is separated from another by at least one processor instruction cycle and usually more than one.
mzise1:
So wait can this do more than 2 functions if you write a program c? :
... snip ...
I wrote the piece of pseudo code that you refer to as an illustration of how to start merging two existing pieces of code. And, of course you could merge as many as you want. It's going to get rather messy however if you have more than two. Perhaps get two working before starting to merge a third.
The example that @BillHo linked to is a better starting point if you want to build your own project. And, in many cases it is easier to start from scratch rather than merge two sketches. Use the existing sketches to learn the technique.
Ideally, when the merging is complete the overall structure will be very like the example.
Also note that some of the library's are incompatible. The newping function isn't fully compatible with the reading of an IR receiver for instance irremote.h). Apparently they both use the same timer. I tried it and it won't compile.
NewPing\NewPing.cpp.o: In function `__vector_13':
C:\Program Files (x86)\Arduino\libraries\NewPing/NewPing.cpp:214: multiple definition of `__vector_13'
IRremote\IRremote.cpp.o:C:\Program Files (x86)\Arduino\libraries\IRremote/IRremote.cpp:311: first defined here
c:/program files (x86)/arduino/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/bin/ld.exe: Disabling relaxation: it will not work with multiple definitions