Switch v2.0

Greetings.

So during last class we have created a switch (an actual switch, not the button) . I have used this wonderful creation of ours to make a fire (not only fire but any type of) alarm. The program runs the usual to us part of code where the LED is turned on as the button is pressed and turned off as the button is pressed again. Then I have added the piezo buzzer to the circuit. The most challenging part was to differ the frequency of the sound to make it sound like an alarm. I’ve made a while loop that changes the frequency of the buzzer and it turns on like the LED switch. Code is attached bellow and the video will be uploaded later on, after the class presentation.

int buzz = 4;
int buttonPin = 2;
int ledPin = 3;
int ledState = LOW;
int prevButtonState = LOW;
bool inter = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
  attachInterrupt(0, pin_ISR, CHANGE);

}
void loop() {
  int currentButtonState = digitalRead(buttonPin);
  if (ledState == HIGH)
  {
    int i = 100;
    while (inter && i < 5000)
    {
      tone(buzz, i, 5);
      i++;
    }
  }
  if (currentButtonState == HIGH && prevButtonState == LOW) {
    if (ledState == HIGH) {
      ledState = LOW;
    } else if (ledState == LOW) {
      ledState = HIGH;
    }
  }

  digitalWrite(ledPin, ledState);
  prevButtonState = currentButtonState;
}

void pin_ISR()
{
  Serial.print("h");
  delay(100);
  inter = !inter;
}


 

Leave a Reply