Calling multiple functions and nested If statements

I’ve been trying to read button states while also receiving serial commands. The serial commands come from a C# program that write to the Arduino and the Arduino sends serial inputs to the C# program which logs when an event occurs. Now it seems when I send serial commands from the Arduino by placing the if arg before my button arg, it will send the Serial character but if I put the button Arg before the serial if arg, it will only read the button state and send serial to the C# program. I can’t get them to do both. Does anyone know what I’m doing wrong because I’ve changed it a dozen ways and can’t seem to get it to work properly. I just want to read serial inputs of “r, c, l and b” or send serial out puts from a button state of “left, right, center, back and log file”

int nbts = 3;
int startpin = 10;
int bts[3];
boolean btgs[3];
int incomingByte = 0;

//End Button Declarations
void setup() {
Serial.begin(9600);
for(int i=0;i<nbts; i++) bts = i+startpin;
_ for(int i=0;i<nbts; i++) btgs = false;_
for(int i=0;i<nbts; i++) pinMode(bts*, INPUT_PULLUP);
_ delay(500);
}
void loop() {
for(int i=0;i<nbts;i++){
if(digitalRead(bts)==LOW){
if(!btgs){
btgs = true;
if(bts== 12){
Serial.println(“left”);
delay(8000);
Serial.println(“Log File”);
}
btgs = true;
if(bts== 10){
Serial.println(“center”);
delay(8000);
Serial.println(“Log File”);
}
btgs = false;
if(bts== 11){
Serial.println(“right”);
delay(8000);
Serial.println(“Log File”);
}
else if(Serial.available() > 0){
incomingByte = Serial.read();
Serial.println(incomingByte, DEC);
if(incomingByte == 114){
Serial.println(“right”);
delay(8000);
Serial.println(“Log File”);
}
if(incomingByte == 99){
Serial.println(“center”);
delay(8000);
Serial.println(“Log File”);
}
if(incomingByte == 108){
Serial.println(“left”);
delay(8000);
Serial.println(“Log File”);
}
if(incomingByte == 98){
Serial.println(“back”);
delay(8000);
Serial.println(“Log File”);
}
}
}
}
}
}*

Where the “else if” arg is where the serial read begins after the buttons state read. Button state reads if I compile it this way. If I place the Serial.available args first and switch positions with the button state then the serial send functions work fine. They just don’t seem to work together. Thanks if anyone has an opinion on what I’m doing wrong!_

And now with code tags, please.

SwitchCase
Auto Format

Whenever you hit one of those "delay(8000);" calls the sketch will do nothing for 8 seconds. This will make the sketch very unresponsive. You will need to use the millis() timer to schedule things to allow multiple operations to share the processor time. See the beginner's guide at the top of the forum.

    if(incomingByte == 'r'){
           ...
            if(incomingByte == 'c'){// etc

So much easier to read, don't you think?

Not really sure I follow your btgs logic but here’s a slightly-revamped version of your code worth trying.

int nbts = 3;
int startpin = 10;
int bts[3];
boolean btgs[3];
int incomingByte = 0;

//End Button Declarations
void setup() 
{
    Serial.begin(9600);
    for( int i=0; i<nbts; i++ )
    {
         bts[i] = i+startpin;
         btgs[i] = false;
         pinMode( bts[i], INPUT_PULLUP );
    }
        
    delay(500); //?
}
    
void loop() 
{
    static unsigned long
        timeStart = 0,
        timeDelay = 0;
    unsigned long
        timeNow;
        
    static bool
        bDelayFlag = false;

    timeNow = millis();
    if( (timeNow - timeStart) < timeDelay )
        return;

    bDelayFlag = false;
    
    if( ReadSerial() )
    {
        Serial.println( incomingByte, DEC );
        switch( incomingByte )
        {
            case    'r':
            case    'R':
                bDelayFlag = true;
                Serial.println("right");
            
            break;
            //
            case    'c':
            case    'C':
                bDelayFlag = true;
                Serial.println("center");
            
            break;
            //
            case    'l':
            case    'L':
                bDelayFlag = true;
                Serial.println("left");
                
            break;
            //
            case    'b':
            case    'B':
                bDelayFlag = true;
                Serial.println("back");
                
            break;
            //
            default:
                bDelayFlag = false;

            break;
        }//switch
        
    }//if
    
    for( int i=0; i<nbts; i++ )
    {
        if( digitalRead( bts[i] ) == LOW )
        {
            if( !btgs[i] )
            {
                btgs[i] = true;
                if( bts[i] == 12 )
                { 
                    bDelayFlag = true;
                    Serial.println("left");
                }//if
                
                btgs[i] = true;
                if( bts[i]== 10 )
                {
                    bDelayFlag = true;
                    Serial.println("center");
                }//if
                
                btgs[i] = false;
                if( bts[i] == 11 )
                {
                    bDelayFlag = true;
                    Serial.println("right");
                }//if
                
            }//if
            
        }//if
        
    }//for

    if( bDelayFlag )
    {
        Serial.println("Log File");
        timeStart = millis();
        timeDelay = 8000;
    }//if
    else
        timeDelay = 0;
        
    
}//loop

int ReadSerial( void )
{
    int
        incomingByte;
        
    if( Serial.available() > 0 )
    {
        incomingByte = Serial.read();
        return( incomingByte );
        
    }//if
    
    return 0;        
        
}//ReadSerial

(compiles but not tested…)