And first of all sorry for my bad English.
I am trying to write code that allows the user to change between continuous or manual mode, but I am having some issues getting it to work. The goal is to be able to switch from manual SOS (user pressing the button to SOS) and automatic SOS (the led automatically blinking SOS morse).
Currently, when I press down the button it loops once the automatic SOS and returns back to its normal state with LED on.
How would I make it, for an example switch the mode to manual when holding down button and giving user some sort of indication when changing the mode (etc blinking the led for X amount of time?)
What I am looking to achieve is:
- Turn LEDS on when pressing the button.
- Start looping automatic SOS signal
- Press or hold down the button to change mode.
- Blink LEDS to indicate the mode change.
- Switch to manual SOS signal (user pressing the button to signal)
- Press or hold down the button to change mode.
- Blink LEDS to indicate the mode change.
- Switch back to an automatic loop.
The code so far:
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int mode;
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
mode = digitalRead(buttonPin);
// Manual SOS mode.
buttonState = mode;
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == LOW) {
digitalWrite(ledPin, HIGH); // turn LED on:
} else {
digitalWrite(ledPin, LOW); // turn LED off:
}
// Automatic SOS mode.
if (mode == HIGH) {
// check if the pushbutton is pressed.
// if it is, starts looping automatic SOS.
int i = 0;
for (i=0; i<3; i++) {
digitalWrite(ledPin, HIGH); // turn the LED on
delay(300); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED
delay(300); // wait for a second
}
for (i=0; i<3; i++){
digitalWrite(ledPin, HIGH); // turn the LED on
delay(1200); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED
delay(1200); // wait for a second
}
for (i=0; i<3; i++){
digitalWrite(ledPin, HIGH); // turn the LED on
delay(300); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED
delay(300); // wait for a second
}
digitalWrite(ledPin, LOW);
// waits 3 seconds before starting the loop again
delay(3000);
}
}