#Is there any way I can make this better/compact? (C++)

14 messages · Page 1 of 1 (latest)

golden birchBOT
#

-# 🛑 @runic zinc, .ino files are potentially dangerous. Your message has been deleted.

#

Thanks for reaching out! To get helpful feedback on your C++ code, please share:

  1. Your actual code - formatted using markdown code blocks (cpp ... )
  2. What you're trying to accomplish - describe the goal or problem
  3. Specific concerns - what aspects are you looking to improve? (performance, readability, functionality, etc.)
  4. 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!

runic zinc
#

damn

golden birchBOT
#

-# C++ @lucid gull @shy bloom @sinful anchor

runic zinc
#
// ===== 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();
  }
}
shy bloom
#

interesting that it calls arduino dangerous

runic zinc
#

lol

#

Is there any way I can make this better/compact? (C++)

shy bloom
#

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

runic zinc
#

aight thanks

sinful anchor
#

.cpp is fine, .ino isn't

shy bloom
#

yeah but why isn't ino safe, that's only used on arduino really

minor swift
#

It's a whitelist and we've never really had anyone ask Arduino questions before lol