How exchange Delay (); for Millis(); or timer countdown

Hi Can you someone help me,please ?

I made ino.files for looper in Kemper Amp.

I need send midi command with timing tempo 120 BMP / 8000 ms. I can do that with delay() but need use program during waiting time.

I need to exchange delay for millis or timer with countdown and then send midi commands. I tried with

unsigned long time = millis()+8000;

but does not work.

Thank you in advance

{       
          lcd.setCursor ( 0, 3 );  
          lcd.print("LOOPER :  RECORD +  ");
      
          midiOUT(0xB0,0x63,0x7D);// rec
          midiOUT(0xB0,0x62,0x58);
          midiOUT(0xB0,0x06,0x00);
          midiOUT(0xB0,0x26,0x01); 
          
         delay(8000);   // EXCHANGE THIS DELAY !!!!!!

     lcd.setCursor ( 0, 3 );                
     lcd.print("LOOPER :   PLAY >>> "); 
      
      midiOUT(0xB0,0x63,0x7D);//play
      midiOUT(0xB0,0x62,0x58);
      midiOUT(0xB0,0x06,0x00);
      midiOUT(0xB0,0x26,0x00);  
     
     
                
   }

ALL CODE

void button()
{
  buttonVal = digitalRead(button_1);     //read input value from button
  delay(10);                              //wait 10ms
  bounceCheck = digitalRead(button_1);   //check again
  if(buttonVal == bounceCheck){           //if val is the same then not a bounce
    if (buttonVal == HIGH && buttonState == 1) {   //check if the input is HIGH
      buttonState = 0;
      digitalWrite(ledPin_1_1,LOW);
      digitalWrite(ledPin_1_2,LOW);
      digitalWrite(ledPin_1_3,LOW);
    }
    if(buttonVal == LOW && buttonState == 0){                   
      buttonState = 1;
      mode_1++; 
      
    digitalWrite(ledPin_1_2,HIGH);
      if (mode_1 > 3) mode_1 = 2; 
  
      if (mode_1 == 1) 
         
     {
          
          lcd.setCursor ( 0, 3 );  
          lcd.print("LOOPER :  RECORD +  ");
      
          midiOUT(0xB0,0x63,0x7D);// rec
          midiOUT(0xB0,0x62,0x58);
          midiOUT(0xB0,0x06,0x00);
          midiOUT(0xB0,0x26,0x01); 
          
         delay(8000);  // EXCHANGE THIS DELAY !!!!


     lcd.setCursor ( 0, 3 );                
     lcd.print("LOOPER :   PLAY >>> "); 
      
      midiOUT(0xB0,0x63,0x7D);//play
      midiOUT(0xB0,0x62,0x58);
      midiOUT(0xB0,0x06,0x00);
      midiOUT(0xB0,0x26,0x00);  
                
   }
   else if (mode_1 == 2)
          
 {  
    
         
       lcd.setCursor ( 0, 3 );                
          lcd.print("LOOPER : OVERBUB+++ ");
         
          midiOUT(0xB0,0x63,0x7D);// rec
          midiOUT(0xB0,0x62,0x58);
          midiOUT(0xB0,0x06,0x00);
          midiOUT(0xB0,0x26,0x01); 

        
 } 
  else if (mode_1 == 3 )
 {  
    
     
          lcd.setCursor ( 0, 3 );                
          lcd.print("LOOPER :   PLAY >>> "); 
          
      midiOUT(0xB0,0x63,0x7D);//play
      midiOUT(0xB0,0x62,0x58);
      midiOUT(0xB0,0x06,0x00);
      midiOUT(0xB0,0x26,0x00);      
           
         
 }
 else if (mode_1 == 4)
          
 {  
          
 }
    }
  }
}

You cannot simple exchange delay() and millis().

millis() returns the number of milliseconds since the Arduino was started, so what you do is set a variable to millis() when you start timing then check each time through loop() whether the required period has elapsed

if (millis() - startTime >= period)
{
  //do stuff after the period has elapsed
}
//do stuff that happens each time through loop()

The BlinkWithoutDelay example in the IDE will show the the basics.

I tried with Code: [Select]

unsigned long time = millis()+8000;

but does not work.

There is more to replacing delay() with millis() than that. It requires a completely different approach. Read, understand, and embrace the blink without delay example.

Throw your existing code away, and start over. KNOWING that you can accomplish your goal is half the battle. Knowing that you have states to deal with (the time of interest has not yet started, the time of interest is happening, the time of interest has passed) is the other half. Dealing with what state you are in, and determining if it is time to move to the next state is trivial.

PaulS: Throw your existing code away, and start over. KNOWING that you can accomplish your goal is half the battle. Knowing that you have states to deal with (the time of interest has not yet started, the time of interest is happening, the time of interest has passed) is the other half. Dealing with what state you are in, and determining if it is time to move to the next state is trivial.

Ye. I am beginner.My ino file have 10 pages.I spend 1 month with code. I know I must learn programing, but its hard.

Have you a similar code like I need? Thank you I appreciate your help.

If your code is 10 pages long and you don't know programming then you are probably getting way ahead of yourself.

Write some simple codes to practice using the Blink Without Delay technique until you understand how it works. Start with the Blink Without Delay example that comes with the IDE.

Thank you for your help.

Ok I wrote this code. But how can I write , if I want send midi masage only onetime. With this code is period 8000 ms and loop repeats. Thank you.

I need ony onetimes.

#define LED       13
#define TIME      8000
#define BUTTON  12

unsigned long Time;      
void setup() 
{ 
  Serial.begin(115200);
  
  pinMode(LED, OUTPUT);             
  pinMode(BUTTON, INPUT_PULLUP); 
  Time = millis();                 
}

void loop() 
{ 
  if(digitalRead(BUTTON) == 1)    
  {
    if(millis() >= Time)               
    { 
              
      digitalWrite(LED, !digitalRead(LED)); 
      Time = Time + TIME;   
      
       midiOUT(0x90,60,127);
    }
  }
  else                          
  { 
    digitalWrite(LED, LOW);   
    
  }
}

 void midiOUT(char command, char value1, char value2) {
  Serial.write(command);
  Serial.write(value1);
  Serial.write(value2);

}

Again

Another code, midi send after 5000 ms but sending midi,still.Non stop .

Please help.

It’s a problem to help me ?

unsigned long TimerA;  

const int buttonPin  = 12;     // the pin that the pushbutton is attached to
const int ledPin     = 13;    // the pin that the LED is attached to
 
int buttonState      = 0;     // current state of the button
int lastButtonState  = 0;     // previous state of the button
int ledState         = 0; 

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);  // initialize the button pin as a input
  pinMode(ledPin, OUTPUT);    // initialize the button pin as a output
  
   Serial.begin(115200);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  
  
if (buttonState == 0){
   
   TimerA = millis();
   digitalWrite (ledPin,LOW);
 }
   if (millis()-TimerA>= 5000UL)
   {
 digitalWrite(ledPin,HIGH);
   midiOUT(0x90,60,127);
   
 }
 else {
 digitalWrite (ledPin,LOW);
 
 }
 lastButtonState = buttonState;
   

}
void midiOUT(char command, char value1, char value2) {
  Serial.write(command);
  Serial.write(value1);
  Serial.write(value2);

}

but

 Time = millis();
}

void loop()
{
  if (digitalRead(BUTTON) == 1)
  {
    if (millis() >= Time)
    {

This is going to be true straight away so will trigger the output with no delay.

  startTime = millis();
}

void loop()
{
  if (digitalRead(BUTTON) == 1)
  {
    if (millis() - startTime >= requiredPeriod)
    {

Will trigger output after the required period.

In order to prevent repeated triggering you need to act only when the button becomes pressed, not when it is pressed. Look at the StateChangeDetection example in the IDE to see how to do that.

Thank you But I m not able to write it. Help please. I tried many code for net, but did not find right.

I m not able to write it.

Did you look at the example I suggested ? If you do not understand it then ask more questions.

Yes I did!

I still dont know why midi repeats.

/*

const int buttonPin = 12;    
const int ledPin =  13;  
const int ledPin1 =  53; 

unsigned long  startTime;
unsigned long requiredPeriod = 3000;

int buttonState = 0;       

void setup() {

  pinMode(ledPin, OUTPUT); 
 pinMode(ledPin1, OUTPUT);   
  pinMode(buttonPin, INPUT_PULLUP);  
  
 Serial.begin(115200); 
}

void loop(){

  buttonState = digitalRead(buttonPin);


  if (buttonState == LOW) { 
     startTime = millis();    
     
    digitalWrite(ledPin, HIGH);  
     digitalWrite(ledPin1, LOW);
  } 
  else {
    if (millis() - startTime >= requiredPeriod)
    {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin, LOW);
    midiOUT(0x90,60,127); 
  }
    
  }
}

void midiOUT(char command, char value1, char value2) {
  Serial.write(command);
  Serial.write(value1);
  Serial.write(value2);

}

You set startTime is when the button is pressed so the code below will return true at [u]any[/u] time after the period has elapsed     if (millis() - startTime >= requiredPeriod)which will be every time through loop. If you want to send the MIDI message only once set a boolean flag to true when you send it and check that before sending the message again. Reset the flag to false when the button is released.

However, a better way to do it is to send the message when the button [u]becomes[/u] pressed rather than when it [u]is[/u] pressed. The StateChangeDetection example in the IDE will show you how to do it.