1 keystroke for tempo

Hi, I am new to arduino and programming in general, i’m sorry for any obvious mistakes. I’m trying to activate 2 LED’s interchangeably based on a tempo controlled by a one keystroke. I am having trouble looping an if statement. Could you please take a look at my code and point out what i’m doing wrong. Any suggestions would be appreciated.

char incomingByte;  
long interval = 5000;
long currentTime = 0;
long previousTime = 0;

void setup() {
        Serial.begin(9600);    
        pinMode(10,OUTPUT);
        digitalWrite(10, LOW);
        pinMode(13, OUTPUT);
        digitalWrite(13, HIGH);
       
}

void loop() 
        
{
  currentTime = millis();  
         if (Serial.available() > 0)
         {
           // read the incoming byte:
           incomingByte = Serial.read();           
         }
         if (incomingByte == 'f')
         {                 
           if (currentTime - previousTime < interval)
           {
             digitalWrite(10, LOW);
             digitalWrite(13, HIGH);     
             delay (250);
             digitalWrite(13, LOW);
             delay (250);
             previousTime = currentTime;          
           }
           else  if (currentTime - previousTime > interval)
           {
             digitalWrite(13, LOW);
             digitalWrite(10, HIGH);
             previousTime = currentTime;  
           }
           
         }
}

I am having trouble looping an if statement.

That statement doesn't make sense. You don't "loop an if statement".

Could you please take a look at my code and point out what i'm doing wrong.

Well, for one thing, the code you posted does something. You didn't say what that was. You expect the code to do something. You didn't say what that was.

Sorry for not being clear enough. I want one red LED at pin 13 to pulse whenever the interval between 'f' keystrokes is less than 5 seconds. If the interval between repeating 'f' keystrokes is greater than 5 seconds, i want a green LED at pin 10 to light.

If the interval between repeating 'f' keystrokes is greater than 5 seconds

Then, you need to set a time when there is serial data. Then, independent of whether or not there is serial data, test how long it has been since the last input.

Currently, you set currentTime on every pass through loop. You need something to save when the last serial data arrived.

I’m not sure i understand what you mean by setting a time when there is serial data. Do you mean specify when serial data is used, or record when there is serial data. Also, what can i use that for?
I have edited my code a little where now the ‘f’ keystroke has an effect on both LED’s but once it satisfied the first if statement it continuously loops. How can I fix this?

char incomingByte;  
long interval = 2000;
unsigned long currentTime = 0;
long previousTime = 0;



void setup() {
        Serial.begin(9600);    
        pinMode(10,OUTPUT);
        digitalWrite(10, LOW);
        pinMode(13, OUTPUT);
        digitalWrite(13, LOW);
       
}

void loop() 
        
{
  currentTime = millis();  
 
         if (Serial.available() > 0)
         {
           // read the incoming byte:
           incomingByte = Serial.read();                          
         }
         if (incomingByte == 'f')
         {
        
             if ( currentTime - previousTime < interval)         
             {     
               digitalWrite(10, LOW);
               digitalWrite(13, HIGH);     
               delay (250);
               digitalWrite(13, LOW);
               delay (250);         
               previousTime = currentTime;  
               incomingByte = 0;                  
             }
           
             else if (currentTime - previousTime > interval)
             {
               digitalWrite(13, LOW);
               digitalWrite(10, HIGH);
               previousTime = currentTime; 
               incomingByte = 0; 
             }
             
         }  
}

When you use millis() this way, you are recording when an event of interest occurred. The event of interest is not currentTime. It isn't previousTime. The variable should have a name that means something. That way, at least the code will give a clue as to what the event of interest is - something you have consistently failed to do.

You say that you want to measure the time between 'f's arriving. So, the event of interest seems to be the arrival of an 'f'. When do you detect that happening? It MIGHT happen in this block:

         if (Serial.available() > 0)
         {
           // read the incoming byte:
           incomingByte = Serial.read();                          
         }

But, it happens only if incomingByte is 'f'. You don't test that, and you don't record when that event occurs.

Therefore, either the explanation of what you want to do was wrong, or the rest of your code is meaningless, since you don't have the time that an event of interest occurred.

Thank you for your replies Paul, your comments kept me trying and i’ve finally got it to work. I am not sure I used any of your suggestions because I never quiet understood your comments.

If anyone is interested this is how i got it working:

char incomingByte;  // keystroke
long interval = 3000; // interval between bites
unsigned long currentTime = 0; // time passed than 
long previousTime = 0;  // previous time since last bite
int ledR = 0; // Red LED light indication


void setup() {
        Serial.begin(9600);    
        pinMode(10,OUTPUT);
        digitalWrite(10, HIGH);
        pinMode(13, OUTPUT);
        digitalWrite(13, LOW);
       
}

void loop() 
        
{
  currentTime = millis();  
 
         if (Serial.available() > 0)
         {
           // read the incoming byte:
           incomingByte = Serial.read();                          
         }
         if (incomingByte == 'f') // keystroke to press when one takes a bite
         {
        
             if ( currentTime - previousTime < interval)         
             {     
               digitalWrite(10, LOW); // turn off green LED
               ledR = 1;  // turn RED LED off      
               previousTime = currentTime;  
               incomingByte = 0;                  
             }
           
             else if (currentTime - previousTime > interval)
             {
               digitalWrite(13, LOW); // turn off red LED
               digitalWrite(10, HIGH); // turn on green LED
               previousTime = currentTime; // update bite time
               incomingByte = 0; 
               ledR = 0;
             }
         }
             
         if (ledR == 1 && incomingByte == 0)
         {
             digitalWrite(13, HIGH);  // turn red LED on
             delay (250); // wait a little bit
             digitalWrite(13, LOW); // turn red LED off
             delay (250); 
         }  
         
}