This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision Last revision Both sides next revision | ||
|
oak:tutorials:rollingball [2016/04/11 05:10] nog3 [Code:] Forgot to add code, whoops. |
oak:tutorials:rollingball [2016/04/11 05:55] nog3 [Conclusion:] |
||
|---|---|---|---|
| Line 62: | Line 62: | ||
| </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. | ||