User Tools

Site Tools


oak:tutorials:rollingball

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
oak:tutorials:rollingball [2016/03/26 00:19]
nog3 created
oak:tutorials:rollingball [2016/04/11 05:58] (current)
nog3
Line 10: Line 10:
 |  Breadboard| 1| | |  Breadboard| 1| |
 |  SW-520D Rolling Ball Switch | 1| | |  SW-520D Rolling Ball Switch | 1| |
-|  ​1k Resistor | 1 | Brown, Black, ​Red |+|  ​10k Resistor | 1 | Brown, Black, ​Orange, Gold|
 |  M to M 30cm Jumper Wire| 3| Red, Black, Blue| |  M to M 30cm Jumper Wire| 3| Red, Black, Blue|
  
Line 19: Line 19:
 {{:​oak:​tutorials:​tiltyswitchexample.jpg?​direct&​200|A tilt switch}} {{:​oak:​tutorials:​tiltyswitchexample.jpg?​direct&​200|A tilt switch}}
  
-**Pull-Down Resistor:** In this lesson we will use a Pull-Down resistor to make sure we can reliably detect ​button presses. If an I/O pin is 'left floating'​ reading it's value can result in either HIGH or LOW. By attaching a resistor between the I/O pin of the Oak and GND we can ensure that if the button ​is not pressed, the state is always LOW.+**Pull-Down Resistor:** In this lesson we will use a Pull-Down resistor to make sure we can reliably detect ​the tilt switch tilting. If an I/O pin is 'left floating'​ reading it's value can result in either HIGH or LOW. By attaching a resistor between the I/O pin of the Oak and GND we can ensure that if the tilt switch ​is tilted, the state is always LOW.
  
 =====Circuit:​===== =====Circuit:​=====
Line 31: Line 31:
  
 <​code>​ <​code>​
 +// define which pins to use
 +const int tilt = 2;     // the number of the tilt switch pin
 +const int ledPin = 1;      // the number of the LED pin
  
 +// set a variable to record the switch'​s state
 +int tiltState = 0;         // variable for reading the tilt switch status
 +
 +void setup() {
 +  // initialize the LED pin as an output:
 +  pinMode(ledPin,​ OUTPUT);
 +  // initialize the tilt switch pin as an input:
 +  pinMode(tilt,​ INPUT);
 +}
 +
 +void loop() {
 +  // read the state of the tilt switch:
 +  tiltState = digitalRead(tilt);​
 +
 +  // check if the tilt switch is tilted.
 +  // if it is, the tiltState is HIGH:
 +  if (tiltState == LOW) {
 +    // turn LED on:
 +    digitalWrite(ledPin,​ HIGH);
 +  }
 +  else {
 +    // turn LED off:
 +    digitalWrite(ledPin,​ LOW);
 +  }
 +}
 </​code>​ </​code>​
  
 +In this example code we create a variable called tiltState that allows us to store the current status of the tilt switch. In the loop() we constantly check its state and then respond based on this. This is very similar to what you'd do with a [[oak:​tutorials:​button|Oak:​ Breadboard Button Lesson]].
 +
 +How about we kick it up a notch?
 +
 +== Letterbox Detector ==
 +
 +This example code, along with the same circuit from above will notify us every time the ball is tilted and returned. This is perfect for detecting a letterbox being opened and closed by the postie.
 +
 +<​code>​
 +// define which pins to use
 +const int tilt = 2;     // the number of the tilt switch pin
 +const int ledPin = 1;      // the number of the LED pin
 +
 +// set a variable to record the switch'​s state
 +int tiltState = 0;         // variable for reading the tilt switch status
 +int tilted;
 +
 +void setup() {
 +  // initialize the LED pin as an output:
 +  pinMode(ledPin,​ OUTPUT);
 +  // initialize the tilt switch pin as an input:
 +  pinMode(tilt,​ INPUT_PULLUP);​
 +}
 +
 +void loop() {
 +  // read the state of the tilt switch:
 +  tiltState = digitalRead(tilt);​
 +
 +  // check if the tilt switch is tilted.
 +  // if it is, the tiltState is HIGH:
 +  if (tiltState == HIGH) {
 +    // set tilted to 1
 +    tilted = 1;
 +  }
 +  else {
 +    // turn LED off:
 +    if (tilted = 1) {
 +      // Tell the Particle Cloud we've done a full tilt
 +      Particle.publish("​Tilted","​Totally"​);​
 +      // Turn the LED on and off to let us know we're publishing this
 +      digitalWrite(ledPin,​ HIGH);
 +      delay(1000);​
 +      digitalWrite(ledPin,​ LOW);
 +      // Set tilted state back to 0
 +      tilted = 0;
 +    }
 +  }
 +  // Let's wait a second to give the postie time to close it before we notify the cloud
 +  delay(1000);​
 +}
 +</​code>​
  
 +This code uses a second variable //tilted// in tandem with //​tiltState//​ to determine when we've completed a full on-off of the tilt. Note that tilt sensors are very sensitive so this may trigger unexpectedly at times.
  
 +Once you've got this code running on your Oak, check out https://​dashboard.particle.io/​user/​logs and tilt your oak over and back. You should get something like this:
 +{{ :​oak:​tutorials:​tiltedtotally.png?​nolink&​800|}}
 =====Conclusion:​===== =====Conclusion:​=====
  
 +Tilt switches are a very simple analog way of checking if something has tilted. They can be used to detect basic movement but are very sensitive and don't like vibrations. Always make sure to debounce them with pullup resistors and if you find them too sensitive, debouncing code. [[https://​www.arduino.cc/​en/​Tutorial/​Debounce|The arduino debouncing tutorial]] is a good primer on this.
oak/tutorials/rollingball.1458976773.txt.gz · Last modified: 2016/03/26 00:19 by nog3