/* Mario-inspired metal spindle productivity aid * Dan Armendariz * danallan@cs.berkeley.edu * * Makes a Mario-inspired coin noise when a piece of * paper is pierced onto the end of a metal spindle. */ // pin locations int pinSpeaker = 7, pinForce = 0; int rawForce = 0, lastForce = 0; // a tolerance, epsilon, for analog value fluctuations in the FSR int tolerance = 8; // how many loop iterations of force changes before we consider state change? int dIdt = 4, i = 0; bool DEBUG=true; void setup() { // enable the speaker pinMode(pinSpeaker, OUTPUT); // prep the Serial port for debugging output if(DEBUG) Serial.begin(9600); } void loop() { // fetch sensor values rawForce = analogRead(pinForce); // remember if the force has changed since the last iteration if (abs(rawForce - lastForce) > tolerance) i++; else if (i > 0) i--; // has the force changed more than our cutoff? if (i >= dIdt) { coin(); delay(1000); i = 0; } // print some debug if (DEBUG) { Serial.print(rawForce); Serial.print(" "); Serial.println(i); } lastForce = rawForce; } /* KA-CHING! this is inspired from the Mario coin collection noise. The frequencies are approximately: 1000 Hz for 0.08s. 1333 Hz for 0.80s. Ththe implementation below is a vague approximation. */ void coin() { digitalWrite(pinSpeaker, LOW); for (int i = 0; i < 100; i++) { digitalWrite(pinSpeaker, HIGH); delayMicroseconds(360); digitalWrite(pinSpeaker, LOW); delayMicroseconds(360); } for (int i = 0; i < 200; i++) { digitalWrite(pinSpeaker, HIGH); delayMicroseconds(240); digitalWrite(pinSpeaker, LOW); delayMicroseconds(240); } }