3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Give Your Cat Twitter Powers

Step 5Detect switch state in Processing

When your cat first jumps on the bed or moves around a bunch, our switch values will probably bounce between on and off for a while. To avoid firing off lots of spurious update messages, we'll "debounce" our switch by requiring that the value has stabilized for a number of iterations before we fire any events.

Here's Processing code that initializes Arduino and checks the switch state in its main loop:

import cc.arduino.*;import processing.serial.*;// Detect Arduino switch state from Processing// via Firmata, with debouncing//-----ARDUINO VARS--------------------Arduino arduino;int matPin = 2; // which input pin are we connected to?int matCounter=0; // counter for debouncing switch inputint bounceLimit=100; //debouncing limitint curMatState = Arduino.HIGH; //current switch stateint lastMatState = Arduino.HIGH; //last switch stateint lastMatEvent = Arduino.LOW; //last event we firedvoid setup() {  // ARDUINO  arduino = new Arduino(this, Arduino.list()[0]); // v2    arduino.pinMode(matPin, Arduino.INPUT); //set input pin  arduino.digitalWrite(matPin, Arduino.HIGH); //enable pull-up}void draw() {  // read current mat state  pollMatState();  // wait a bit  delay(10);}// Read current mat state with debounce timer and toggle check// calls fireMatEvent() when state of mat changed and has been stable for a whilevoid pollMatState() {  curMatState = arduino.digitalRead(matPin);     if(curMatState == lastMatState) {      //still in same state - incrase bounce counter      if(matCounter < bounceLimit) {        matCounter++;      } else if (matCounter==bounceLimit) {        //we've debounced enough and are ready to fire        if(lastMatEvent != curMatState) {          //only fire if this event is difft from last one          fireMatEvent(curMatState);          lastMatEvent = curMatState;        }        matCounter++;      } else if (matCounter > bounceLimit) {        // event already fired - do nothing      }     } else {       //restart count      matCounter=0;    }    lastMatState = curMatState;}// Fire a new mat state change eventvoid fireMatEvent(int state) {  if(state==Arduino.LOW) {    println("cat is off mat");  } else {    println("cat is on mat");  }   }
« Previous StepDownload PDFView All StepsNext Step »
1 comment
Feb 19, 2010. 7:17 PMSonic says:
This is What I was looking for! thank you!!
...
 is there any way you could post the code in a txt file or something?
I find this really confusing!

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
6
Followers
2
Author:bpunkt