MIllis , int not refresh value

Hi everybody i have problem with code .

int val1=1000;
int val2=1000;

const byte ledPins[] = {12, 13};
unsigned long periods[] = {val1, val2};
byte index = 0;
unsigned long startTime;
unsigned long currentTime;

void setup()
{
  pinMode(ledPins[0], OUTPUT);
  pinMode(ledPins[1], OUTPUT);
}

void loop()
{
  currentTime = millis();
  if (currentTime - startTime >= periods[index])
  {
    digitalWrite(ledPins[index], LOW);
    index++;
    index = index % 2;
    digitalWrite(ledPins[index], HIGH);
    startTime = currentTime;
  }
}

This is part of my code responsible for diode lighting, "int" values ​​change on the LCD menu .

When I check the value " int " on the serial monitor everything changes correctly, but it looks like they are not loaded into periods[] they stay with the value that was at the start of the program.

Does any smarter man have any solution ?
Regards

"This is part of my code responsible for diode lighting, "int" values ​​change on the LCD menu .
When I check the value " int " on the serial monitor everything changes correctly,"

This is part of my code responsible for diode lighting, "int" values ​​change on the LCD menu .
When I check the value " int " on the serial monitor everything changes correctly,"

There is no serial activity nor is there an LCD in your posted code.

Nothing in that code ever changes any variables except index and certainly never changes any of the elements of periods[]. That's why the values you get out of that array never change.

If you have some other code that does something useful perhaps you should post that.

Steve

[Corrected]
Mah... It should work...
Instead of currentTime you could use millis() directly.

Datman:
Mah... It should work...
Please consider that I/O 0 and I/O 1 are Rx and Tx! There could be interference between LED state and serial monitor.
Instead of currentTime you could use millis() directly.

Where do you see io 0 and io 1?

Ooops... :slight_smile:

I could send the whole code, but it would bring unnecessary confusion.
I have prepared a cod that shows my problem. the value in int changes, but is not loaded and changes in "periods"

int val1=1000;
int val2=1000;

const byte ledPins[] = {12, 13};
unsigned long periods[] = {val1, val2};
byte index = 0;
unsigned long startTime;
unsigned long currentTime;



 void setup()
 {      pinMode(ledPins[0], OUTPUT);
  pinMode(ledPins[1], OUTPUT);
     Serial.begin(9600); // baud rate
    Serial.flush();
 }
 
 void loop()
 {
     String input = "";
 
     // Read any serial input
     while (Serial.available() > 0)
    {
        input += (char) Serial.read(); // Read in one char at a time
         //delay(5); // Delay for 5 ms so the next char has time to be received
     }
 
     if (input == "+")
     {val2=3000;
       
     }
     else if (input == "-")
     {
        val2=200;
     }
 currentTime = millis();
  if (currentTime - startTime >= periods[index])
  {
    digitalWrite(ledPins[index], LOW);
    index++;
    index = index % 2;
    digitalWrite(ledPins[index], HIGH);
    startTime = currentTime;
  }

    Serial.println(val1);
 }

When I check the value " int " on the serial monitor everything changes correctly, but it looks like they are not loaded into periods[] they stay with the value that was at the start of the program.

Just because the periods[] array is initialised with values from variables does not mean that if you change the values of the variables that the values stored in the array will change.

You need to explicitly change the values in the array from user input in order to change the LED periods.

UKHeliBob:
You need to explicitly change the values in the array from user input in order to change the LED periods.

Since you're here, and I'm using your code, could you help? :wink:

What do you need help with?

Get rid of val1 and val2 COMPLETELY.

Initialize the array with hardcoded values.

That way you won't be thinking that periods is somehow bound to the values in val1 and val2. It most certainly is not.

Since you're here, and I'm using your code, could you help? :wink:

It was, indeed, my code originally and it did exactly what you asked. Then you changed the scope of what you wanted to do, hence my suggestion to post here rather than PM me again.

As has been said, you need to change the values in the array based on user input. So, in your program instead of

    if (input == "+")
     {val2=3000;

etc change the value in the array instead. If you do not know how to do it then you will learn something from a little research.

As Paul said, there is no reason to have the val1 and val2 variables in the program.

unfortunately in my target code the variable "delay time" is read from int (lcd encoder)

kamil_szczepan:
unfortunately in my target code the variable "delay time" is read from int (lcd encoder)

I have no idea what that means. If you got the code you posted in reply #8 it would be a start. As has been said before, change the value in the periods array instead of changing the value of val2. Once that works you can apply the principle to your target program. It does not matter where the timing variable comes from, it just needs to be put in the right place in the periods array

Ok, thank you guys for your clues.
I solved the problem in a trivial way, Thank you for the time .

int val1=1000;
int val2=2000;
const byte ledPins[] = {11, 12};
byte index = 0;
unsigned long startTime;
unsigned long currentTime;
int variable;
void setup()
{
  pinMode(ledPins[0], OUTPUT);
  pinMode(ledPins[1], OUTPUT);
}

void loop()
{
    currentTime = millis();
  if (currentTime - startTime >= variable )
  {
    digitalWrite(ledPins[index], LOW);
    index++;
    index = index % 2;
    digitalWrite(ledPins[index], HIGH);
    startTime = currentTime;
  }

if (index == 0){
  variable = val1;
}else{
  variable = val2;
}
}

I solved the problem in a trivial way

What have you solved ?

Your original requirement was to turn 2 LEDs on sequentially with different timing periods. The current code does not do that. Have your requirements changed again ?

I feel like I am chasing shadows here.

This started for me with a PM

Turning on and off is simple, I want to turn on one led, control how long it should be on. if after it goes out, turn on another with another time and something does not work out.

when I used the delay it looked like this.

digitalWrite(focus_pin, HIGH);
delay(focus);
digitalWrite(focus_pin, LOW);
digitalWrite(shooter_pin, HIGH);
delay(shooter);
digitalWrite(shooter_pin, LOW);

I took pity on him/her and wrote more of less what was in the original post although my version had fixed periods, no val1 and val2 variables and the periods array was a const as it did not need to change.

After a few PMs where the requirements changed I suggested that he/she post here but I am no clearer as to what is required.