Advertisement
Guest User

Shutter

a guest
Apr 25th, 2013
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. #include <math.h>
  2.  
  3. int pwmPin = 9; // output pin supporting PWM
  4. int inPin = 2; // voltage connected to analog pin 3, e.g. a potentiometer
  5. int aperturePin = 3; //Pin 2 for aperture selection.
  6. int pushButton = 2;
  7.  
  8.  
  9.  
  10. void setup()
  11. {
  12.   pinMode(pushButton, INPUT);
  13.   pinMode(pwmPin, OUTPUT); // sets the pin as output
  14.   TCCR1B = TCCR1B & 0b11111000 | 0x01; //31250 hz
  15.   Serial.begin(9600);
  16. }
  17.  
  18. void loop()
  19. {
  20.  
  21.  
  22.   analogWrite(pwmPin, 0);
  23.  
  24.  
  25.  
  26.   int buttonState = digitalRead(pushButton);
  27.  
  28.   if(buttonState == 1){
  29.  
  30.     shutter(analogRead(3),analogRead(inPin));  //47 max, 25 min.
  31.   }
  32.  
  33. }
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40. void delay_x(uint32_t millis_delay)
  41. {
  42.   uint16_t micros_now = (uint16_t)micros();
  43.  
  44.   while (millis_delay > 0) {
  45.     if (((uint16_t)micros() - micros_now) >= 1000) {
  46.       millis_delay--;
  47.       micros_now += 1000;
  48.     }
  49.   }  
  50. }
  51.  
  52.  
  53.  
  54.  
  55.  
  56. int shutter(float aperture, float shutterSpeed)
  57. //Make sure no variables are out of bound
  58. {
  59.   if(shutterSpeed <= 4){
  60.     shutterSpeed = 4;  
  61.   }
  62.  
  63.  
  64.  
  65.   if(aperture > 255){
  66.     aperture = 255;
  67.   }
  68.   //Calculate actual aperture voltage
  69.   //  float apertureBias = 6.477707151*exp(-63.42518309/aperture);  
  70.   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));
  71.  
  72.  
  73.   float correctAperture = 9.821433066*pow(10,-1)*shutterSpeed/(shutterSpeed - apertureBias);
  74.   correctAperture = aperture*correctAperture;
  75.   //Warm up conductor
  76.   analogWrite(pwmPin, 255);
  77.   delay_x(1);
  78.   analogWrite(pwmPin, 0);
  79.   delay_x(1);
  80.  
  81.  
  82.  
  83.   //Feedback
  84.   Serial.println(shutterSpeed);
  85.   Serial.println(correctAperture);
  86.   //Trigger shutter
  87.   analogWrite(pwmPin, correctAperture);  
  88.   delay_x(shutterSpeed);   //shutter speed
  89.   analogWrite(pwmPin, 0);
  90.   delay_x(100);
  91.   analogWrite(pwmPin, 0);
  92.   return 1;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement