#include <Arduino.h>
#define led1 D0
#define led2 D4
#define led3 D7
#define led4 D8
int i,j;
int ledState = 0;
int ledpins[4] = { led1, led2, led3, led4 };
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
Serial.begin(9600);
}
void led_on(){
for(int i=0; i<=3; i++){
if (ledState == 0){
ledState = 1;
}
digitalWrite(ledpins[i], ledState);
Serial.println(digitalRead(ledpins[i]));
delay(200);
}
}
void led_off (){
for(int j=0; j<=3; j++) {
if (ledState == 1){
ledState = 0;
}
digitalWrite(ledpins[j], ledState);
Serial.println(digitalRead(ledpins[j]));
delay(200);
}
}
void loop() {
led_on();
led_off();
}
What do you expect to gain by replacing delay in this code?
It seems to me that delay is what you want here.
Normally people want to use millis() to achieve some sort of multitasking so tasks can run asynchronously. Here you just want blocking code like delay is. There is no point in writing delay with millis() as it will still be blocking code.
You seem to have four LEDs but are only driving three of them.
Sir/Mam, I am a new learner, I just want to replace the delay with millis to improve my coding knowledge, help me.
It will not improve your coding knowledge but if you want to do it then this is how you go about it. This defines a function called millisDelay..
void millisDelay( long int delayTime){
long int start_time = millis();
while ( millis() - start_time < delayTime) ;
}
I hope I get good marks for your homework.
We hope you get a good job when you graduate.
Look at File -> Examples -> 02.Digital -> BlinkWithoutDelay. That will show you how to do 'something' at regular intervals. In your case, you want to do 'something' every 200 milliseconds.
The 'something' you want to do changes each time. The first four times through you want to turn on an LED. The next four times through you want to turn off an LED. Then you want to start over. You can use a variable to keep track of what 'something' you need to do. If 'counter' is less than 4, turn on LED[counter]. If 'counter' is >= 4, turn off LED[counter-4]. Increment 'counter'. If 'counter' > 7, set 'counter' to 0.
int ledState = 0;
int ledpins[4] = {2, 3, 4, 5}; //digital pins 2,3,4 & 5 on Nano
unsigned long previousMillis = 0;
unsigned long interval = 200;
void setup() {
pinMode(ledpins[0], OUTPUT); //digital pins 2
pinMode(ledpins[1], OUTPUT); //digital pins 3
pinMode(ledpins[2], OUTPUT); //digital pins 4
pinMode(ledpins[3], OUTPUT); //digital pins 5
Serial.begin(9600);
}
void led_change_state(){
for(int i=0; i<=3; i++){
digitalWrite(ledpins[i], ledState);
Serial.println(digitalRead(ledpins[i]));
}
}
void loop() {
if (millis() - previousMillis >= interval) {
previousMillis = millis();
if (ledState == 0){
ledState = 1;
}
else{
ledState = 0;
}
led_change_state();
}
}
The millis() variable is a non-stop millisecond counter that runs at boot of Arduino
Within void loop();, we are constantly testing the "if (millis() - previousMillis > interval) {" statement.
The interval is equal to 200 milliseconds
At boot of the Arduino, once the variable millis becomes "200" (which means 200ms have elapsed since the boot of the arduino), the if statement becomes true, since;
200 - 0 = 200
When the if statement becomes true, we make previousMillis = millis(), which is 200. We also change the LED state within this if statement.
the if statement will become true again in 200ms, as Millis will now equal 400, while previous millis is 200;
millis() - previousMillis = 400 - 200 = 200.
I made the same mistake initially. The delay(200) is INSIDE the loop so the LEDs come on and go off at 200-millisecond intervals. Your code (and my initial guess) has all the LEDs turn on at the same time and turn off at the same time.
Just replace your signle delay(200); line with the following multi lines: (It is not helping to improve your coding knowledge; it is helping you to get familiar with the use of millis() function for an unproductive task.)
unsigned long prMillis = millis();
while(millis() - prMillis < 200)
{
;
}
When uploading of a sketch is finished into the UNO, a 32-bit counter (you may call it millisCounter) is allocated within the RAM area of the MCU with initial value of 0. The millisCounter is advanced by one for the elapse of every one milli second time. The execution of millis() function brings to you the current time (in miili seconds) being hold by the the millisCounter.
Oops. Yes we both assumed the same thing. OP should be able to figure it out himself with the resources provided, but here's an untested solution anyway because I have nothing better to do
int ledState = 0;
int LEDcounter = 0;
int ledpins[4] = {2, 3, 4, 5}; //digital pins 2,3,4 & 5 on Nano
unsigned long previousMillis = 0;
unsigned long interval = 200;
void setup() {
pinMode(ledpins[0], OUTPUT);
pinMode(ledpins[1], OUTPUT);
pinMode(ledpins[2], OUTPUT);
pinMode(ledpins[3], OUTPUT);
Serial.begin(9600);
}
void led_turn_on(){
digitalWrite(ledpins[LEDcounter], ledState);
ledState = 1;
}
void led_turn_off(){
digitalWrite(ledpins[LEDcounter], ledState);
ledState = 0;
LEDcounter++; //go to next LED
}
void loop() {
if (millis() - previousMillis >= interval) {
previousMillis = millis();
if (ledState == 0){
led_turn_on();
}
else{
led_turn_off();
}
if(LEDcounter > 3){
LEDcounter = 0;
}
}
}
To add to what @GolamMostafa is saying;
Using this;
void loop(){
unsigned long prMillis = millis();
while(millis() - prMillis < 200)
{
;
}
}
is the EXACT same thing as delay(200);, as it is a "while loop" (with no code executed inside), which halts any other task from being performed for as long as millis() minus prMillis, is less than 200ms.
It is good practice to use a while loop to understand how a delay() works
whereas this;
void loop(){
if(millis() - prMillis < 200)
{
prMillis = millis();
}
}
is an if statement, which allows you to perform a specific function every 200ms, while also being able to perform other tasks within the main loop simultaneously.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.