/* 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++; } }