Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- int pwmPin = 9; // output pin supporting PWM
- int inPin = 2; // voltage connected to analog pin 3, e.g. a potentiometer
- int aperturePin = 3; //Pin 2 for aperture selection.
- int pushButton = 2;
- void setup()
- {
- pinMode(pushButton, INPUT);
- pinMode(pwmPin, OUTPUT); // sets the pin as output
- TCCR1B = TCCR1B & 0b11111000 | 0x01; //31250 hz
- Serial.begin(9600);
- }
- void loop()
- {
- analogWrite(pwmPin, 0);
- int buttonState = digitalRead(pushButton);
- if(buttonState == 1){
- shutter(analogRead(3),analogRead(inPin)); //47 max, 25 min.
- }
- }
- void delay_x(uint32_t millis_delay)
- {
- uint16_t micros_now = (uint16_t)micros();
- while (millis_delay > 0) {
- if (((uint16_t)micros() - micros_now) >= 1000) {
- millis_delay--;
- micros_now += 1000;
- }
- }
- }
- int shutter(float aperture, float shutterSpeed)
- //Make sure no variables are out of bound
- {
- if(shutterSpeed <= 4){
- shutterSpeed = 4;
- }
- if(aperture > 255){
- aperture = 255;
- }
- //Calculate actual aperture voltage
- // float apertureBias = 6.477707151*exp(-63.42518309/aperture);
- float apertureBias = -4.073328941*pow(10,-2)*(exp(5.369935444*pow(10,-2))*aperture - exp(-4.073328941*pow(10,-2))*aperture)/(-4.073328941*pow(10,-2) - 5.369935444*pow(10,-2));
- float correctAperture = 9.821433066*pow(10,-1)*shutterSpeed/(shutterSpeed - apertureBias);
- correctAperture = aperture*correctAperture;
- //Warm up conductor
- analogWrite(pwmPin, 255);
- delay_x(1);
- analogWrite(pwmPin, 0);
- delay_x(1);
- //Feedback
- Serial.println(shutterSpeed);
- Serial.println(correctAperture);
- //Trigger shutter
- analogWrite(pwmPin, correctAperture);
- delay_x(shutterSpeed); //shutter speed
- analogWrite(pwmPin, 0);
- delay_x(100);
- analogWrite(pwmPin, 0);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement