# DIY tilting sensor with Arduino UNO.

Hey guys, a newbie here, pardon to disturb you for request some help.
I’m running on a project about tilting sensor, but unfortunately I’d accidentally fried my accelerator sensor, and I’d to use the self make DIY tilting sensor.
The DIY tilting sensor is just like a V shape, with each 45 degree, and there’s a bearing ball inside the tube. When the bearing ball touches either the both end of the top in the V-tube, it will trigger the buzzer and LED will blink. Despite self-making DIY tilting sensor, we still managed to plug the wires from the tube to the jumpers. So here’s the situation, in the below sketch, INPIN = the tilting sensor. When the bearing balls reach both the end of the side, it will close the circuit and the electricity from the battery pack which installed extend for the tilting sensor will flow to the breadboard and the board will activate the buzzer and LED. I’m trying to program the whole system with the following code, but it ended up the buzzer makes sound and the LED Blinks although sometimes the sensor is not in 45 degree.
Anyone can help with this?

``````#define buzzer 8
void buzz(int targetPin, long frequency, long length) {
long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
//// 1 second's worth of microseconds, divided by the frequency, then split in half since
//// there are two phases to each cycle
long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing
//// multiply frequency, which is really cycles per second, by the number of seconds to
//// get the total number of cycles to produce
for (long i=0; i < numCycles; i++){ // for the calculated length of time...
digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
delayMicroseconds(delayValue); // wait for the calculated delay value
digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
delayMicroseconds(delayValue); // wait againf or the calculated delay value
}
}
#define LEDPIN 13
#define INPIN 2
#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

// define some values used by the panel and buttons
int backLight   = 10;    // LCD Panel Backlight LED connected to digital pin 10
int lightLevel  = 170;   // Initialise light full on
int lcd_key     = 0;
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5

{
// my [Mark Bramwell's] buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
if (adc_key_in < 50)   return btnRIGHT;
if (adc_key_in < 195)  return btnUP;
if (adc_key_in < 380)  return btnDOWN;
if (adc_key_in < 555)  return btnLEFT;
if (adc_key_in < 790)  return btnSELECT;
return btnNONE;  // when all others fail, return this...
}

void setup()
{
pinMode(8, OUTPUT);
pinMode(13, OUTPUT);
pinMode(2, INPUT);
lcd.begin(16, 2);              // start the LCD library
lcd.setCursor(0,0);          // move cursor to beginning of line "0"
lcd.print("USBKSystemONLINE"); // print a simple message

}

void loop(){

analogWrite(backLight, lightLevel);
lcd.setCursor(13,1);            // move to position 13 on the second line
lcd.print(lightLevel);

lcd.setCursor(0,1);            // move to the begining of the second line

switch (lcd_key)               // depending on which button was pushed, we perform an action
{
case btnRIGHT:
{
lcd.print("LED On          ");
lightLevel = 170;
break;
}
case btnLEFT:
{
lcd.print("LED Off         ");
lightLevel = 1;
break;
}
case btnUP:
{
if (lightLevel < 170) lightLevel += 1;
break;
}
case btnDOWN:
{
if (lightLevel > 1) lightLevel -= 1;
break;
}
case btnSELECT:
{
lcd.print("Enable Alarm");
digitalWrite(LEDPIN, LOW);
buzz(8,2500,500);
break;
}
case btnNONE:
{
lcd.print("                ");
break;
}
}
{
digitalWrite(LEDPIN, HIGH);

buzz(8, 5000, 500); // buzz the buzzer on pin 4 at 2500Hz for 500 milliseconds
delay(250);
lcd.print("Alarm Armed ") ;
}
else {
digitalWrite(LEDPIN, LOW);
buzz(8,0,0);
}
}

}
``````

In setup()

pinMode(2,INPUT); digitalWrite(2,HIGH);//MISSING LINE