Go Down

Topic: Demo of merging the code from two sketches (Read 7448 times) previous topic - next topic

Robin2

Jul 31, 2014, 11:11 am Last Edit: Jul 31, 2014, 11:13 am by Robin2 Reason: 1
There are frequent questions here about how to merge two different sketches and I thought it might be helpful to post a simple worked example.

I have taken as my starting point the sketches Button.ino and Sweep.ino from the examples that come with the Arduino IDE. For convenience I have made a small change to the Button sketch so it doesn't need an external resistor. This is explained in comments within the sketch.

Then I have produced the sketch ButtonSweepA.ino which explains (in the comments) how to include the different parts from the two original sketches with out making any further changes to the code.

Finally the sketch ButtonSweepB.ino shows how to add a few lines of code so that the servo only sweeps when the button is pressed.

There are many other changes that could be considered but I have deliberately stayed as close as possible to the original code from the examples.

This is the original Button.ino code slightly modified

Code: [Select]
// Button.ino
// Robin2, July 2014
// This is the code from the Button example with most of the comments removed to
//  shorten it
//
// There is a small change to use the internal pullup resistor rather than requiring the
//  external 10k pull-down resistor as in the original sketch
//  This means that pressing the button results in a LOW rather than a HIGH reading
//
// The simplified button switch wiring is
//   buttonPin ------ switch ------ Ground



/*
created 2005
by DojoDave <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe
modified July 2014
by Robin2
*/

// constants won't change.
const int buttonPin = 2;
const int ledPin =  13;

// variables will change:
int buttonState = HIGH;

void setup() {
 pinMode(ledPin, OUTPUT);
 pinMode(buttonPin, INPUT_PULLUP); // changed from original example
}

void loop() {
 buttonState = digitalRead(buttonPin);

 if (buttonState == LOW) {    // changed from original example
   digitalWrite(ledPin, HIGH);
 }
 else {
   digitalWrite(ledPin, LOW);
 }
}


And the original Sweep.ino code

Code: [Select]
// Sweep.ino
// Robin2, July 2014
// This is the code from the Sweep example with most of the comments removed to
//  shorten it


/* Sweep
by BARRAGAN <http://barraganstudio.com>
modified 8 Nov 2013
by Scott Fitzgerald
http://arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>

Servo myservo;

int pos = 0;

void setup()
{
 myservo.attach(9);
}

void loop()
{
 for(pos = 0; pos <= 180; pos += 1)
 {
   myservo.write(pos);
   delay(15);
 }
 for(pos = 180; pos>=0; pos-=1)
 {                                
   myservo.write(pos);
   delay(15);
 }
}


This code, ButtonSweepA.ino includes the code from both sketches

Code: [Select]
// ButtonSweepA.ino
// Robin2, July 2014
// this is the first step in merging the Button and Sweep codes
// all of the functionality is in this sketch but the button does not yet
//   control the servo.
//
// You will note that the button press is only detected when the Servo
//   finishes its cycle
//   Changing things so the button is detected at any time is easy but
//     is outside the scope of this demo

//==============================
//       First, include everything before setup() from both sketches
//==============================

//=======From Button.ino
// constants won't change.
const int buttonPin = 2;
const int ledPin =  13;

// variables will change:
int buttonState = 0;

//=======From Sweep.ino
#include <Servo.h>

Servo myservo;

int pos = 0;

//==============================
//      Second, include everything from within setup() in both sketches
//==============================

void setup() {
//=======From Button.ino
 pinMode(ledPin, OUTPUT);
 pinMode(buttonPin, INPUT_PULLUP);
 
 //=======From Sweep.ino
 myservo.attach(9);
}

//==============================
//      Third, create a new loop() function that calls two other functions
//==============================

void loop() {
 buttonLoopCode();
 sweepLoopCode();
}

//==============================
//      Fourth, create two new functions each of which includes the code from within
//        loop() in each of the original sketches
//==============================

void buttonLoopCode() {
 //=======all of the code from within loop() in Button.ino
 buttonState = digitalRead(buttonPin);

 if (buttonState == LOW) {
   digitalWrite(ledPin, HIGH);
 }
 else {
   digitalWrite(ledPin, LOW);
 }
}

//==============

void sweepLoopCode() {
 //=======all of the code from within loop() in Sweep.ino
 for(pos = 0; pos <= 180; pos += 1)
 {
   myservo.write(pos);
   delay(15);
 }
 for(pos = 180; pos>=0; pos-=1)
 {                                
   myservo.write(pos);
   delay(15);
 }
}


And, finally, this code ButtonSweepB.ino links the button to control the servo

Code: [Select]
// ButtonSweepB.ino
// Robin2, July 2014
// this is the second step in merging the Button and Sweep codes
// Now pressing the button causes the servo to sweep
//  This is achieved by adding 3 lines of code at the top of the function sweepLoopCode()

// constants won't change.
const int buttonPin = 2;
const int ledPin =  13;

// variables will change:
int buttonState = 0;

#include <Servo.h>

Servo myservo;

int pos = 0;


void setup() {
//=======From Button.ino
 pinMode(ledPin, OUTPUT);
 pinMode(buttonPin, INPUT_PULLUP);
 
 //=======From Sweep.ino
 myservo.attach(9);
}



void loop() {
 buttonLoopCode();
 sweepLoopCode();
}



void buttonLoopCode() {

 buttonState = digitalRead(buttonPin);

 if (buttonState == LOW) {
   digitalWrite(ledPin, HIGH);
 }
 else {
   digitalWrite(ledPin, LOW);
 }
}

//==============

void sweepLoopCode() {
  // next 3 lines of code are new and ensure the servo only works if the button is pressed
  if (buttonState == HIGH) {
     return;  // do nothing if the button is not pressed
  }

 
 for(pos = 0; pos <= 180; pos += 1)
 {
   myservo.write(pos);
   delay(15);
 }
 for(pos = 180; pos>=0; pos-=1)
 {                                
   myservo.write(pos);
   delay(15);
 }
}



...R
Two or three hours spent thinking and reading documentation solves most programming problems.

JimboZA

Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

JimboZA

I think this should be a sticky at the top of this forum
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

PaulS

Quote
There are many other changes that could be considered but I have deliberately stayed as close as possible to the original code from the examples.

The thing that you did that separates you from those that can't figure out how to merge two sketches was to start with an understanding of:
1) What the original sketches do
2) What the merged sketch should do.

Without those two items, no amount of explanation is useful.

Nice try, though.
The art of getting good answers lies in asking good questions.

azizi

Hi Robin2,
Thanks for the explanations,

I am trying to merge the two examples MD_MAX72xx _Test and
MD_MAX72xx_DaftPunk in one single sketch ( one after the other )

I followed all your advice and I successfully put together the two sketches and NO errors when I compile.

but when I upload my sketch to the matrix ,only one sketch is playing (MD_MAX72xx _Test) !!

Can you please help me on this?

Thank you.

UKHeliBob

Do you think that it might help if you posted your combined program ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up