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