Looking for general guidelines merging sketches

Hey All,
I've been playing with Arduino for a couple of years now but still have limited skills. I'm working on yet another project that requires I merge 2 sketches. It's always hit or miss if I can successfully make it happen. I often get hung up when dealing with functions. This points to the fact that I need stronger coding skills and understanding.

My question to the forum is are there any general guidelines to consider when merging sketches?

Thanks
Rich

I'm not sure completely what you mean. But I think that you should look into making a library. There's tutorial here: http://arduino.cc/en/Hacking/LibraryTutorial. It makes it alot easier to add to the code.

Maybe you could explain what your two sketches is about?

Grumpy_Mike has a tutorial on this very subject:

Merging Code.

Simple method:

combine the stuff before void setup()

combine the stuff in void setup()

combine the stuff in void loop()

Resolve any ordering issues - all #callouts go at the top of the sketch,
Remove any duplication (like libraries, or things like Serial.begin),
Resolve any duplicates in pins being used twice, variables being used twice.
Check the program flow in void loop() and see if it makes sense.

CrossRoads:
You forgot #0:
Look for any hardware conflicts like chip selects and Timers with unintended consequences.

Yes, you can get really in depth, that takes it a little past general guidelines tho.

Thanks for the responses everyone, things are slightly clearer. So on to my specific question. I have a an Arduino set up to read IR signals to control LEDs, I want to include physical buttons to control the same pins that I can control via IR . Below are the two files I'm trying to use. My goal is to control the variable "statLED6" or pin 11 using IR and button.

Thanks
Rich

int irPin = A5; //Sensor pin 1 wired to Arduino's pin A5

int statLED1 = 3; //Toggle the status LED every time Power is pressed
int statLED2 = 5; //Toggle the status LED every time Power is pressed
int statLED3 = 6; //Toggle the status LED every time Power is pressed
int statLED4 = 9; //Toggle the status LED every time Power is pressed
int statLED5 = 10; //Toggle the status LED every time Power is pressed
int statLED6 = 11; //Toggle the status LED every time Power is pressed

int statLED_state1 = LOW; //mac
int statLED_state2 = LOW; //mac
int statLED_state3 = LOW; //mac
int statLED_state4 = LOW; //mac
int statLED_state5 = LOW; //mac
int statLED_state6 = LOW; //mac

int ledLevel = 0;

int start_bit = 2200; //Start bit threshold (Microseconds)
int bin_1 = 1000; //Binary 1 threshold (Microseconds)
int bin_0 = 400; //Binary 0 threshold (Microseconds)

void setup() {
pinMode(statLED1, OUTPUT);
pinMode(statLED2, OUTPUT);
pinMode(statLED3, OUTPUT);
pinMode(statLED4, OUTPUT);
pinMode(statLED5, OUTPUT);
pinMode(statLED6, OUTPUT);

pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);

digitalWrite(statLED1, statLED_state1); //mac
digitalWrite(statLED2, statLED_state2); //mac
digitalWrite(statLED3, statLED_state3); //mac
digitalWrite(statLED4, statLED_state4); //mac
digitalWrite(statLED5, statLED_state5); //mac
digitalWrite(statLED6, statLED_state6); //macc

pinMode(irPin, INPUT);

Serial.begin(9600);
Serial.println("Waiting: ");
}

void loop() {
int key = getIRKey(); //Fetch the key

if(key != 0) //Ignore keys that are zero
{
Serial.print("Key Recieved: ");
Serial.print(key);
switch(key)
{

case 1302:
Serial.print("Circuit 1");
if(statLED_state1 != LOW) //This toggles the statLED every time power button is hit
statLED_state1 = LOW;
else
statLED_state1 = HIGH;
digitalWrite(statLED1, statLED_state1); //mac
delay(250);
break;

case 1281:
Serial.print("Circuit 2");
if(statLED_state2 != LOW) //This toggles the statLED every time power button is hit
statLED_state2 = LOW;
else
statLED_state2 = HIGH;
digitalWrite(statLED2, statLED_state2); //mac
delay(250);
break;

case 1282:
Serial.print("Circuit 3");
if(statLED_state3 != LOW) //This toggles the statLED every time power button is hit
statLED_state3 = LOW;
else
statLED_state3 = HIGH;
digitalWrite(statLED3, statLED_state3); //mac
delay(250);
break;

case 1283:
Serial.print("Circuit 4");
if(statLED_state4 != LOW) //This toggles the statLED every time power button is hit
statLED_state4 = LOW;
else
statLED_state4 = HIGH;
digitalWrite(statLED4, statLED_state4); //mac
delay(250);
break;

case 1284:
Serial.print("Circuit 5");
if(statLED_state5 != LOW) //This toggles the statLED every time power button is hit
statLED_state5 = LOW;
else
statLED_state5 = HIGH;
digitalWrite(statLED5, statLED_state5); //mac
delay(250);
break;

case 1285:
Serial.print("Up Circuit 6");
ledLevel = ledLevel + 3;
if (ledLevel > 0) ledLevel = 0;
analogWrite(statLED6, ledLevel);
break;

case 1288: // button 9 on the remote. This does PWM
Serial.print("Circuit 9");
if(statLED_state6 != LOW) //This toggles the statLED every time power button is hit
statLED_state6 = LOW;
else
statLED_state6 = HIGH;
digitalWrite(statLED6, statLED_state6); //mac
delay(250);
break;

case 1286: // button 8 on the remote. This does PWM
Serial.print("Down Circuit 1");
ledLevel = ledLevel - 3;
if (ledLevel > 0) ledLevel = 0;
analogWrite(statLED1, ledLevel);
break;

case 1289: // button 0 on the remote.
Serial.print("Down Circuit 2");
ledLevel = ledLevel - 3;
if (ledLevel > 0) ledLevel = 0;
analogWrite(statLED2, ledLevel);
break;

case 1287:
Serial.print("Down Circuit 2");
ledLevel = ledLevel + 3;
if (ledLevel > 0) ledLevel = 0;
analogWrite(statLED2, ledLevel);
break;

case 1330: // button 9 on the remote. This does PWM
Serial.print("Up Circuit 2");
ledLevel = ledLevel + 3;
if (ledLevel > 0) ledLevel = 0;
analogWrite(statLED4, ledLevel);
break;

case 1337:
Serial.print("Down Circuit 2");
ledLevel = ledLevel - 3;
if (ledLevel > 0) ledLevel = 0;
analogWrite(statLED4, ledLevel);
break;

case 1379: // button 9 on the remote. This does PWM
Serial.print("Up Circuit 3");
ledLevel = ledLevel + 3;
if (ledLevel > 0) ledLevel = 0;
analogWrite(statLED3, ledLevel);
break;

case 1380:
Serial.print("Down Circuit 3");
ledLevel = ledLevel - 3;
if (ledLevel > 0) ledLevel = 0;
analogWrite(statLED3, ledLevel);
break;

Serial.print(key);
}
Serial.println();
}
}

int getIRKey() {
int data[12];
int i;

while(pulseIn(irPin, LOW) < start_bit); //Wait for a start bit

for(i = 0 ; i < 11 ; i++)
data = pulseIn(irPin, LOW); //Start measuring bits, I only want low pulses

  • for(i = 0 ; i < 11 ; i++) //Parse them*
  • {*
    if(data > bin_1) //is it a 1?
    _ data = 1;
    else if(data > bin_0) //is it a 0?
    data = 0;
    * else*
    * return -1; //Flag the data as invalid; I don't know what it is! Return -1 on invalid data*
    * }
    int result = 0;
    for(i = 0 ; i < 11 ; i++) //Convert data bits to integer*

    if(data == 1) result |= (1<<i);
    * return result; //Return key number*
    }
    [/quote]
    Here is my code for the button
    > int switchPin = 8;
    > int ledPin = 13;
    > boolean lastButton = LOW;
    > boolean currentButton = LOW;
    > boolean ledOn = false;
    >
    > void setup()
    > {
    > pinMode(switchPin, INPUT);
    > pinMode(ledPin, OUTPUT);
    > }
    >
    > boolean debounce(boolean last)
    > {
    > boolean current = digitalRead(switchPin);
    > if (last != current)
    > {
    > delay(5);
    > current = digitalRead(switchPin);
    > }
    > return current;
    > }
    >
    > void loop()
    > {
    > currentButton = debounce(lastButton);
    > if (lastButton == LOW && currentButton == HIGH)
    > {
    > ledOn = !ledOn;
    > }
    > lastButton = currentButton;
    >
    > digitalWrite(ledPin, ledOn);

    >
    > }_

richiep:

int irPin = A5; //Sensor pin 1 wired to Arduino's pin A5

int statLED1 = 3; //Toggle the status LED every time Power is pressed
int statLED2 = 5; //Toggle the status LED every time Power is pressed
int statLED3 = 6; //Toggle the status LED every time Power is pressed
int statLED4 = 9; //Toggle the status LED every time Power is pressed
int statLED5 = 10; //Toggle the status LED every time Power is pressed
int statLED6 = 11; //Toggle the status LED every time Power is pressed

int statLED_state1 = LOW;  //mac
int statLED_state2 = LOW;  //mac
int statLED_state3 = LOW;  //mac
int statLED_state4 = LOW;  //mac
int statLED_state5 = LOW;  //mac
int statLED_state6 = LOW;  //mac

int ledLevel = 0;

int start_bit = 2200; //Start bit threshold (Microseconds)
int bin_1 = 1000; //Binary 1 threshold (Microseconds)
int bin_0 = 400; //Binary 0 threshold (Microseconds)

void setup() {
  pinMode(statLED1, OUTPUT); // statLED1 = 3 from above
  pinMode(statLED2, OUTPUT);  //statLED2 = 5 from above
  pinMode(statLED3, OUTPUT);  //statLED3 = 6 from above
  pinMode(statLED4, OUTPUT);  //statLED4 = 9 from above
  pinMode(statLED5, OUTPUT); // statLED5 = 10 from above
  pinMode(statLED6, OUTPUT); // statLED6 = 11 from above

pinMode(3, OUTPUT);  //so why are you setting it as OUTPUT twice?
  pinMode(5, OUTPUT); //so why are you setting it as OUTPUT twice?
  pinMode(6, OUTPUT); //so why are you setting it as OUTPUT twice?
  pinMode(9, OUTPUT); //so why are you setting it as OUTPUT twice?
  pinMode(10, OUTPUT); //so why are you setting it as OUTPUT twice?
  pinMode(11, OUTPUT); //so why are you setting it as OUTPUT twice?

richiep:
Thanks for the responses everyone, things are slightly clearer.

Code tags please.

Read this before posting a programming question

richiep:
My question to the forum is are there any general guidelines to consider when merging sketches?

It's not a particularly easy question to answer. The simple answer is "understand how both sketches work and then it should be obvious".

Let's say you know how to cook a roast pork, and you also know how to bake a loaf of bread. Now the trick is, to do both at once.

Now clearly some "initialization" (such as turning on the oven) only needs to be done once. You may have problems if shared resources are both needed at the same time (eg. the oven).
Some situations will work out nicely, some will just be impossible. It's hard to give hard and fast rules here.

Glancing over your code, yours may be fairly simple, if both sketches use different resources (pins) and the timing is not too critical.

I cleaned up my code as Henry suggested and read all the stuff you guys posted but I still have two un-merged sketches. If anyone feels like taking a stab at the code above I'd appreciate it.

Thanks
Rich

richiep:
Here is my code for the button

int switchPin = 8;
int ledPin = 13;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean ledOn = false;  //All of the above goes before setup()

void setup()
{
  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT); //All this goes into setup()
}

boolean debounce(boolean last)  //copy this function into the other sketch, as it is
{
  boolean current = digitalRead(switchPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}

void loop()
{
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)
  {
    ledOn = !ledOn;
  }
  lastButton = currentButton;
  digitalWrite(ledPin, ledOn); 
//copy all between the first and last curlys here and paste 
//into loop(), at the top, on your other sketch. 
}

The only problem I can forsee is that when your program is doing the while statement, it won't be able to read the switches.

Thanks Henry,
I've tried what you suggested and it compiles but when I upload it and try the button it doesn't respond. Meanwhile the IR stuff functions as intended.

I've verified the circuit is wired correctly because I've uploaded the button sketch by itself and it works.

Rich

Hey All,

Please could anyone help. i write vb code and applications but the language that the sketch is written in is strange to me.

If anyone can help : i need the stranded receive sms sketch combined with a section for sending AT commands to the gsm shield.

when i load the receive sms sketch i can receive perfectly, but no sending.

when i load the send sms sketch i can send sms's, but i can not receive and send.

if possible i would like to receive sms's the way it does with the receive sms sketch, but then be able to send sms's with AT commands from my vb.net application.

it seems so easy but yet out of my reach.

pretty please, anyone

richiep:
Thanks Henry,
I've tried what you suggested and it compiles but when I upload it and try the button it doesn't respond.

Did you see what I wrote above about the buttons not being read during the time that the program is in the while loop (//Wait for a start bit)? Change the while statement to an if statement (//If start bit received).
Also, see the 'blink without delay' example to get rid of the delays. No buttons can be read when the program is in a delay.