Problem with while loop

Hello
any suggestion on using while loop in the below program?
I have a voice recognition module and i am trying to control brightness of 230V lamp using a triac the problem is that for different brightness different output of voice recognition is used since i use a traic to control current flow by switching at required rate but during that time i am not able get output from the voice recognition module since switching is very fast.so i used while loop to do continues switching of triac with some delay i tried reading voice recognition output data but arduino doesn’t take it.I think problem is with while loop,any suggestion?

#include <SoftwareSerial.h>
#include "VoiceRecognitionV3.h"

/**        
  Connection
  Arduino    VoiceRecognitionModule
   2   ------->     TX
   3   ------->     RX
*/
VR myVR(4,5);    // 2:RX 3:TX, you can choose your favourite pins.

uint8_t records[7]; // save record
uint8_t buf[64];

int led1 = 8;
int led2 = 9;
int led3 = 11;
#define oneRecord    (0)
#define twoRecord   (1)
#define threeRecord  (2)
#define fourRecord  (3)
#define TRIACPULSE 13
#define INTER 2
/**
  @brief   Print signature, if the character is invisible, 
           print hexible value instead.
  @param   buf     --> command length
           len     --> number of parameters
*/
void printSignature(uint8_t *buf, int len)
{
  int i;
  for(i=0; i<len; i++){
    if(buf[i]>0x19 && buf[i]<0x7F){
      Serial.write(buf[i]);
    }
    else{
      Serial.print("[");
      Serial.print(buf[i], HEX);
      Serial.print("]");
    }
  }
}

/**
  @brief   Print signature, if the character is invisible, 
           print hexible value instead.
  @param   buf  -->  VR module return value when voice is recognized.
             buf[0]  -->  Group mode(FF: None Group, 0x8n: User, 0x0n:System
             buf[1]  -->  number of record which is recognized. 
             buf[2]  -->  Recognizer index(position) value of the recognized record.
             buf[3]  -->  Signature length
             buf[4]~buf[n] --> Signature
*/
void printVR(uint8_t *buf)
{
  Serial.println("VR Index\tGroup\tRecordNum\tSignature");

  Serial.print(buf[2], DEC);
  Serial.print("\t\t");

  if(buf[0] == 0xFF){
    Serial.print("NONE");
  }
  else if(buf[0]&0x80){
    Serial.print("UG ");
    Serial.print(buf[0]&(~0x80), DEC);
  }
  else{
    Serial.print("SG ");
    Serial.print(buf[0], DEC);
  }
  Serial.print("\t");

  Serial.print(buf[1], DEC);
  Serial.print("\t\t");
  if(buf[3]>0){
    printSignature(buf+4, buf[3]);
  }
  else{
    Serial.print("NONE");
  }
  Serial.println("\r\n");
}

void setup()
{
  /** initialize */
  myVR.begin(9600);
  pinMode(2,INPUT);
  digitalWrite(2,HIGH);
  pinMode(TRIACPULSE,OUTPUT);
  Serial.begin(115200);
  Serial.println(" Control LED sample");
  
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(led3,OUTPUT); 
  if(myVR.clear() == 0){
    Serial.println("Recognizer cleared.");
  }else{
    Serial.println("Not find VoiceRecognitionModule.");
    Serial.println("Please check connection and restart Arduino.");
    while(1);
  }
  
  if(myVR.load((uint8_t)oneRecord) >= 0){
    Serial.println("oneRecord loaded");
  }
  
  if(myVR.load((uint8_t)twoRecord) >= 0){
    Serial.println("twoRecord loaded");
  }
  
  if(myVR.load((uint8_t)threeRecord) >= 0){
    Serial.println("threeRecord loaded");
  }
  
  if(myVR.load((uint8_t)fourRecord) >= 0){
    Serial.println("fourRecord loaded");
  }
}
void function1()
{
 
  delayMicroseconds(9000);
  digitalWrite(TRIACPULSE,HIGH);
  delayMicroseconds(200);
  digitalWrite(TRIACPULSE,LOW);
  digitalWrite(led3,LOW);
  delay(1500);
  printVR(buf);
 }  

void function2()
{
  delayMicroseconds(7000);
  digitalWrite(TRIACPULSE,HIGH);
  delayMicroseconds(200);
  digitalWrite(TRIACPULSE,LOW);
  delay(1500);
  printVR(buf);   
}
void function3()
{ 
  delayMicroseconds(4000);
  digitalWrite(TRIACPULSE,HIGH);
  delayMicroseconds(200);
  digitalWrite(TRIACPULSE,LOW); 
  delay(1500);
  printVR(buf); 
 }  

void function4()
{ 
  delayMicroseconds(2000);
  digitalWrite(TRIACPULSE,HIGH);
  delayMicroseconds(200);
  digitalWrite(TRIACPULSE,LOW);
  delay(1500);
  printVR(buf);
}  
void main1()
{
   while(buf[1]==oneRecord)
  {
  if(digitalRead(INTER)==0)
  {
    attachInterrupt(0,function1,FALLING);

  }
  else if(digitalRead(INTER)==1)
  {
   detachInterrupt(0);
  }
  //delay(1500);
  //printVR(buf);
  } 
}

void main2()
{
  while(buf[1]==twoRecord)
  {
  
  if(digitalRead(INTER)==0)
  {
    attachInterrupt(0,function2,FALLING);
  }
  else if(digitalRead(INTER)==1)
  {
   detachInterrupt(0);
  }
  //delay(1500); 
  //printVR(buf);
  }
}

void main3()
{
  while(buf[1]==threeRecord)
  {
   if(digitalRead(INTER)==0)
  {
    attachInterrupt(0,function3,FALLING);
  }
  else if(digitalRead(INTER)==1)
  {
   detachInterrupt(0);
  }
  //delay(1500);
  //printVR(buf);
  } 
}

void main4()
{
  while(buf[1]==fourRecord)
  {
   if(digitalRead(INTER)==0)
  {
    attachInterrupt(0,function4,FALLING);
  }
  else if(digitalRead(INTER)==1)
  {
   detachInterrupt(0);
  } 
  //delay(1500);
  //printVR(buf);
  }
}
void loop()
{
  int ret;
  ret = myVR.recognize(buf, 50);
  if(ret>0){
    switch(buf[1]){
      case oneRecord:
          digitalWrite(led1,HIGH); 
          main1();
          break;
      case twoRecord:
          digitalWrite(led2,HIGH);
          main2();
          break;
      case threeRecord:
          digitalWrite(led3,HIGH);
          main3();
          break;  
      case fourRecord:
          main4();
          break;  
      default:
        Serial.println("Record function undefined");
        break;
    }
    /** voice recognized */
    printVR(buf);
  }
}

Give your functions meaningful names instead of main1() and function1() and post a new version of your program and I may be able to make sense of it.

Edit to add ...
I just noticed you have huge delay()s in your code. That is likely to be a major problem because the Arduino can do nothing during a delay(). See how millis() is used to manage timing without blocking in Several Things at a Time

...R

I didn’t take a very detailed look at the code (maybe point out which while loop you think is the problem?), but here’s what usually trips up newbies with while loops:

They have a variable in the argument of the while loop, but NEVER UPDATE the variable in the loop!! This causes the Arduino to never exit the while loop! You might want to check for that and fix it.

Power_Broker:
I didn't take a very detailed look at the code (maybe point out which while loop you think is the problem?), but here's what usually trips up newbies with while loops:

They have a variable in the argument of the while loop, but NEVER UPDATE the variable in the loop!! This causes the Arduino to never exit the while loop! You might want to check for that and fix it.

That's pretty much exactly what's happening here.

Quite honestly, your problem is far more than just the while loop. Take this snippet:

void function3()
{
  delayMicroseconds(4000);
  digitalWrite(TRIACPULSE,HIGH);
  delayMicroseconds(200);
  digitalWrite(TRIACPULSE,LOW);
  delay(1500);
  printVR(buf);
 }

I assume that this is supposed to pulse the triac on after the zero crossing. Not only are delays a terrible way of doing this, you also delay for 1,500 milliseconds each time. You'll never see the lamp turn on like this. You're also doing Serial prints in an ISR, which will lock things up once the buffer fills up.

Thanks.Yes i am detecting zero crossing and when that is true interrupt 0 will become low now i jump to function there i am waiting for 4000 microseconds because since i use a 50hz supply each cycle takes 20msec to complete so in half cycle it will be 10msec that is 10000 microseconds if i delay for 4000 microseconds and then give pulse the output ac voltage will be change in this way i can control the brightness. So any suggestion on how can i run the "function" infinitely until given condition is met also in that iteration it has to update the voice input signal and take the value for comparison depending on that value it has to run the 'function'.
condition: buf[1]==oneRecord (buf[1] need to be updated at each iteration)

void function()
{
  delayMicroseconds(4000);
  digitalWrite(TRIACPULSE,HIGH);
  delayMicroseconds(200);
  digitalWrite(TRIACPULSE,LOW);
}