//----------Variables For TRANSMITTER Toggle Stage---------------//
#define OUTPUT_PIN A3
#define INPUT_PIN 6
byte lastButtonState = HIGH;
byte outputState = LOW;
unsigned long debounceDuration = 50; // millis
unsigned long lastTimeButtonStateChanged = 0;
//----------Variables For TRANSMITTER Blink Stage---------------//
const int ledPin = A2; // Define the pin where the Blink LED is connected.
int ledState = LOW;
unsigned long previousMillis = 0; // Variables to keep track of time for interval timing.
const long interval = 200; // ""
void toggleBlink() {
if (millis() - lastTimeButtonStateChanged > debounceDuration) {
byte buttonState = digitalRead(INPUT_PIN);
if (buttonState != lastButtonState) {
lastTimeButtonStateChanged = millis();
lastButtonState = buttonState;
if (buttonState == LOW) {
outputState = (outputState == HIGH) ? LOW : HIGH;
digitalWrite(OUTPUT_PIN, outputState);
}
}
}
outputState = digitalRead(OUTPUT_PIN); // Read the state of the output.
if (outputState == HIGH) { // Check if the output output is high.
// Get the current time.
unsigned long currentMillis = millis();
// Check if it's time to toggle the ledState based on the interval.
if (currentMillis - previousMillis >= interval) {
// Save the last time the LED was toggled.
previousMillis = currentMillis;
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
digitalWrite(ledPin, ledState); // Apply the new LED state.
}
} else {
digitalWrite(ledPin, LOW); // If the output is low, turn off the LED.
}
}
void setup() {
pinMode(OUTPUT_PIN, OUTPUT);
pinMode(INPUT_PIN, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop() {
toggleBlink();
}