I am trying to make a Christmas light display controller from a Arduino mega. I want the board to turn the relays on and off ( which I have a code for that works alone ) and also the adafruit fm transmitter ( which I have a code for that works alone ) ...My problem is when I load one code into the Arduino it seems to over write the other and I have a very hard time to get them to work at the same time ....Also why wont the code stay when the Arduino is unplugged, if its unplugged I have to re upload the code to get it to work again is this normal?
Arduinos only contain and run a single program. If you want a program to do several things you need to combine the separate programs into one bigger one. See Merging Code for how to do this. Give it a try and if you have problems post it here and we can help sorting it out.
However when a program is loaded it will normally stay there, powered or not, until overwritten so I'm not sure what's going on there. It may be that your program is in and running but stuck somewhere not doing anything visible.
Steve
You can, in effect, run multiple programs on an Arduino by including code for them all, and using program logic or function pointers to select among them at run time. I have done it.
welcome to the Forums !
please read how to use this forum, it can be found at the top of every forum and please read and understand #7 about posting code using code tags.
and post your code.
if it works when plugged in, but not when disconnected, is there some connection that is required between your computer and your micro ?
power is good ?
when you combine two sketches, I do it in 3 basic steps.
#1) merge the declaration parts of the two sketchs, adding libraries first, and then globals, with comments on each part.
check to make sure it compiles without errors.
upload and make sure it runs without errors.
#2) merge the setup parts,
if you have any duplicate names, you need to change them in your original program so that in the end, your single program and your merged will have identical bits.
check to make sure it compiles without errors.
upload and make sure it runs without errors.
#3) re-name your void loop to void lights()
create a new void called void FMradio()
and create a new void that is the actual new void loop()
put all your radio bits into the radio void
put all your light bits into the lights void
and in void loop() call each
void loop() {
FMradio();
lights();
}
now, the program will go to each, run that once thru, come back, then run the next one, once through, repeat
This is where your knowledge of global vs local variable comes into play.
to pass a value from one to the next, you have to use globals.
You can comment out each one and just run the other to test them working individually or together
once you get the sketch to work, you can decide if you want to actually merge them into one loop() that runs both bits at the same time.
but, at this point you know
#1) your declarations are working together
#2) your setup is working together
#3) your sketch(s) can work, emphasis on CAN
now, when (if) you want to merge, you can start that process.
for that bit, I use separators
// ++++++++++++++++ READ PINS +++++++++
// +++++++ math functions ++++++++++++++++++
//+++++++++++++++ Out Puts +++++++++++++
and obviously, you can have lots more with more descriptive names.
I find that by keeping the bits sorted, I do not wind up with a huge mess that is hard to troubleshoot.
BTW, put these new voids above loop()
I assume you want some kind of "combined" program, in which BOTH the lights are doing their thing, AND, at the same time (at least as far as a human can notice, "the adafruit fm transmitter"...
You need at least to tell us what you meant by that latter bit. I presume you want to connect one of them, and have it do... something? What???
If you DON'T need the COMBINED program, only want a Mega that can, at any one time... for minutes on end... do one of the programs, or the other.... that's quite do-able. But you have to write ONE program, with provision for each, and dedicate one pin as an input. You make that high or low, and write the program so that when the input is high, the LED SUB-program, say, runs, and when the input is low, the other sub-program runs.
But the needs of the two programs mustn't overlap. Let's say the LED program uses D2, D3, and D4... and the fm program uses D5, D6, and D7... fine! Or, if each needs, say, an LED driven by an output, they can share one LED on one pin.