/* * Hacked Cassette deck * Code: Daniel PalmŽr * Concept: Daniel PalmŽr, David …sterlind, Lisa Wallin * Malmš hšgskola, K3 2009 * vmguld@gmail.com * vmguld.se * */ // Declare pins int motorPin = 9; int speakerPin = 5; int ldrPin = 4; int potPin = 5; // Variables for the values used int potVal; int ldrVal; int speakVal; // Stores the LDR-settings in the setup int ldrMinVal; int ldrMaxVal; // Max number of milliseconds which the sound will stop when turning the potentiometer // ex: 400 = 400ms noise, then 400ms silence int speakerTimeout = 400; // minimum speaker timeout (if lesser than speakerTimeoutTreshold = no timeout = steady flow to speakers) int speakerTimeoutTreshold = 3; // Min/max values read from the potentiometer(standard analog values 0- 1023) int potMaxVal = 1023; int potMinVal = 0; // Max/min values for the motor (our cassette stopped completely at 150) int motorMaxVal = 255; int motorMinVal = 160; // Max/min values for output to the speakers (our turned all silent below 80) int speakerMaxVal = 255; int speakerMinVal = 80; void setup() { // Set pins pinMode(motorPin, OUTPUT); pinMode(speakerPin, OUTPUT); // Calibrate the LDR // Wait 5 sec delay(5000); ldrMinVal = analogRead(ldrPin); // Then cover the LDR for 5 sec delay(5000); ldrMaxVal = analogRead(ldrPin); } void loop() { // Read values from sensors ldrVal = constrain(analogRead(ldrPin), ldrMinVal, ldrMaxVal); ldrVal = map(ldrVal, ldrMinVal, ldrMaxVal, motorMaxVal, motorMinVal); potVal = map(analogRead(potPin), potMinVal, potMaxVal, 0, speakerTimeout); // Adjust volume output to speakerTimeout speakVal = map(potVal, 0, speakerTimeout, speakerMinVal, speakerMaxVal); // Put out values to the cassette deck analogWrite(motorPin, ldrVal); analogWrite(speakerPin, speakVal); if (potVal > speakerTimeoutTreshold) { delay(potVal); analogWrite(speakerPin, 0); delay(potVal); } }