Last Updated:22/10/2021
How To 3 - Joining Sketches Together
Lessons >> How To >> How To 3 - Joining Sketches Together
Overview
One of the issues that comes up constantly on forums and facebook groups is problems arising from trying to join a couple of sketches together.
In this tutorial we will look at some simple steps to make the process easier.
Circuit
Push buttons
10k Ohm resistors
Digital pin and resistor to Gnd are on one side of the button
5v connection to opposite side of the button
Example 1: joinSketchv1_blink.ino
Simple skecth that produces an SOS sequence of LED flashes using the built in LED based off the basic Blink sketch.
Click to Download code:joinSketchv1_blink.ino
/* 21/10/2021
* joinSketchv1_blink
*
* This is the basic blink example
* All I have added is the serial output
*
* As usual I list any pins used in the sketch, this prevents duplicates
* pins used:
*
* 13 built in LED
*/
//LED_BUILTIN not used because it does not work on some boards such as the ESP32
//So stops bad habits developing
const int ledPin = 13; //set the LED pin
void setup() {
//Serial started for debugging
Serial.begin(9600);
//print skecth name so we know the sketch has uploaded.
Serial.print("joinSketchv1_blink");
//set the pin mode for the LED pin
pinMode(ledPin,OUTPUT);
}
void loop() {
/*
//The standard blink sketch switches the led on and off after a 1 second delay
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(1000);
*/
//to make out code a bit more interesting the sequence has been lengthened to create an SOS
//with all the delays this lengthens the sequence to about 7+ seconds
//This will help us see the issue when joining sketches together
//three dots..quick pulses
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(600);
//Three dashes
digitalWrite(ledPin, HIGH);
delay(600);
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPin, HIGH);
delay(600);
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPin, HIGH);
delay(600);
digitalWrite(ledPin, LOW);
delay(600);
//three dots..quick pulses
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(2000); //wait 2 seconds before doing it all again
}
Example 2: joinSketchv2_sosbuttons.ino
Now we add some buttons from the "button" example and start to see the issues when joining the two sketches.
This sketch does not work and is used as an example of why sketches do not join ttogether well.
Click to Download code:joinSketchv2_sosbuttons.ino
/* 21/10/2021
joinSketchv2_sosbuttons
This sketch does not work properly...this is delivberate to highlight the issue
when joining sketches
In this example we add a single button that we want to use to start the sequence or end the sequence
We will start to see the issues when the two sketches are joined
Based on the Examples>Digital>button
As usual I list any pins used in the sketch, this prevents duplicates
pins used:
pin 13 built in LED
pin 8 button
*/
//LED_BUILTIN not used because it does not work on some boards sich as the ESP32
//So stops bad habits developing
const int ledPin = 13; //set the LED pin
const int buttonPin = 8;//push button pin
void setup() {
Serial.begin(9600);
Serial.println("joinSketchv2_sosbuttons");
//set the pin mode for the LED pin
pinMode(ledPin, OUTPUT);
//set the pin mode for the button pin
pinMode(buttonPin, INPUT);
}
void loop() {
int buttonState;
//in the button example pressing the button turns the LED on, releasing turns LED off
/* The problem in this sketch is because of the delay() during the sequence
* Once the sequence has started there is no way for the system to know that the
* button is no longer being pressed until the SOS sequence has finished.
* Pressing the button and releasing it during the sequence is not picked up
* This is a regular cause of conflict when joining sketches together.
* The other issue with this sketch is that the button relased is printed endlessly to the Serial monitor
*/
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {//button pressed start the led sos sequence
Serial.print("button pressed");
//three dots..quick pulses
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(600);
//Three dashes
digitalWrite(ledPin, HIGH);
delay(600);
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPin, HIGH);
delay(600);
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPin, HIGH);
delay(600);
digitalWrite(ledPin, LOW);
delay(600);
//three dots..quick pulses
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(2000);//wait 2 seconds before doing it all again
} else {//button released, we want the sequence to stop
Serial.println("button released");
digitalWrite(ledPin, LOW);
}
}
Example 3: joinSketchv3_sosbuttons.ino
This code has been corrected so that the seqience will stop whenever the button is released.
Although the sketch works well, it could be improved by debouncing the buttons. If you take a look at the example
How To 1 : Debouncing buttons it could be adapted and added into the sketch to make it even better.
Click to Download code:joinSketchv3_sosbuttons.ino
/* 21/10/2021
joinSketchv3_sosbuttons
This version fixes the issues in joinSketchv2_sosbuttons
as an example of what to look for when joining sketches.
Although it works it is not perfect, if you put "show timestamp"
in serial monitor you will see that when pressing and especially releasing the button
You can get multiple press releases...the button is bouncing as the contact is made/broken
Goals of the sketch
Pressing the button and holding it down starts the sequence
Releasing the button stops the sequence at any point
seril print button state one...pressed or released.
As usual I list any pins used in the sketch, this prevents duplicates
pins used:
pin 13 built in LED
pin 8 button
*/
//LED_BUILTIN not used because it does not work on some boards sich as the ESP32
//So stops bad habits developing
const int ledPin = 13; //set the LED pin
const int buttonPin = 8;//push button pin
//global variables used to control the system
int lastbuttonState;
int ledSequencePosition = 100;//keeps track of our position in the led sequence
unsigned long ledTimer;//the timer to alter the state of the led
//array to keep the sequence data first part will store the time delay, 2nd the led state
int ledSequenceArray[18][2] = {
//dots
{200, 1}, //HIGH for 0.2 seconds
{200, 0}, //LOW for 0.2 seconds
{200, 1},
{200, 0},
{200, 1},
{600, 0}, //LOW for 0.6 seconds
//dashes
{600, 1}, //HIGH for 0.6 seconds
{200, 0}, //LOW for 0.2 seconds
{600, 1},
{200, 0},
{600, 1},
{600, 0}, //LOW for 0.6 seonds
//second set of dots
{200, 1}, //HIGH for 0.2 seconds
{200, 0}, //LOW for 0.2 seconds
{200, 1},
{200, 0},
{200, 1},
{2000, 0}, //LOW for 2 seconds, end of sequence
};
void setup() {
Serial.begin(9600);
Serial.println("joinSketchv3_sosbuttons");
//set the pin mode for the LED pin
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
//sketch is broken into 2 sections,
//the first constantly checks the state of the buttons
int buttonState;
buttonState = digitalRead(buttonPin);
//has the button been pressed or stopped being pressed since we last checked
if (buttonState != lastbuttonState) {
lastbuttonState = buttonState;//update lastbuttonState
//As the state has changed let's respond to the change
if (buttonState > 0) { //button is HIGH...pressed so start sequence
//Serial.println() the state
Serial.println("button pressed");
ledSequencePosition = 0;//starts the first part of the sequence
ledTimer = 0;//start the timer now
} else { //button LOW released so stop sequence
//Serial.println() the state
Serial.println("button released");
digitalWrite(ledPin, LOW);//turn LED off
ledSequencePosition = 100;//set value outside the routine so nothing gets processed
}
}
if(millis() > ledTimer && ledSequencePosition < 18){
//set the led to the next state
digitalWrite(ledPin, ledSequenceArray[ledSequencePosition][1]);
//set the millis() timer for the next change
ledTimer = millis() + ledSequenceArray[ledSequencePosition][0];
//incremement the ledSequencePosition for the next event
ledSequencePosition++;
}
}
Additional Resource Links
Lesson 3: Using "if else" to control code 19/07/2021
Lesson 7: delay() v's millis(), controlling timing of programs 23/07/2021
Lesson 10: Using Global and Local variables 11/08/2021
Lesson 11: Arrays 28/08/2021
Functions 1 : digitalWrite() Using digital pins to turn things on and off 05/08/2021
Functions 2 : digitalRead() Reading a digital pin and pullup resistors 11/08/2021
Comments
This site has been designed to be child friendly, this means that comments cannot be added to videos or directly to the site. To add a comment or ask a question please email the address in this image: and use How To 3 - Joining Sketches Together as a reference.