/*********************************************************************** * Filename ResCap.c * This program demonstrates a way to simulate an analog read by measuring * the time it takes to charge a capacitor through a variable resistor. * It uses a photocell and a force sensitive resistor but the technique will * work to read a potentiometer or any resistive sensor. ***********************************************************************/ #include #include #include #include int photocellPin = 0; // Photocell and capacitor connected to pin zero. int fsrPin = 2; // Force sensitive resistor and capacitor connected to pin two. int ledPin = 1; // The LED connected to pin one. int toggle = 1; // On/off state of LED. unsigned long time; // Save the time to set the blink rate. /*********************************************************************** * RCtime() - Function, uses a digital pin to measure a resistive sensor * by first discharging capacitor then measuring the time it takes to * charge the capacitor through a variable resistor. When the voltage * rises to Vcc/2 the pin will go high. ***********************************************************************/ int RCtime(int RCpin) { int PinVal = 0; // Start with zero. pinMode(RCpin, OUTPUT); // Set pin to output and pull to LOW. (ground) digitalWrite(RCpin, LOW); delay(2); // Allow time to let capacitor discharge. pinMode(RCpin, INPUT); // Now set the pin to an input and... while (digitalRead(RCpin) == LOW) { // Count how long it takes to rise up to HIGH. PinVal++; // Increment to keep track of time. if (PinVal == 30000) { break; // if we got this far, the resistance is to high, } // no input to sensor, leave the loop. } PinVal = PinVal/25; // Divide by 25 for calibration. if(PinVal > 1023) PinVal = 1023; // Cap The value at 1023. return PinVal; // Returns zero - 1023. } /************************************************************************** * loop() - function runs in a continuous loop until program is stopped. * * More presure on the force sensitive resistor makes the LED blink faster. * More light on the photocell makes the light dimmer. **************************************************************************/ void loop(void) { int photocellReading = RCtime(photocellPin); // Read photocell. int fsrReading = RCtime(fsrPin); // Read force sensitive resistor. if(millis()-time > (fsrReading)) { toggle = !toggle; //If true not toggle time = millis(); //and reset time. } if(toggle) pwmWrite(ledPin, photocellReading*4); else pwmWrite(ledPin, 0); // printf("photocell %d - fsr %d\n", photocellReading, fsrReading); // uncomment for debugging. } /*********************************************************************** * setup() - function is run by main() one time when the program starts. ***********************************************************************/ void setup(void) { wiringPiSetup(); // Required. pinMode(ledPin, PWM_OUTPUT); time = millis(); // Save the time to set the blink rate. } /*********************************************************************** * main() - required ***********************************************************************/ int main(void) { setup(); while(1) { loop(); } }