Unit 25: On the edge

 

 

 

Code for Sketch One: 



const int tiltPin = 13; // Tilt sensor connected to pin 13
const int buzzerPin = 3; // Buzzer connected to pin 3

void setup() {
  pinMode(tiltPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int tiltState = digitalRead(tiltPin); // Read the state of the tilt sensor
  Serial.print("Tilt State: ");
  Serial.println(tiltState);

  if (tiltState == LOW) { // If tilted
    Serial.println("Buzzer ON");
    digitalWrite(buzzerPin, HIGH); // Turn on buzzer
  } else { // If upright
    Serial.println("Buzzer OFF");
    digitalWrite(buzzerPin, LOW); // Turn off buzzer
  }

  delay(500); // Delay for stability
}


  

  

 

Notes: