Timing Issues

Hi, I wonder if someone could help me please?

I am builing a basic (for now) intervalometer for my DSLR.
I have it up and running and am pretty happy with it so far.
However, I am having trouble with varying the delay time using a pot.
I have mapped my pot value but keep getting strange readings and the loop freezes.
When I turn the pot above about half way, I start getting eratic negative values.
Could anybody tell me why this might be happening please?

I have included my code below.
I am a newb to this so please forgive my coding.


// Basic intervalometer for DSLR.

// Pins 4 and 5 connected to the camera cable via two optocouplers.

// Action 1 (FOCUS) is turned on for one second to allow the cama=era to auto focus.

// Action 2 (SHUTTER) is then turned on for one second to allow for the shutter to activate.

// The interval between shots is controlled by the potentiometer.

// The potentiometer is connected to +5v, Gnd and Pin A0.

#include <Wire.h>            // Include the I2C Library.

#include <HTI2CLCD.h>        // Include the I2C-LCD Backpack Library.

HTI2CLCD lcd;                // Create an instance of the I2C-LCD Backpack.

const int  lcd_address=58;   // I2C Address of LCD backpack.

int bl=0;                    // Backlight power value.

#define FOCUS  4             // Define the Focus Pin as Digital Pin 4.             

#define SHUTTER  5           // Define the Shutter Pin as Digital Pin 5.          

int potpin0 = A0;    // Select the input pin for the potentiometer.
int pot0 = 0;        // Variable to store the potetiometer reading.

void setup()

delay(200);                                  // Give LCD time to configure itself.
    Wire.begin();                            // join i2c bus (address optional for master).
    lcd.setType(lcd_address,2,16);           // Define rows and columns for LCD.
    lcd.clear(lcd_address);                  // Clear LCD.
    lcd.backLight(lcd_address,100);           // Set backlight to 100.
    // Display String
    lcd.print(lcd_address,"PR-DSLR ver1.0");  //Print splash info.
// Initialise the Arduino data pins for OUTPUT.

  pinMode(FOCUS, OUTPUT);       




 void loop()


    pot0 = analogRead(potpin0);  // Reads the value of Pot. 
    pot0 = map(pot0, 0, 1023, 1000, 60000);  // Maps the pot reading to a delay time.
    lcd.clear(lcd_address);                  // Clears the LCD.
    lcd.print(lcd_address,pot0);             // Displays the mapped pot reading (Range 1 second to 1 minute).
    lcd.print(lcd_address," Millisecs");     //Adds the word Millisecs after the mapped pot reading.
   digitalWrite(FOCUS,HIGH);           // Turns optocoupler 1 on, activating the auto focus.

   delay(1000);                        // Wait 1 second.

   digitalWrite(SHUTTER,HIGH);         // Turns optocoupler 2 on, activating the shutter.

   delay(1000);                        // Wait 1 second

   digitalWrite(FOCUS,LOW);            // Turns optocoupler 1 off.
   digitalWrite(SHUTTER,LOW);          // Turns optocoupler 2 off.

   delay(pot0);                       // Wait 15 seconds.


Try putting an L suffix on the 60000:

    pot0 = map(pot0, 0, 1023, 1000, 60000L);  // Maps the pot reading to a delay time.

It's too big to fit in an int, but the compiler assumes that it should be one and screws it up for you.

Hi Bill, thanks for the quick reply.

I just tried it and unfortunately it's just the same.

Thanks very much though, greatly appreciated I can assure you.


    pot0 = map(pot0, 0, 1023, 1000, 60000);  // Maps the pot reading to a delay time.

You are trying to stuff a value that can range from +1000 to [u]+60000[/u] into a variable that can hold a value from -32768 to [u]+32767[/u]. pot0 needs to be a different type. unsigned int, long, unsigned long, or uint16_t come to mind.

unsigned long pot0 = 0;        // Variable to store the potetiometer reading.


    pot0 = map(pot0, 0, 1023, 1000, 60000L);  // Maps the pot reading to a delay time.

Now you can use long, LONG delays!


That works perfectly.

Thanks very much to everyone.