Step 5Detect switch state in Processing
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 Step | Download PDFView All Steps | Next Step » |











































...
is there any way you could post the code in a txt file or something?
I find this really confusing!