cant find a solution delay adjust with analogInput

Hi
i am working on a project
and im stopped a the last line of my code and canot find any solution

i want to control a relay board with 6 relay conected to digital 2 trough 7
and an analog pin A0 conected to a potentiometer 10k
everything is conected to an arduino promini 328

My problem is that every relay need to be cycled from one to the other each 24 hours
or in fact each 86400000 milisecond

if i set the delay directly at 86400000 millisecond everything work as wanted
but i need the delay to be adjustable with the potentiometer and now its my problem

i have try to map my delay directly into the delay(xxx); fuction
but everytime i am trying to do this the delay is gettin set to negative 30000millis through +30000

is there a limit of delay applied with a (map) function??

int chrg1 = 2;
int chrg2 = 3;
int chrg3 = 4;
int chrg4 = 5;
int chrg5 = 6;
int chrg6 = 7;
int led = 13;
int sensorPin = A3; 
int sensorValue = 0; 
int outputValue = 0;
int Map = 86400000;
void setup() {   

  Serial.begin(9600);   
  
  pinMode(chrg1, OUTPUT);  
  pinMode(chrg2, OUTPUT); 
  pinMode(chrg3, OUTPUT); 
  pinMode(chrg4, OUTPUT); 
  pinMode(chrg5, OUTPUT); 
  pinMode(chrg6, OUTPUT);
  pinMode(led, OUTPUT); 
  
}


void loop() {{
  sensorValue = analogRead(sensorPin);
  outputValue = map(sensorValue, 0, 1024, 0, 255);
  Map = map(outputValue, 0,1024, 0, 255);
  
  Serial.print("sensor = " );                       
  Serial.print(sensorValue);      
  Serial.print("\t output = ");      
  Serial.println(Map); 

}
   digitalWrite(led, HIGH);   
     delay(100);               
   digitalWrite(led, LOW);    
     delay(100);
   digitalWrite(led, HIGH);   
     delay(100); 
   digitalWrite(led, LOW);
     delay(100);  
  digitalWrite(chrg1, HIGH);   
     delay(Map);                                
   digitalWrite(chrg1, LOW);
     delay(50);   
  digitalWrite(chrg2, HIGH);   
     delay(Map);               
   digitalWrite(chrg2, LOW); 
     delay(50); 
  digitalWrite(chrg3, HIGH);   
     delay(Map);               
   digitalWrite(chrg3, LOW); 
     delay(50); 
  digitalWrite(chrg4, HIGH);   
     delay(Map);               
   digitalWrite(chrg4, LOW);
     delay(50);  
  digitalWrite(chrg5, HIGH);   
     delay(Map);               
   digitalWrite(chrg5, LOW);
     delay(50);  
  digitalWrite(chrg6, HIGH);   
     delay(Map);               
   digitalWrite(chrg6, LOW);     
     delay(50); 

}

thank you for your help
and sorry for my very bad english....
i am french canadian..

Never use delay() See blink withoutdelay.

Mark

86400000 won't fit into an int. It has to be a long int.

But if you are using it to store seconds in a day, why call it "Map"? Wouldn't "secsPerDay" or something like that be easier to understand? Also, you destroy the value later by using it on the left hand of an assignment.

Can you give me an example what it look like ....
im not usual with blinkwithout delay

holmes4:
Never use delay() See blink withoutdelay.

Mark

I disagree. It is not necessary when there is only a single task. Using delay() in this case is a reasonable solution. It's a good practice to hide unnecessary complexity (as the complexity of millis() is contained inside delay() ).

do you have a solution??

qpwerx:
do you have a solution??

To what? I gave you the answer. int datatypes are too small for your values.

to put a long int?

qpwerx:
to put a long int?

Yes. You should go look up the ranges for C datatypes. Don't just believe an internet forum. It might be wrong. :slight_smile:

i have added but now the maximum output is 1 750 000 milis.

.... i think its to much even with a 4 bytes stream on it...

You think? Well, exactly how big do you think 2^31 is?
log(1750000)/log(2) = 20.739 so it can be represented in 21 bits.

exactly but the number of millis is 86 400 000 millis
needed to be higher.. no??

qpwerx:
exactly but the number of millis is 86 400 000 millis
needed to be higher.. no??

log(86400000)/log(2) = 26.3645
No.
Anyway, it doesn't matter what value you use to initialize the variable if you assign a different value to it later, without using that value in any way. This is the second time I'm telling you.

Use unsigned long variables for timing. That allows for values up to 2^32. Your knowledge of physics is not sufficiently advanced to require negative time values.

Look at how timing is managed with millis() in several things at a time

...R

Robin2:
Use unsigned long variables for timing. That allows for values up to 2^32. Your knowledge of physics is not sufficiently advanced to require negative time values.

Look at how timing is managed with millis() in several things at a time

...R

Of course, I agree that unsigned long is preferable. But it won't make any difference to this program (if it uses long int), as long as the variable that is passed to the delay() function is less than or equal to 2^31-1. Everything inside the delay() function is done with unsigned long ints. The OP doesn't require millis() at this time, because there is only one task.

What do you think happens when you pass an int to delay()? It's signed! Do things break? No.

update .....

i have try the long and unsigned long and the timing is never more than 1750000 millis
everything is fine and smooth after a certain limit it start to fall into negative millis.....

i really dont know what can i do with it...

Well if you're certain what the time limit is, please tell us.

Try googling for a tutorial which explains "integer overflow". That will explain why adding large numbers together gives a negative result. You have almost certainly overflowed one of your variables somewhere.

I'm not sure what you're doing here:

 sensorValue = analogRead(sensorPin);
  outputValue = map(sensorValue, 0, 1024, 0, 255);
  Map = map(outputValue, 0,1024, 0, 255);

You read the analog pin, took it's 10 bit value and scaled it to an 8 bit value.
The very next line, you take the 8 bit value and said it was a 10 bit, and scaled back again.
This means your max pot value will be 64 (63?).

I'm not seeing anywhere you use the pot value to calculate anything - such as your delay time.

Using the data type that you should (long, unsigned long), just multiply the max pot value (1024) by some constant (x) to equal the number of milliseconds for your delay.

By the way, the BlinkWithoutDelay example is in the IDE under File>Examples>02.Digital>BlinkWithoutDelay. Using method in this example makes life easier in the long run on future projects.

Hi

thank you ih55t

i have found a solution that work very good whith your idea

for your question i did not have to assign a variable to a delay
and a delay is automaticaly an (unsigned long)

i am now getting my stuff to work very good with this solution!

const int chrg1 = 2;
const int chrg2 = 3;
const int chrg3 = 4;
const int chrg4 = 5;
const int chrg5 = 6;
const int chrg6 = 7;
const int led = 13;
int sensorPin = A3;
long int sensorValue = 0;
long int outputValue = 0;
long int Map = 0;
long int Map0 = 0;

void setup() {   

  Serial.begin(9600);   
  
  pinMode(chrg1, OUTPUT);  
  pinMode(chrg2, OUTPUT); 
  pinMode(chrg3, OUTPUT); 
  pinMode(chrg4, OUTPUT); 
  pinMode(chrg5, OUTPUT); 
  pinMode(chrg6, OUTPUT);
  pinMode(led, OUTPUT); 
  
}


void loop() {{
  sensorValue = analogRead(sensorPin);
  outputValue = map(sensorValue, 0, 1023, 0, 255);
  Map0 = outputValue * 338800;
  Map = Map0 + 1000;
 
  Serial.print("sensor = " );                       
  Serial.print(sensorValue);      
  Serial.print("\t output = ");      
  Serial.println(Map); 

}
   digitalWrite(led, HIGH);   
     delay(100);               
   digitalWrite(led, LOW);    
     delay(100);
   digitalWrite(led, HIGH);   
     delay(100); 
   digitalWrite(led, LOW);
     delay(100);  
  digitalWrite(chrg1, HIGH);   
     delay(Map);                                
   digitalWrite(chrg1, LOW);
     delay(50);   
  digitalWrite(chrg2, HIGH);   
     delay(Map);               
   digitalWrite(chrg2, LOW); 
     delay(50); 
  digitalWrite(chrg3, HIGH);   
     delay(Map);               
   digitalWrite(chrg3, LOW); 
     delay(50); 
  digitalWrite(chrg4, HIGH);   
     delay(Map);               
   digitalWrite(chrg4, LOW);
     delay(50);  
  digitalWrite(chrg5, HIGH);   
     delay(Map);               
   digitalWrite(chrg5, LOW);
     delay(50);  
  digitalWrite(chrg6, HIGH);   
     delay(Map);               
   digitalWrite(chrg6, LOW);     
     delay(50); 

}

i know that it could look like im a redneck of programing but this is it ......
assigned 3 different variable to the calculation 1: for the minimum of 1000millisecond
one to calculate the input variable and the last one to calculate the output to the delay function

it work great with no bug

Glad that helped out.

One last bit of advice. This bit of code here:

 sensorValue = analogRead(sensorPin);
  outputValue = map(sensorValue, 0, 1023, 0, 255);
  Map0 = outputValue * 338800;
  Map = Map0 + 1000;

By mapping the 10 bit pot value to the 8 bit value, you're limiting the pot resolution to 255 increments. If you leave it at the 10 bit value, it will then have 1023 increments - much finer increments.

This bit of code could replace the above code.

sensorValue = analogRead(sensorPin);
 Map = (sensorValue * 84452)+1000;

Don't worry about doing things like a redneck - everybody has to start somewhere.