#Is there any way I can make this better/compact? (C++)
14 messages · Page 1 of 1 (latest)
Thanks for reaching out! To get helpful feedback on your C++ code, please share:
- Your actual code - formatted using markdown code blocks (
cpp ...) - What you're trying to accomplish - describe the goal or problem
- Specific concerns - what aspects are you looking to improve? (performance, readability, functionality, etc.)
- Context - any relevant details about your environment or constraints
Without seeing your code and understanding your goals, it's hard to provide meaningful suggestions. Once you add these details, you'll likely get great feedback right away!
damn
-# C++ @lucid gull @shy bloom @sinful anchor
// ===== PIN DEFINITIONS =====
const int zipperPin = 2; // Slide switch / zipper sensor
const int buzzerPin = 9; // Passive buzzer
// ===== TIMING =====
const unsigned long ALARM_DELAY = 3000; // Delay (Milisecods/ms)
// ===== BUZZER PATTERN =====
// Pattern: OFF, ON, OFF, ON, OFF, ON (ms)
const unsigned int beepPattern[] = {
150, 150,
150, 150,
150, 150,
150, 150,
600, 0
};
const int patternLength = sizeof(beepPattern) / sizeof(beepPattern[0]);
// ===== STATE =====
unsigned long zipperOpenedTime = 0;
bool alarmActive = false;
// ===== SETUP =====
void setup() {
pinMode(zipperPin, INPUT_PULLUP); // HIGH = open, LOW = closed
pinMode(buzzerPin, OUTPUT);
noTone(buzzerPin);
}
// ===== PATTERNED BUZZER (DROP-IN) =====
void pulseBuzzerPattern() {
static unsigned long timer = 0;
static int step = 0;
static bool on = false;
unsigned long now = millis();
if (now - timer >= beepPattern[step]) {
timer = now;
on = !on;
if (on) {
tone(buzzerPin, 2000); // Buzzer Frequency
} else {
noTone(buzzerPin);
}
step = (step + 1) % patternLength;
}
}
// ===== MAIN LOOP =====
void loop() {
bool zipperOpen = digitalRead(zipperPin); // HIGH = open
if (zipperOpen) {
// Zipper just opened
if (zipperOpenedTime == 0) {
zipperOpenedTime = millis();
}
// Alarm delay passed?
if (millis() - zipperOpenedTime >= ALARM_DELAY) {
alarmActive = true;
}
} else {
// Zipper closed → reset everything
zipperOpenedTime = 0;
alarmActive = false;
noTone(buzzerPin);
}
// Run buzzer pattern if alarm is active
if (alarmActive) {
pulseBuzzerPattern();
}
}
interesting that it calls arduino dangerous
i would recommend bool zipperOpen = (digitalRead(zipperPin) == HIGH); // HIGH = open
that will make sure it's still valid if HIGH ever gets changed from true, some boards may do that already though idk for sure
other than that seems probably fine to me
aight thanks
it's just a hardcoded list of file extensions lol
.cpp is fine, .ino isn't
yeah but why isn't ino safe, that's only used on arduino really
It's a whitelist and we've never really had anyone ask Arduino questions before lol