// main time constants #define NumberOfPhotos 20 #define PhotoSavingTime 300 #define InitialWait 100 // Shutter release definitions #define ReleaseMotor OUT_A #define ReleaseTouch SENSOR_1 #define PressWait 5 #define PressedWait 5 #define ReleaseWait 15 // motors & sensors #define RotationMotor OUT_C #define RotationSense SENSOR_2 #define CalibrateTime 15 #define Hyster 10 #define CountPerTurn 160 // Threshold for the light sensor encoder int Threshold; // Position of the turntable int RotAngle; int count=0; task main() { SetPower(OUT_A,1); SetSensor(ReleaseTouch ,SENSOR_TOUCH); SetSensorType(RotationSense,SENSOR_TYPE_LIGHT); SetSensorMode(RotationSense,SENSOR_MODE_RAW); SetPower(RotationMotor,2); Wait(InitialWait); Calibrate(); RotAngle=0; start CountRotation; while (count < NumberOfPhotos) { count++; OnRev(RotationMotor); until (RotAngle >= (count * CountPerTurn)/NumberOfPhotos); Off(RotationMotor); Wait(PhotoSavingTime); ShutterRelease(); Wait(PhotoSavingTime); } StopAllTasks(); } // Measures the light in white and black zones of the encoder // and place the threshold in-between // As an added bonus, finish by rotating the plate in forward direction, // thus compensating looseness of gears sub Calibrate() { int low = 1000; int high = 0; int temp; Wait(50); OnFwd(RotationMotor); repeat (CalibrateTime) { temp = RotationSense; if (temp < low) { low = temp; } if (temp > high) { high = temp; } Wait(1); } Off(RotationMotor); Wait(50); OnRev(RotationMotor); repeat (CalibrateTime) { temp = RotationSense; if (temp < low) { low = temp; } if (temp > high) { high = temp; } Wait(1); } Off(RotationMotor); Wait(50); Threshold = (high+low)/2; } //Counts the number of transitions between black and white task CountRotation() { while (true) { until (RotationSense < Threshold - Hyster); RotAngle++; until (RotationSense > Threshold + Hyster); RotAngle++; } } sub ShutterRelease() { OnRev(ReleaseMotor); until (ReleaseTouch == 1); Wait(PressWait); Off(ReleaseMotor); Wait(PressedWait); OnFwd(ReleaseMotor); until (ReleaseTouch == 0); Wait(ReleaseWait); Off(ReleaseMotor); }