I constructed a journal which uses a force sensor to turn an integrated reading light on and off when the cover is opened and closed. The journal also plays hello and goodbye musical bits on open and close and occasionally plays a soft intermittent chime when closed for long periods of time - a plea for attention.Components UsedBreadboard (1)Arduino Decimila Board (1)Piezo Speaker (1)Super-Bright LED with leads (scavenged from an old project)Resistor (1)Cable lengths (6)Jumpers (1)Moleskine Unruled Journal (1) Arduino & Components sans Journal
Journal Integrating Force Sensor and LED into Back Cover Journal in Action Arduino Codeint forcePin = 0; // select the input pin for the force sensorint speakerPin = 7;int val = 0;byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'}; int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";byte hello[] = "1c1p1d1p1e1p1f1p1g1p1a1p1b1p1C1p";byte goodbye[] = "1C1p1b1p1a1p1g1p1f1p1e1p1d1p1c1p";byte wave[] = "1f1c1f1c";int count = 0;int count2 = 0;int count3 = 0;int MAX_COUNT = 24;int statePin = LOW;boolean isOpen = true;int waveWait = 0;void setup() { pinMode(speakerPin, OUTPUT); beginSerial(9600); Serial.println("ready"); digitalWrite(speakerPin, LOW); val = analogRead(forcePin); // read value from the sensor if(val > 100) isOpen = false;}void loop() { val = analogRead(forcePin); // read value from the sensor Serial.print("val="); Serial.print(val); Serial.println(); //check to see if the journal has been opened or closed if(val < 1 && !isOpen){ //light up the LEDs and play welcome sound playHello(); analogWrite(9, 255); isOpen = true; } else if(val > 100 && isOpen){ //dim LED and play goodbye sound playGoodbye(); analogWrite(9, 0); isOpen = false; } if(!isOpen){ //play a reminder chime if(waveWait++%50000 == 0){ playWave(); } }}void playHello(){ playJams(hello,16);}void playGoodbye(){ playJams(goodbye,16);}void playWave(){ playJams(wave,8);}//plays the a series of notes defined in an arrayvoid playJams(byte jams[], int length){ for (count = 0; count < length; count++) { statePin = !statePin; for (count3 = 0; count3 <= (jams[count*2] - 48) * 30; count3++) { for (count2=0;count2<8;count2++) { if (names[count2] == jams[count*2 + 1]) { digitalWrite(speakerPin,HIGH); delayMicroseconds(tones[count2]); digitalWrite(speakerPin, LOW); delayMicroseconds(tones[count2]); } if (jams[count*2 + 1] == 'p') { // make a pause of a certain size digitalWrite(speakerPin, 0); delayMicroseconds(500); } } } }}