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.
"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.
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.
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.
Since you're here, and I'm using your code, could you help?
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.
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
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 ?
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.
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.