AVR-Detecting Movement:
The most common tilt sensor is a ball bearing in a box with contacts at one end. When the box is tilted the ball rolls away from the contacts and the connection is broken. When the box is tilted to roll the other way the ball touches the contacts and completes a circuit. Markings, or pin configurations, show which way the sensor should be oriented. Tilt sensors are sensitive to small movements of around 5 to 10 degrees when
oriented with the ball just touching the contacts. If you position the sensor so that the ball bearing is directly above (or below) the contacts, the LED state will only change if it is turned right over. This can be used to tell if something is upright or upside down. To determine if something is being shaken, you need to check
how long it’s been since the state of the tilt sensor changed. If it hasn’t changed for a time you consider significant, the object is not shaking. Changing the orientation of the tilt sensor will change how vigorous the shaking needs to be to trigger it. The following code lights an LED when the sensor is shaken:
This sketch uses a switch that closes a circuit when tilted, called a tilt sensor. The switch recipes will not work with a tilt sensor substituted for the switch. The sketch below will switch on the LED attached to pin 11 when the tilt sensor is tilted one way, and the LED connected to pin 12 when it is tilted the other way:
/* tilt sketch a tilt sensor attached to pin 2, lights one of the LEDs connected to pins 11 and 12 depending on which way the sensor is tilted */
const int tiltSensorPin = 2; //pin the tilt sensor is connected to
const int firstLEDPin = 11; //pin for one LED
const int secondLEDPin = 12; //pin for the other
void setup()
{
pinMode (tiltSensorPin, INPUT); //the code will read this pin
digitalWrite (tiltSensorPin, HIGH); // and use a pull-up resistor
pinMode (firstLEDPin, OUTPUT); //the code will control this pin
pinMode (secondLEDPin, OUTPUT); //and this one
}
void loop()
{
if (digitalRead(tiltSensorPin)){ //check if the pin is high
digitalWrite(firstLEDPin, HIGH); //if it is high turn on firstLED
digitalWrite(secondLEDPin, LOW); //and turn off secondLED
}
else{ //if it isn't
digitalWrite(firstLEDPin, LOW); //do the opposite
digitalWrite(secondLEDPin, HIGH);
}
}
The most common tilt sensor is a ball bearing in a box with contacts at one end. When the box is tilted the ball rolls away from the contacts and the connection is broken. When the box is tilted to roll the other way the ball touches the contacts and completes a circuit. Markings, or pin configurations, show which way the sensor should be oriented. Tilt sensors are sensitive to small movements of around 5 to 10 degrees when
oriented with the ball just touching the contacts. If you position the sensor so that the ball bearing is directly above (or below) the contacts, the LED state will only change if it is turned right over. This can be used to tell if something is upright or upside down. To determine if something is being shaken, you need to check
how long it’s been since the state of the tilt sensor changed. If it hasn’t changed for a time you consider significant, the object is not shaking. Changing the orientation of the tilt sensor will change how vigorous the shaking needs to be to trigger it. The following code lights an LED when the sensor is shaken:
This sketch uses a switch that closes a circuit when tilted, called a tilt sensor. The switch recipes will not work with a tilt sensor substituted for the switch. The sketch below will switch on the LED attached to pin 11 when the tilt sensor is tilted one way, and the LED connected to pin 12 when it is tilted the other way:
/* tilt sketch a tilt sensor attached to pin 2, lights one of the LEDs connected to pins 11 and 12 depending on which way the sensor is tilted */
const int tiltSensorPin = 2; //pin the tilt sensor is connected to
const int firstLEDPin = 11; //pin for one LED
const int secondLEDPin = 12; //pin for the other
void setup()
{
pinMode (tiltSensorPin, INPUT); //the code will read this pin
digitalWrite (tiltSensorPin, HIGH); // and use a pull-up resistor
pinMode (firstLEDPin, OUTPUT); //the code will control this pin
pinMode (secondLEDPin, OUTPUT); //and this one
}
void loop()
{
if (digitalRead(tiltSensorPin)){ //check if the pin is high
digitalWrite(firstLEDPin, HIGH); //if it is high turn on firstLED
digitalWrite(secondLEDPin, LOW); //and turn off secondLED
}
else{ //if it isn't
digitalWrite(firstLEDPin, LOW); //do the opposite
digitalWrite(secondLEDPin, HIGH);
}
}
No comments:
Post a Comment