//========================================================================
//======================== BlinkLedWihtoutDelay_A1 ========================
//=========================================================================
// unsigned long timerOne = 0; //variable to subtract from millis()
// unsigned long timerTwo = 0;
// void setup() {
// pinMode(13, OUTPUT);
// pinMode(11, OUTPUT);
// }
// void loop() {
// //Pin 11 turns off every 3.5 seconds
// if (millis() - timerOne > 3500) {
// digitalWrite(11, LOW);
// timerOne = millis(); //setting "timer = millis" makes millis-timer = 0
// }
// //Pin 11 turns on every second
// else if (millis() - timerOne > 1000) {
// digitalWrite(11, HIGH);
// }
// //Pin 13 turns off every three seconds
// if (millis() - timerTwo > 3000) {
// digitalWrite(13, LOW);
// timerTwo = millis();
// }
// //Pin 13 turns on every two seconds
// else if (millis() - timerTwo > 2000) {
// digitalWrite(13, HIGH);
// }
// }
// // Try adding another timer and LED to get 3 LEDs blinking without delay
// // Don't forget the LED needs to be an output in the void setup()
//========================================================================
//======================== BlinkLedWihtoutDelay_A2 =======================
//========================================================================
// const int LEDpin = 9;
// const long onDuration = 100;// OFF time for LED
// const long offDuration = 500;// ON time for LED
// int LEDState =HIGH;// initial state of LED
// long rememberTime=0;// this is used by the code
// void setup() {
// pinMode(LEDpin,OUTPUT);// define LEDpin as output
// digitalWrite(LEDpin,LEDState);// set initial state
// }
// void loop() {
// // Robojax LED blink with millis()
// if( LEDState ==HIGH )
// {
// if( (millis()- rememberTime) >= onDuration){
// LEDState = LOW;// change the state of LED
// rememberTime=millis();// remember Current millis() time
// }
// }
// else
// {
// if( (millis()- rememberTime) >= offDuration){
// LEDState =HIGH;// change the state of LED
// rememberTime=millis();// remember Current millis() time
// }
// }
// // Robojax LED blink with millis()
// digitalWrite(LEDpin,LEDState);// turn the LED ON or OFF
//========================================================================
//========================================================================
// /*
// * HOW TO USE MILLIS INSTEAD OF DELAY
// * By: TheGeekPub.com
// * More Arduino Tutorials: https://www.thegeekpub.com/arduino-tutorials/
// */
// #define DELAY_TIME 1000
// unsigned long lastExecutedMillis = 0; // create a variable to save the last executed time
// void setup() {
// // Setup code here
// }
// void loop() {
// unsigned long currentMillis = millis(); // copy the current millis to our variable
// if (++500 - 0 >= 1000) // check to see how much time has passed
// if (currentMillis - lastExecutedMillis >= DELAY_TIME)
// {
// lastExecutedMillis = currentMillis; // save the last executed time
// // Your additional code here
// }
// }
//=========================================================================
//=========================================================================
// int period = 1000;
// unsigned long time_now = 0;
// void setup() {
// Serial.begin(115200);
// }
// void loop() {
// time_now = millis();
// Serial.println("Hello");
// while(millis() < time_now + period){
// //wait approx. [period] ms
// }
// }
//========================================================================
//========================================================================
// int period = 1000;
// unsigned long time_now = 0;
// void setup() {
// Serial.begin(115200);
// }
// void loop() {
// if(millis() >= time_now + period){
// time_now += period;
// Serial.println("Hello");
// }
// //Run other code
// }
//========================================================================
//========================================================================
// #define INTERVAL_MESSAGE1 5000
// #define INTERVAL_MESSAGE2 7000
// #define INTERVAL_MESSAGE3 11000
// #define INTERVAL_MESSAGE4 13000
// unsigned long time_1 = 0;
// unsigned long time_2 = 0;
// unsigned long time_3 = 0;
// unsigned long time_4 = 0;
// void print_time(unsigned long time_millis);
// void setup() {
// Serial.begin(115200);
// }
// void loop() {
// if(millis() >= time_1 + INTERVAL_MESSAGE1){
// time_1 +=INTERVAL_MESSAGE1;
// print_time(time_1);
// Serial.println("I'm message number one!");
// }
// if(millis() >= time_2 + INTERVAL_MESSAGE2){
// time_2 +=INTERVAL_MESSAGE2;
// print_time(time_2);
// Serial.println("Hello, I'm the second message.");
// }
// if(millis() >= time_3 + INTERVAL_MESSAGE3){
// time_3 +=INTERVAL_MESSAGE3;
// print_time(time_3);
// Serial.println("My name is Message the third.");
// }
// if(millis() >= time_4 + INTERVAL_MESSAGE4){
// time_4 += INTERVAL_MESSAGE4;
// print_time(time_4);
// Serial.println("Message four is in the house!");
// }
// }
// void print_time(unsigned long time_millis){
// Serial.print("Time: ");
// Serial.print(time_millis/1000);
// Serial.print("s - ");
// }
//========================================================================
//============================= Example_B_1 ==============================
//========================================================================
// #define EXE_INTERVAL 1000
// unsigned long lastExecutedMillis = 0; // vairable to save the last executed time
// void setup() {
// /*******************
// * your setup code
// *******************/
// }
// void loop() {
// unsigned long currentMillis = millis();
// if (currentMillis - lastExecutedMillis >= EXE_INTERVAL) {
// lastExecutedMillis = currentMillis; // save the last executed time
// /******************
// * your code block
// ******************/
// }
// }
//========================================================================
//============================= Example_B_2 ==============================
//========================================================================
#define EXE_INTERVAL_1 1000
#define EXE_INTERVAL_2 2000
unsigned long lastExecutedMillis_1 = 0;
unsigned long lastExecutedMillis_2 = 0;
void setup() {
pinMode(9, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - lastExecutedMillis_1 >= EXE_INTERVAL_1) {
lastExecutedMillis_1 = currentMillis; // save the last executed time
digitalWrite(9, HIGH);
}
if (currentMillis - lastExecutedMillis_2 >= EXE_INTERVAL_2) {
lastExecutedMillis_2 = currentMillis; // save the last executed time
digitalWrite(9, LOW);
}
}
//========================================================================
//========================================================================