can someone add millis to this code for me please?

hi, so i can't figure how to use millis, and I don't really have the time to right now because this is a school project and I need it done, I just need someone to please help me by changing my delays to millis so pumping_drink and pump_relay2 will run at the same time, thanks! i will try to learn this after i just dont have the time right now, my code works but because of me using delay it wont work properly.

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9           
#define SS_PIN          10          
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

MFRC522 mfrc522(SS_PIN, RST_PIN);   

String read_rfid;                   
String ok_rfid_1="a070dba4";        
String ok_rfid_2="be31dbd9";        
int lock = 7;                       
int lock2 = 7;                      

void setup() {
    Serial.begin(9600);         
    while (!Serial);            
    SPI.begin();                
    mfrc522.PCD_Init();         
    
    display.begin(SSD1306_SWITCHCAPVCC, 0x3C); 
 
    display.display();
    delay(2000);

    display.clearDisplay();
    display.setTextSize(1);
    display.setCursor(0, 0);
    display.setTextColor(WHITE);
    
    pinMode(lock, OUTPUT);
    pinMode(lock2, OUTPUT);
}

void dump_byte_array(byte *buffer, byte bufferSize) {
    read_rfid="";
    for (byte i = 0; i < bufferSize; i++) {
        read_rfid=read_rfid + String(buffer[i], HEX);
    }
}

void pump_relay() {
  
  digitalWrite(lock,HIGH);
  delay(500);
  digitalWrite(lock,LOW);
}

void pumping_drink() {
    display.setCursor(0, 0);
  display.print("pumping");
  display.display();
  delay(1000);
  display.clearDisplay();
    display.setCursor(0, 0);
  display.print("pumping.");
  display.display();
  delay(1000);
  display.clearDisplay();
    display.setCursor(0, 0);
  display.print("pumping..");
  display.display();
  delay(1000);
  display.clearDisplay();
    display.setCursor(0, 0);
  display.print("pumping...");
  display.display();
  delay(1000);
  display.clearDisplay();
    display.setCursor(0, 0);
  display.print("pumping");
  display.display();
  delay(1000);
  display.clearDisplay();
    display.setCursor(0, 0);
  display.print("pumping.");
  display.display();
  delay(1000);
  display.clearDisplay();
    display.setCursor(0, 0);
  display.print("Done!");
  display.display();
  delay(1000);
  display.clearDisplay();
}


void pump_relay2() {          
  digitalWrite(lock2, HIGH);
  delay(5000);
  digitalWrite(lock2,LOW);
}


void loop() {

  
    if ( ! mfrc522.PICC_IsNewCardPresent())
        return;

    
    if ( ! mfrc522.PICC_ReadCardSerial())
        return;

    dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
    Serial.println(read_rfid);
    if (read_rfid==ok_rfid_1) {
      
      pump_relay2();
      pumping_drink();
    }

    Serial.println(read_rfid);
    if (read_rfid==ok_rfid_2) {
     
      pump_relay();
    }

}

Thanks! :slight_smile:

You should be working on doing that for yourself as I predict it will be quite a wait for someone, here, to do it for you. We will gladly help you if you make an effort.

Here is a starting point.
This might help, too.

Give it a try. If it does not work, post the code with a description of what the code actually does and how that differs from what you want to do.

Read the how to use this forum sticky.

Can I call the school and let them know I helped you?

"Won't work properly" is not a specification. What does the code currently do? What do you want it to do different?

IcyBlade:
i can't figure how to use millis

What exactly is the stumbling block with millis() ?

Sounds like a job for Blackfin.

groundFungus:
I predict it will be quite a wait for someone, here, to do it for you.

Perhaps s/he hasn't read this thread yet?

But on a more serious note, having had a quick squiz through the code, it looks to me more like a complete re-do of the approach, not a simple "add millis to this code". (edit: although having said that, I suppose a switch from delay() to millis(), is pretty much by definition a change in approach.)

millis
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9           
#define SS_PIN          10          
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

MFRC522 mfrc522(SS_PIN, RST_PIN);   

String read_rfid;                   
String ok_rfid_1="a070dba4";        
String ok_rfid_2="be31dbd9";        
int lock = 7;                       
int lock2 = 7;                      

void setup() {
    Serial.begin(9600);         
    while (!Serial);            
    SPI.begin();                
    mfrc522.PCD_Init();         
    
    display.begin(SSD1306_SWITCHCAPVCC, 0x3C); 
 
    display.display();
    delay(2000);

    display.clearDisplay();
    display.setTextSize(1);
    display.setCursor(0, 0);
    display.setTextColor(WHITE);
    
    pinMode(lock, OUTPUT);
    pinMode(lock2, OUTPUT);
}

void dump_byte_array(byte *buffer, byte bufferSize) {
    read_rfid="";
    for (byte i = 0; i < bufferSize; i++) {
        read_rfid=read_rfid + String(buffer[i], HEX);
    }
}

void pump_relay() {
  
  digitalWrite(lock,HIGH);
  delay(500);
  digitalWrite(lock,LOW);
}

void pumping_drink() {
    display.setCursor(0, 0);
  display.print("pumping");
  display.display();
  delay(1000);
  display.clearDisplay();
    display.setCursor(0, 0);
  display.print("pumping.");
  display.display();
  delay(1000);
  display.clearDisplay();
    display.setCursor(0, 0);
  display.print("pumping..");
  display.display();
  delay(1000);
  display.clearDisplay();
    display.setCursor(0, 0);
  display.print("pumping...");
  display.display();
  delay(1000);
  display.clearDisplay();
    display.setCursor(0, 0);
  display.print("pumping");
  display.display();
  delay(1000);
  display.clearDisplay();
    display.setCursor(0, 0);
  display.print("pumping.");
  display.display();
  delay(1000);
  display.clearDisplay();
    display.setCursor(0, 0);
  display.print("Done!");
  display.display();
  delay(1000);
  display.clearDisplay();
}


void pump_relay2() {          
  digitalWrite(lock2, HIGH);
  delay(5000);
  digitalWrite(lock2,LOW);
}


void loop() {

  
    if ( ! mfrc522.PICC_IsNewCardPresent())
        return;

    
    if ( ! mfrc522.PICC_ReadCardSerial())
        return;

    dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
    Serial.println(read_rfid);
    if (read_rfid==ok_rfid_1) {
      
      pump_relay2();
      pumping_drink();
    }

    Serial.println(read_rfid);
    if (read_rfid==ok_rfid_2) {
     
      pump_relay();
    }

}

There you go. I added it right at the start so you can see it easily.

OK, jokes aside, as everyone else said the spirit of this forum is to help you out when you are stuck. Not to do people's work when they don't even try themselves.

To do things simultaneously you need a variable (variables that are compared to millis() should be of the type unsigned long) to record when something started. So in your case it looks like you need three variables to record the time at which pump1 was switched on, the time pump2 was switched on, and the timing for your printing function.

When you switch your pumps on, set the relevant variable to millis() and turn it on. Do this inside your if statements. This means you will have to remove the turning on part from the function. Inside the function you need to compare the current time (i.e. millis()) against your pump on time to see if it has exceeded your time limit.

The pump functions should then be called within loop, and not within the if statements. What the functions will now do (if you have written them correctly) is to switch the pump off when you have exceeded your desired time.

I'll leave it up to you to imagine how you do the printing one.