Problem while adding numbers

I don’t know what is happening. I paste all my code to see if you can see what I am doing wrong.

I am trying to make a program to make time lapse with my nikon camera. And I added a lcdkeypad to my arduino uno to select time between photos.

Maybe I have some mistaked related to that part, I haven’t tested yet due to my problem but I don’t mind that for the moment.

The problem is that if I add +1 or substract -1 to the seconds value (+1000 or -1000) it works fine. but if I try +5000 or -5000, when it reach 30 secs or -30 secs, it changes the sign. If the number was +30, the next value will be -25 and so on…

// include the library code:
#include <LiquidCrystal.h>

int seconds;
long int select;
long int millisCount;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);


void setup() {
  // set up the LCD's number of columns and rows:
  Serial.begin(9600);
  pinMode(6, OUTPUT);
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.setCursor(0,0);
  lcd.print("TimeLapse");
  lcd.setCursor(0,1);
  //lcd.print("Time in secs:");
  seconds = 1;
  select = 0;
  millisCount = 0;
  lcd.print (seconds, DEC);
}

void loop() {
  int x;
  x = analogRead (0);
  if(select == 1){
	  lcd.setCursor(0,1);
	  if (x < 60) { //Right
		lcd.print ("          ");
		lcd.setCursor(0,1);
		seconds = seconds + 1000;
		lcd.print (seconds/1000);
		
	  }
	  else if (x < 200) { //UP
		lcd.print ("          ");
		lcd.setCursor(0,1);
		seconds = seconds + 5000;
		lcd.print (seconds/1000);
                Serial.println(sec);		  
	  }
	  else if (x < 400){ //Down
		lcd.print ("          ");
		lcd.setCursor(0,1);
		seconds = seconds - 5000;
		lcd.print (seconds/1000);
                Serial.println(seconds/1000);		
	  }
	  else if (x < 600){ //Left
		lcd.print ("          ");
		lcd.setCursor(0,1);  
		seconds = seconds - 1000;
		lcd.print (seconds/1000);
		
	  }
          if(millis() - millisCount > seconds){
            millisCount = millis();
            digitalWrite(6, HIGH);
            delay(200);
            digitalWrite(6, LOW);
          }
	}
  if (x < 800 && x > 600 && select != 1){
    lcd.setCursor(10,1);  
    lcd.print ("Select");
    select = 1;
  }else if(x < 800 && x > 600&& select == 1){
    lcd.setCursor(10,1);  
    lcd.print ("      ");
    select = 0;
  }
  delay(250);
}

Your seconds variable is declared as a signed int. As such the range of values that it can hold is from -32,768 to 32,767. So, after you add 1000 to its starting value of zero 30 times it is perilously close to its maximum positive value and will roll over to negative values after a couple of more positive button presses. Try printing the value of seconds on the serial monitor after it has been incremented and watch what happens.

Then change the data type to long and see what happens.

That wat, thanks.

I changed int to long int and now it works. So I should use the biggest possible if I pretend to use big numbers

You should use as big or a little bigger than you could need and do bounds checks on numbers that may in any way exceed bounds.
If timing milliseconds (even in the 1000’s) and pushing a button can subtract then don’t let the result be < 0 or more than too long. So if you are going to subtract 5000 from x, check first if x < 5000 and if so then x = 0 else subtract.

unsigned int can count 0 to 65535. You need how long at most?

Suriken: That wat, thanks.

I changed int to long int and now it works. So I should use the biggest possible if I pretend to use big numbers

Personally I would not add/subtract 1000 for each button press. Rather, add/subtract 1 and multiply the number by 1000 for the purposes of timing when needed.

UKHeliBob:

Suriken: That wat, thanks.

I changed int to long int and now it works. So I should use the biggest possible if I pretend to use big numbers

Personally I would not add/subtract 1000 for each button press. Rather, add/subtract 1 and multiply the number by 1000 for the purposes of timing when needed.

That depends on how many time checks per button press wouldn't it? There may be many many 1000's.

Sorry, I don't get what you mean.

Start with zero. Press the button 3 times and add 1000 each time and you get 3000 Press the button 3 times and add 1 each time. Multiply the result by 1000 and you get 3000

How many times does this run

          if(millis() - millisCount > seconds){
            millisCount = millis();
            digitalWrite(6, HIGH);
            delay(200);
            digitalWrite(6, LOW);
          }

between button presses to change the value of seconds?

Sorry but I still don't see what point you are making. Granted that the period variable (seconds) used in that snippet would need to be changed, but it would simply be seconds * 1000 to achieve the same effect as far as I can see, which is what I said when I proposed smaller increments.

You would rather multiply a value 100's to 1000's of times in the time it takes to press a button than to multiply once and use the same result all those times? If the user does not touch a button for a whole minute, the multiply will be required millions of times.

If I run a loop that uses constants, I want to combine operations and constants that won't change in the loop so as to not perform those operations over and over, I want to reduce the number.

Human is so slow and Arduino so fast that 1 change by the user will be used an immense number of times before the user even notices let alone thinks to begin motion to make another change. So why do over and over what can be done once per user action?

Thanks. I see what you are getting at now.