-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLaserControl.ino
More file actions
54 lines (45 loc) · 1.22 KB
/
LaserControl.ino
File metadata and controls
54 lines (45 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
---
## 📄 Code (`LaserControl.ino`)
```cpp
// LaserControl.ino
// Author: bocaletto-luca
// MIT License — 2025
//
// 12 V Laser PWM controller with toggle switch and potentiometer.
const int laserPin = 9; // PWM output to MOSFET gate
const int potPin = A0; // Power adjust
const int btnPin = 2; // Toggle ON/OFF
bool laserOn = false;
int lastBtnState = HIGH;
unsigned long lastDebounce = 0;
const unsigned long debounceDelay = 50;
void setup() {
pinMode(laserPin, OUTPUT);
pinMode(btnPin, INPUT_PULLUP);
Serial.begin(9600);
Serial.println("🔴 LaserControl ready.");
}
void loop() {
// 1) Debounce and toggle laserOn
int reading = digitalRead(btnPin);
if (reading != lastBtnState) {
lastDebounce = millis();
}
if ((millis() - lastDebounce) > debounceDelay) {
if (reading == LOW && lastBtnState == HIGH) {
laserOn = !laserOn;
Serial.print("Laser ");
Serial.println(laserOn ? "ON" : "OFF");
}
}
lastBtnState = reading;
// 2) Read potentiometer and set PWM if laser is ON
if (laserOn) {
int potVal = analogRead(potPin);
int pwmVal = map(potVal, 0, 1023, 0, 255);
analogWrite(laserPin, pwmVal);
} else {
analogWrite(laserPin, 0);
}
delay(10);
}