Programming help for a declaired variable that generates a random number for some reason

Hello, I am wondering if anyone can help me with a program that I am trying to create for work? I am 95% finished but cannot get one function to work as I would like. From my general knowledge (limited) i have done the coding properly but for some reason one variable gives me random numbers even though I have a specific value declared. Any help or advice would be greatly appreciated, thank you.

Welcome to the forum

The problem is in the code that you have not posted. The problem sounds like it is caused by one variable writing over another. A classic cause of this is writing outside the bounds of an array which would be easy to diagnose if you had posted your sketch

This code is for commercial purposes? Great!* So what % of the profits will someone get if they help you?

* Not for me, I should add. My employer does not tolerate moonlighting.

If you want, we can move your topic to a forum section where you can offer payment for assistance.

I am not being paid for this project, I am building a timer circuit for my current employer that was mostly built, programmed and parts provided from my current employer. I am no where near experienced to do this and try and make a profit, I just like tech and when he couldnt find a timer in store to do what he wanted i said i would see what i can do.

Here is the sketch I created, my problem is button 3 is a manual select timer that should add 30 seconds and increase by an addition 30 seconds every time the button is pressed (10 in this sketch to make it run faster and i will change the time once its functioning properly). I hope that makes sense. But in the current sketch it will add 10 if i eneble ttime=mtime, but if i create a line that is ttime=ttime+30 (exactly what i want to do) it generates random numbers that are way larger than i want or need.

#include <LiquidCrystal.h>

//Buttons & LED's
int but1=11;
int but11;
int but2=12;
int but22;
int but3=3;
int but33;

int led1=8;
int led2=9;
int led3=10;
int relay1=0;
int buz1=1;

int timer;
int stime=3; ///short timer
int ltime=5; //long timer
int mtime=10; //manual timer
int ttime=0;
int tttime;
int j;
int a;

//Lcd
int rs=7;
int en=6;
int d4=5;
int d5=4;
int d6=13;
int d7=2;

LiquidCrystal lcd(rs,en,d4,d5,d6,d7);

void setup() {
// put your setup code here, to run once:

lcd.begin(16,2);
pinMode(but1, INPUT_PULLUP);
pinMode(but2, INPUT_PULLUP);
pinMode(but3, INPUT_PULLUP);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(relay1, OUTPUT);
pinMode(buz1, OUTPUT);

attachInterrupt(digitalPinToInterrupt(but3),timeradd,LOW);

}

void loop() {
// put your main code here, to run repeatedly:

ttime=0;

lcd.setCursor(0,0);
lcd.print("Countdown Timer");

lcd.setCursor(6,1);
lcd.print("Stop");

but11=digitalRead(but1);
if(but11==0){
ttime=stime;
digitalWrite(led1, HIGH);
}

but22=digitalRead(but2);
if(but22==0){
ttime=ltime;
digitalWrite(led2, HIGH);
}

//but33=digitalRead(but3);
//if(but33==0){
//ttime=mtime;
//digitalWrite(led3, HIGH);
//}

//j=ttime;

for(j=ttime;j>0;j=j-1){

digitalWrite(relay1, HIGH);
lcd.setCursor(0,0);
lcd.print("Countdown Timer");

lcd.setCursor(2,1);
lcd.print(ttime);

lcd.setCursor(6,1);
lcd.print("seconds");
delay(1000);

lcd.clear();
ttime=ttime-1;

//j=ttime;

}

digitalWrite(relay1, LOW);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);

}

void timeradd() {

//ttime=mtime;

digitalWrite(led3, HIGH);

//}

}

How did I know that you would not use code tags when posting your sketch ?

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Hello
Why the sketch has an interrupt service implemented?

Do the timer, timer setting, buttons and leds have a functional name too?

Where is the time coded ?

All variable changed within an ISR and used outside of it should be declared as volatile

In addition, when such variables are used outside of an ISR then if they are multi byte variables, such as an int, then interrupts should be disabled during the period that they are accessed to avoid one or more of the bytes that they are made up of being changed by the ISR part way through the operation

I note too that triggering of the ISR could occur multiple time from a single button press due to switch contact bounce

I believe I have the code tags entered now.

#include <LiquidCrystal.h>


//Buttons & LED's
int but1 = 11;
int but11;
int but2 = 12;
int but22;
int but3 = 3;
int but33;

int led1 = 8;
int led2 = 9;
int led3 = 10;
int relay1 = 0;
int buz1 = 1;

int timer;
int stime = 3;  ///short timer
int ltime = 5;  //long timer
int mtime = 10;   //manual timer
int ttime = 0;
int tttime;
int j;
int a;


//Lcd
int rs = 7;
int en = 6;
int d4 = 5;
int d5 = 4;
int d6 = 13;
int d7 = 2;



LiquidCrystal lcd(rs, en, d4, d5, d6, d7);




void setup() {
  // put your setup code here, to run once:


  lcd.begin(16, 2);
  pinMode(but1, INPUT_PULLUP);
  pinMode(but2, INPUT_PULLUP);
  pinMode(but3, INPUT_PULLUP);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(relay1, OUTPUT);
  pinMode(buz1, OUTPUT);

  attachInterrupt(digitalPinToInterrupt(but3), timeradd, LOW);




}

void loop() {
  // put your main code here, to run repeatedly:


  ttime = 0;

  lcd.setCursor(0, 0);
  lcd.print("Countdown Timer");

  lcd.setCursor(6, 1);
  lcd.print("Stop");



  but11 = digitalRead(but1);
  if (but11 == 0) {
    ttime = stime;
    digitalWrite(led1, HIGH);
  }



  but22 = digitalRead(but2);
  if (but22 == 0) {
    ttime = ltime;
    digitalWrite(led2, HIGH);
  }


  //but33=digitalRead(but3);
  //if(but33==0){
  //ttime=mtime;
  //digitalWrite(led3, HIGH);
  //}






  //j=ttime;


  for (j = ttime; j > 0; j = j - 1) {



    digitalWrite(relay1, HIGH);
    lcd.setCursor(0, 0);
    lcd.print("Countdown Timer");


    lcd.setCursor(2, 1);
    lcd.print(ttime);


    lcd.setCursor(6, 1);
    lcd.print("seconds");
    delay(1000);



    lcd.clear();
    ttime = ttime - 1;



    //j=ttime;

  }

  digitalWrite(relay1, LOW);
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);


}


void timeradd() {



  //ttime=mtime;




  digitalWrite(led3, HIGH);

  //}

}

There are organisations to help those who are victims of Modern Slavery in many countries. Where are you working?

Thank you very much Bob, I read your message and was able to figure out a couple things going wrong. I corrected those problems only to run into another because I was using a FOR loop, switched to a WHILE loop and got everything working as wanted, thank you again for the reply. I would also like to give a huge shout out to PaulRB, I called my prime minister and looked into labor laws and am now getting a nice settlement payout.