Uppmärksamhetsrobot
const loop = () => { run(); setTimeout(loop, 70); };let angle = 0; let r; let sine; let amplitude = 0.3; let ramp = false; let threshold = 0.7; let timeStart = Date.now(); let elapsed = 0;function run() { let buttonCount = 0; let buttonState = digitalRead(BTN); const checkElapsed = () => { let now = Date.now(); elapsed = (now - timeStart) / 2000; return elapsed; }; if (buttonState == 1) { checkElapsed(); buttonCount++; } else { timeStart = Date.now(); } ramp = false; angle += 90; r = (Math.PI / 180) * angle; // sine waves if (sine > threshold && ramp) { sine = 0; analogWrite(LED1, sine); r = 0; angle = 0; } else { if (buttonState == 1 && elapsed > 0) { if (elapsed > 15) { sine = Math.sin((r + 1.5 * Math.PI) * elapsed / 2) * amplitude + 0.7; console.log("value of sine (r) based on scaled to elapsed: " + sine); } else { sine = Math.sin((r + 1.5 * Math.PI) / (elapsed * 3)) * amplitude + 0.7; console.log("value of sine (r) based on scaled to elapsed: " + sine); } } else if (elapsed > 10 && buttonState == 0) { sine = Math.sin((((Math.PI / 180) * 90) + 1.5 * Math.PI) / (45)) * amplitude + 0.7; } else { sine = Math.floor(Math.random() * ((Math.floor(2) - Math.ceil(0)) + Math.ceil(0))) * Math.floor(Math.random() * ((Math.floor(2) - Math.ceil(0)) + Math.ceil(0))); console.log("not pressed sine: " + sine); } analogWrite(LED2, sine); } }let buttonPressedAt = 0; function onButtonPress() { const elapsed = Date.now - buttonPressedAt; buttonPressedAt = Date.now(); } let buttonPressed = Date.now(); let time = Date.now() - buttonPressed;loop();