const int ledPin = 13; // Pin connected to the LED
const int clockPin = 12;
const int btnPin = 6;
const int laserPin = 7;
void setup() {
pinMode(btnPin, INPUT);
pinMode(laserPin, INPUT);
pinMode(clockPin, OUTPUT); // Set clock pin as an output
pinMode(ledPin, OUTPUT); // Set the LED pin as an output
Serial.begin(9600); // Initialize serial communication
}
void loop() {
bool btn = digitalRead(btnPin); // Read the state of the button pin
bool laser = digitalRead(laserPin); // Read the state of the laser pin
Serial.print("Button: ");
Serial.print(btn);
Serial.print(" Laser: ");
Serial.println(laser);
clk(btn, laser); // Call the clk function with button and laser states
}
void clk(int btn, int laser){
static bool clockRunning = false; // Track the state of the clock
if(btn == 1 && laser == 1){ // Start the clock if both btn and laser are high
digitalWrite(clockPin, HIGH); // Set the clock pin high
digitalWrite(ledPin, HIGH); // Turn on the LED
delay(500); // Wait for 500 milliseconds
digitalWrite(clockPin, LOW); // Set the clock pin low
digitalWrite(ledPin, LOW); // Turn off the LED
delay(500);// Wait for 500 milliseconds
clockRunning = true; // Update the clock state
}
else if(btn == 1 && laser == 0 && clockRunning){ // Stop the clock if btn is high and laser is low
digitalWrite(clockPin, LOW); // Stop the clock
clockRunning = false; // Update the clock state
}
}