Hi i solved many problems with millis but can you find some solution to this :
I want my sketch to start blinking leds for 20 seconds with a button1 press or serial monitor command "a" and after another button2 press or serial command "b" i want to run it for 30 seconds. I used millis
but it does not do what i wanted my code is below:
const int ledPin1 = 10;
const int ledPin2 = 11;
bool running = false;
int Received = 0;
int count = 0;
unsigned long startMillis2 = 0;
unsigned long startMillis3 = 0;
unsigned long startMillis = 0; //some global variables available anywhere in the program
unsigned long currentMillis = 0;
unsigned long period = 3000; // number of millisecs that Motor is OFF
unsigned long Interval_a = 20000;
unsigned long Interval_b = 30000;
//------- VARIABLES (will change)
byte led1_State = LOW; // used to record whether the LEDs are on or off
byte led2_State = LOW; // LOW = off
void setup() {
Serial.begin(9600);
//------- set the Led pins as output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
startMillis = millis(); //initial start time
//=====
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
startMillis2 = millis();
startMillis3 = millis();
running = false;
}
}
void loop() {
if (Serial.available() > 0) {
Received = Serial.read();
}
if (running == true) {
//Serial.println("i am running");
currentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (currentMillis - startMillis >= period) //test whether the period has elapsed
{
count++; //increment a counter variable: 0 => 1 => 2 => 3
//When the counter reaches 4, reset back to 0.
if (count == 4) {
count = 0;
}
switch (count) {
case 0:
led1_State = HIGH;
period = 3000; // or whatever delay you want for this cycle
break;
case 1:
led2_State = LOW;
period = 2200; // 200ms added // or whatever delay you want for this cycle
break;
case 2:
led2_State = HIGH;
period = 3000; // or whatever delay you want for this cycle
break;
case 3:
led1_State = LOW;
period = 2200; // 200ms added // or whatever delay you want for this cycle
break;
}
digitalWrite(ledPin1, led1_State);
digitalWrite(ledPin2, led2_State);
startMillis = currentMillis;
}
}
else if (running == false) {
led1_State = HIGH;
led2_State = HIGH;
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
}
//=====(a)
if (Received == 'a') {
running = true;
// Serial.println("i am true");
startMillis2 = currentMillis;
}
if (currentMillis - startMillis2 >= Interval_a) // check if the Preset Time is over for this round.
{
running = false;
startMillis2 = currentMillis;
}
//=====(a)
//=====(b)
if (Received == 'b') {
running = true;
// Serial.println("i am true");
startMillis3 = currentMillis;
}
if (currentMillis - startMillis3 >= Interval_b) // check if the Preset Time is over for this round.
{
running = false;
startMillis3 = currentMillis;
}
}
Yep, this is the third new thread on basically the same program.
Just the OP slightly modifies the requirements each time
You do not need two of these when you only have one timer (just with the requirement of a different time)...
if (currentMillis - startMillisX >= Interval_X) // check if the Preset Time is over for this round.
{
running = false;
startMillisX = currentMillis;
}
Make Interval_X a variable.
When you receive an ‘a’ set Interval_X = 20000.
When you receive a ‘b’ set Interval_X = 30000.
Edit: In fact go back to the code in post #66 in your previous thread
There you already have variable ‘Interval’ which you can set accordingly depending on if you receive ‘a’ or ‘b’.
Add two “if” statements to check for ‘a’ and ‘b’
Inside each “if” set “running”, “startMillis2” and “Interval” appropriately
pcbbc:
Follow the steps in my edit to post #4.
The variable in your original sketch is called 'Interval'.
void setup() {
startMillis = millis()
startMillis2 = millis()
}
yes your suggestion worked I am happy
Now just a query i have declared a second timer startMillis2 = millis() in setup is this correct or I am ok with just startMillis = millis() there?!
And how can I assign an hour to variable Interval_X ?
younus claimed that he has solved "many problems" and now he is asking something like this???
but can you find some solution to this :
I want my sketch to start blinking leds for 20 seconds with a button1 press or serial monitor command "a" and after another button2 press or serial command "b" i want to run it for 30 seconds. I used millis
but it does not do what i wanted my code is below:
**"**but can you find some solution to this:"
to me this sounds like a new bait to see how many users can he can get up and posting code-examples DFTT
#include <ezOutput.h> // ezOutput library
ezOutput led1(10); // create ezOutput object that attach to pin 10;
ezOutput led2(11); // create ezOutput object that attach to pin 11;
char Received = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
led1.loop(); // MUST call the led.loop() function in loop()
led2.loop(); // MUST call the led.loop() function in loop()
if (Serial.available()) { // if there is data comming
Received = Serial.read();
if (Received == 'a')
led1.blink(500, 500, 0, 20000);
else if (Received == 'b')
led2.blink(500, 500, 0, 30000);
// function is void blink(unsigned long lowTime, unsigned long highTime, unsigned long startTime, long blinkTimes);
}
}
ezOutput led1(10); // create ezOutput object that attach to pin 10;
ezOutput led2(11); // create ezOutput object that attach to pin 11;
char Received = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
led1.loop(); // MUST call the led.loop() function in loop()
led2.loop(); // MUST call the led.loop() function in loop()
if (Serial.available()) { // if there is data comming
Received = Serial.read();
if (Received == 'a')
led1.blink(500, 500, 0, 20000);
else if (Received == 'b')
led2.blink(500, 500, 0, 30000);
// function is void blink(unsigned long lowTime, unsigned long highTime, unsigned long startTime, long blinkTimes);
}
}
Above code used this [ezOutput library](https://arduinogetstarted.com/tutorials/arduino-output-library)
Do not get confuse regarding my threads please it was my need and I am thanking you for all your replies in this new thread this was a need not just wastage of time. I thought that you would not reply if I would have started this new topic in previous thread.
so do stay responsive to me when i need!