Stap 3: Het programma
De RCtime() functie doet de lezing van de sensoren. Ik probeerde om het te schrijven als generieke mogelijk zodat het kan worden opgeheven uit en gebruikt in andere programma's. Misschien vindt u soms waar enkele aanpassingen zullen moeten worden gemaakt voor het kalibreren. In de functie van de lus in deze lijn bijvoorbeeld "if(toggle) pwmWrite (ledPin, photocellReading * 4);" Ik vermenigvuldigen de fotocel lezen door 4. Uw resultaten kunnen verschillen, ik heb twee van de dezelfde fotocellen en ze zich anders gedragen.
Toen ik begon te werken aan het project heb ik mijn eerste experimenteren op een Arduino. Toen ik het werkt ik het overgedragen aan de RaspberryPi en het werkte niet. Het kostte me een lange tijd om erachter te komen wat er mis was. De RaspberryPi is geen hot rod, maar het is vele malen sneller dan een Arduino. De oplossing was om toe te voegen van de "delay(2)" in de functie van de RCtime() tijd te geven de condensator te lozen.
Omdat de hele program stopt uitgevoerd tijdens een delay(), gebruikt de code voor de knippersnelheid van de millis() in plaats van delay(). Met behulp van millis() het programma blijft actief en wijzigingen zijn onmiddellijk. "tijd" is een globale variabele gedefinieerd net onder de kop programma en de omvat. Deze wordt geïnitialiseerd in de Setup-functie. Let op het gebruik van de tijd, en knevel variabelen, evenals de millis() functie in de loop-functie om te zien hoe dit wordt gedaan.
/*********************************************************************** * 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 <stdio.h> #include <stdlib.h> #include <unistd.h> #include <wiringPi.h> 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(); } }