Last active 1745376926

blink.html Raw
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LED Blinking Duration Demo</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #f5f5f5;
}
.container {
display: flex;
justify-content: space-around;
flex-wrap: wrap;
gap: 20px;
}
.action-container {
background-color: white;
border-radius: 8px;
padding: 20px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
width: 300px;
}
h1 {
text-align: center;
color: #333;
}
h2 {
color: #444;
margin-top: 0;
}
.led {
width: 80px;
height: 80px;
border-radius: 50%;
background-color: #ccffcc;
margin: 20px auto;
box-shadow: 0 0 10px rgba(0,255,0,0.2);
transition: all 0.1s ease;
}
.led.on {
background-color: #00ff00;
box-shadow:
0 0 10px rgba(0,255,0,0.8),
0 0 20px rgba(0,255,0,0.6),
0 0 30px rgba(0,255,0,0.4);
}
label {
display: block;
margin: 10px 0 5px;
font-weight: bold;
}
input[type="range"] {
width: 100%;
}
.value-display {
text-align: right;
font-size: 0.9em;
color: #666;
}
button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 15px;
border-radius: 4px;
cursor: pointer;
margin-top: 15px;
width: 100%;
font-size: 16px;
}
button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<h1>LED Blinking Duration Demo</h1>
<div class="container">
<div class="action-container">
<h2>Action 1: Quick Blinks</h2>
<div id="led1" class="led"></div>
<div class="controls">
<label for="onTime1">ON Duration (ms): <span id="onTimeValue1" class="value-display">200</span></label>
<input type="range" id="onTime1" min="50" max="1000" value="200" step="50">
<label for="offTime1">OFF Duration (ms): <span id="offTimeValue1" class="value-display">300</span></label>
<input type="range" id="offTime1" min="50" max="1000" value="300" step="50">
<button id="toggleButton1">Start Blinking</button>
</div>
</div>
<div class="action-container">
<h2>Action 2: Slow Blinks</h2>
<div id="led2" class="led"></div>
<div class="controls">
<label for="onTime2">ON Duration (ms): <span id="onTimeValue2" class="value-display">800</span></label>
<input type="range" id="onTime2" min="50" max="2000" value="800" step="50">
<label for="offTime2">OFF Duration (ms): <span id="offTimeValue2" class="value-display">800</span></label>
<input type="range" id="offTime2" min="50" max="2000" value="800" step="50">
<button id="toggleButton2">Start Blinking</button>
</div>
</div>
</div>
<script>
// LED 1 elements
const led1 = document.getElementById('led1');
const onTime1 = document.getElementById('onTime1');
const offTime1 = document.getElementById('offTime1');
const onTimeValue1 = document.getElementById('onTimeValue1');
const offTimeValue1 = document.getElementById('offTimeValue1');
const toggleButton1 = document.getElementById('toggleButton1');
// LED 2 elements
const led2 = document.getElementById('led2');
const onTime2 = document.getElementById('onTime2');
const offTime2 = document.getElementById('offTime2');
const onTimeValue2 = document.getElementById('onTimeValue2');
const offTimeValue2 = document.getElementById('offTimeValue2');
const toggleButton2 = document.getElementById('toggleButton2');
// Variables to track intervals
let interval1 = null;
let interval2 = null;
let led1State = false;
let led2State = false;
// Update display values for sliders
onTime1.addEventListener('input', () => {
onTimeValue1.textContent = onTime1.value;
if (interval1) {
stopBlink(1);
startBlink(1);
}
});
offTime1.addEventListener('input', () => {
offTimeValue1.textContent = offTime1.value;
if (interval1) {
stopBlink(1);
startBlink(1);
}
});
onTime2.addEventListener('input', () => {
onTimeValue2.textContent = onTime2.value;
if (interval2) {
stopBlink(2);
startBlink(2);
}
});
offTime2.addEventListener('input', () => {
offTimeValue2.textContent = offTime2.value;
if (interval2) {
stopBlink(2);
startBlink(2);
}
});
// Toggle button handlers
toggleButton1.addEventListener('click', () => {
if (interval1) {
stopBlink(1);
toggleButton1.textContent = 'Start Blinking';
} else {
startBlink(1);
toggleButton1.textContent = 'Stop Blinking';
}
});
toggleButton2.addEventListener('click', () => {
if (interval2) {
stopBlink(2);
toggleButton2.textContent = 'Start Blinking';
} else {
startBlink(2);
toggleButton2.textContent = 'Stop Blinking';
}
});
// Functions to start and stop blinking
function startBlink(ledNum) {
if (ledNum === 1) {
led1State = true;
led1.classList.add('on');
interval1 = setInterval(() => {
if (led1State) {
// LED is on, turn it off
led1.classList.remove('on');
led1State = false;
setTimeout(() => {
if (interval1) { // Check if still blinking
led1.classList.add('on');
led1State = true;
}
}, parseInt(offTime1.value));
}
}, parseInt(onTime1.value) + parseInt(offTime1.value));
} else {
led2State = true;
led2.classList.add('on');
interval2 = setInterval(() => {
if (led2State) {
// LED is on, turn it off
led2.classList.remove('on');
led2State = false;
setTimeout(() => {
if (interval2) { // Check if still blinking
led2.classList.add('on');
led2State = true;
}
}, parseInt(offTime2.value));
}
}, parseInt(onTime2.value) + parseInt(offTime2.value));
}
}
function stopBlink(ledNum) {
if (ledNum === 1) {
clearInterval(interval1);
interval1 = null;
led1.classList.remove('on');
led1State = false;
} else {
clearInterval(interval2);
interval2 = null;
led2.classList.remove('on');
led2State = false;
}
}
</script>
</body>
</html>