Controlling Delay with a potentiometer

Hi all.

Im Tring to make 2 delay timer with 2 potentiometer. So the first one will delay from 10 seconds to 2 minutes and the second one from 30 minutes to 6 hours

I did try MAP function but it look like I'm over the arduino bit capacity.

Hope someone can help me.

Merci.

Potentiometer_To_Control_Delay_Timer_for_forum.ino (1.73 KB)

Please post your code with code tags.. Like this:

int potMin = 0;    // potentiometer minute time
int potHours = 1;    // potentiometer hours time
int ledMin = 12;     // select the pin for the minute LED
int ledHours = 11;     // select the pin for the hours LED
int Minute = 0;    // variable to store the value coming from the sensor
int Hours = 0;    // variable to store the value coming from the sensor


void setup() {
  pinMode(ledMin, OUTPUT);                // declare the ledPin as an OUTPUT
  pinMode(ledHours, OUTPUT);             // declare the ledPin as an OUTPUT
  Serial.begin(9600);
}

void loop() {
  Minute = analogRead(potMin);            // read the value from the Minute  sensor
  Hours = analogRead(potHours);        // read the value from the Hours sensor
 
  Serial.print("Min sensor = ");          // print the value from the Minute sensor
  Serial.print(Minute);  
  Serial.print("\t Time in min = ");      // print the convertion in minuts       
  Minute = map( Minute, 0,1023, 10000, 122400);  // 2 minut max so 0 will be 10 secound and 1020 will be 2 min .
  Serial.println(Minute);           
  
  Serial.print("Hours sensor = ");           // print the value from the Hours sensor 
  Serial.print(Hours);  
  Serial.print("\tTime in hours = ");           // print the convertion in hours
  Hours = map( Hours, 0, 1023,1841400,22096800  );
  Serial.println(Hours);           // 1841400 is equal to 30 minutes and 22096800 is equal to 6 hours.
  Serial.println(" ");


   digitalWrite(ledMin, LOW);          // turn the led min  ON
   digitalWrite(ledHours, HIGH);          // turn the led hours OFF
   delay(Minute);  

   digitalWrite(ledMin, HIGH);         // turn the led min OFF
   digitalWrite(ledHours, LOW);         // turn the led hours ON
   delay(Hours);              
}

Arduino is 16bit, numbers like "1841400" does not fit in 16 bit (65535 unsigned max). You need to specifically tell the compiler to use longs, or even better - declare them as constants:

//Using constants
const long HOUR_MIN = 1841400;
const long HOUR_MAX = 22096800;
...
Hours = map( Hours, 0, 1023, HOURS_MIN, HOURS_MAX);
...

//Using inline declarations
Hours = map( Hours, 0, 1023, 1841400L, 22096800L); //Note the "L"

Hi Danois90

Thanks for the reply, I will try it.

How do I post with code tag ? Sorry I'm super new in arduino world

Read this post :slight_smile:

Ok,

Thanks again for the '' const long '' declaration and the inline one.

I be leave I did program both way like it should. But got result witch seam not right. You can see the pictures.

MINUTE_MIN Work fine giving me 10000;
Both HOURS_MIN give me 6392 and should give me 1841400 ( -xxxxx ).

INLINE DECLARATION

//const long MINUTE_MIN = 10000;
//const long MINUTE_MAX = 122400;
//const long HOURS_MIN = 1841400;
//const long HOURS_MAX = 22096800;
int potMin = 0;    // potentiometer minute time
int potHours = 1;    // potentiometer hours time
int ledMin = 12;     // select the pin for the minute LED
int ledHours = 11;     // select the pin for the hours LED
int Minute = 0;    // variable to store the value coming from the sensor
int Hours = 0;    // variable to store the value coming from the sensor


void setup() {
  pinMode(ledMin, OUTPUT);                // declare the ledPin as an OUTPUT
  pinMode(ledHours, OUTPUT);             // declare the ledPin as an OUTPUT
  Serial.begin(9600);
 
}

void loop() {
  Minute = analogRead(potMin);            // read the value from the Minute  sensor
  Hours = analogRead(potHours);        // read the value from the Hours sensor
 
  Serial.print("Min sensor = ");          // print the value from the Minute sensor
  Serial.print(Minute);  
  Serial.print("\t Time in min = ");      // print the convertion in minuts       
  Minute = map(Minute, 0, 1023, 10000L, 122400L);
  //Minute = map( Minute, 0, 1023, MINUTE_MIN, MINUTE_MAX);  // 2 minut max so 0 will be 10 secound and 1020 will be 2 min .
  Serial.println(Minute);           
  
  Serial.print("Hours sensor = ");           // print the value from the Hours sensor 
  Serial.print(Hours);  
  Serial.print("\tTime in hours = ");           // print the convertion in hours
  //Hours = map( Hours, 0, 1023, HOURS_MIN, HOURS_MAX);
  Hours = map( Hours, 0, 1023, 1841400L, 22096800L);
  Serial.println(Hours);           // 1841400 is equal to 30 minutes and 22096800 is equal to 6 hours.
  Serial.println(" ");


   digitalWrite(ledMin, LOW);          // turn the led min  ON
   digitalWrite(ledHours, HIGH);          // turn the led hours OFF
   delay(4000);  

   digitalWrite(ledMin, HIGH);         // turn the led min OFF
   digitalWrite(ledHours, LOW);         // turn the led hours ON
   delay(4000);              
}

DECLAIRE IN THE SCOPE

const long MINUTE_MIN = 10000;
const long MINUTE_MAX = 122400;
const long HOURS_MIN = 1841400;
const long HOURS_MAX = 22096800;
int potMin = 0;    // potentiometer minute time
int potHours = 1;    // potentiometer hours time
int ledMin = 12;     // select the pin for the minute LED
int ledHours = 11;     // select the pin for the hours LED
int Minute = 0;    // variable to store the value coming from the sensor
int Hours = 0;    // variable to store the value coming from the sensor


void setup() {
  pinMode(ledMin, OUTPUT);                // declare the ledPin as an OUTPUT
  pinMode(ledHours, OUTPUT);             // declare the ledPin as an OUTPUT
  Serial.begin(9600);
 
}

void loop() {
  Minute = analogRead(potMin);            // read the value from the Minute  sensor
  Hours = analogRead(potHours);        // read the value from the Hours sensor
 
  Serial.print("Min sensor = ");          // print the value from the Minute sensor
  Serial.print(Minute);  
  Serial.print("\t Time in min = ");      // print the convertion in minuts       
  //Minute = map(Minute, 0, 1023, 10000L, 122400L);
  Minute = map( Minute, 0, 1023, MINUTE_MIN, MINUTE_MAX);  // 2 minut max so 0 will be 10 secound and 1020 will be 2 min .
  Serial.println(Minute);           
  
  Serial.print("Hours sensor = ");           // print the value from the Hours sensor 
  Serial.print(Hours);  
  Serial.print("\tTime in hours = ");           // print the convertion in hours
  Hours = map( Hours, 0, 1023, HOURS_MIN, HOURS_MAX);
  //Hours = map( Hours, 0, 1023, 1841400L, 22096800L);
  Serial.println(Hours);           // 1841400 is equal to 30 minutes and 22096800 is equal to 6 hours.
  Serial.println(" ");


   digitalWrite(ledMin, LOW);          // turn the led min  ON
   digitalWrite(ledHours, HIGH);          // turn the led hours OFF
   delay(4000);  

   digitalWrite(ledMin, HIGH);         // turn the led min OFF
   digitalWrite(ledHours, LOW);         // turn the led hours ON
   delay(4000);              
}

Instead of:

int Minute = 0;    // variable to store the value coming from the sensor
int Hours = 0;    // variable to store the value coming from the sensor

Try:

long Minute = 0;    // variable to store the value coming from the sensor
long Hours = 0;    // variable to store the value coming from the sensor

Thanks Outsider

It did almost fix it totally but the HOURS_MAX = 22,096,800 is still bad.

And if you read ''long int''

It should go up to 2,147,483,647.

I posted a Print-screen whit the result.

In the mean time, I did program the timer with a different syntax.

int potMin = 0;    // potentiometer minute time
int potHours = 1;    // potentiometer hours time
int ledMin = 12;     // select the pin for the minute LED
int ledHours = 11;     // select the pin for the hours LED
int MinuteVal = 0;    // variable to store the value coming from the sensor
int HoursVal = 0;    // variable to store the value coming from the sensor

void setup() {
  pinMode(ledMin, OUTPUT);                // declare the ledPin as an OUTPUT
  pinMode(ledHours, OUTPUT);             // declare the ledPin as an OUTPUT
  Serial.begin(9600);
}
void loop() {
  MinuteVal = analogRead(potMin);            // read the value from the Minute  sensor
  HoursVal = analogRead(potHours);        // read the value from the Hours sensor

   digitalWrite(ledMin, LOW);                           // turn the led min  ON
   digitalWrite(ledHours, HIGH);                        // turn the led hours OFF   
   if (MinuteVal <= 85 ) delay ( 1000*10 );             // 85 is the MinuteVal coresponding to 10 sec
   if ( MinuteVal > 85 ) delay (MinuteVal*60UL*2UL);    // max 2 min

   digitalWrite(ledMin, HIGH);                          // turn the led min OFF
   digitalWrite(ledHours, LOW);                         // turn the led hours ON
   if (HoursVal <= 85 ) delay ( 1000*60*30 );           //  85 is the HoursVal coresponding to 30 min
   if (HoursVal > 85 ) delay(HoursVal*60UL*60UL*6UL);   // max 6 hours       
}