Setup Loop style sketch to int main avrgcc style sketch

I have a working sketch that is worked by a switch to drive four leds in response to onetap, doubletap tripletap quadtap, hold and tapandhold.

This is a part of code will be added to later.

The working code is

//Target AVR ATmega32@16MHz


//www.diyusthad.com
//www.youtube.com/c/diyusthad
//www.facebook.com/diyusthad
//program by diyusthad
//code for programming multiple functions to a single push button
#define sw PC2 //pin at push sw is connected
#define white PB3
#define red PB0
#define green PB1
#define blue PB2

int lastButtonState = HIGH;   // the previous reading from the input pin
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
bool buttonState = HIGH; //saving state of the switch
byte tapCounter; //for saving no. of times the switch is pressed
int timediff; //for saving the time in between each press and release of the switch
bool flag1, flag2; //just two variables
long double presstime, releasetime; //for saving millis at press and millis at release


void setup () {
  //Serial.begin(9600); //for serial monitor
  //pinMode(sw, INPUT_PULLUP); //setting pin 5 as input with internal pull up resistor
  DDRC &= ~((1 << DDC2) | (1 << DDC1) | (1 << DDC0)); //Set PC0 and PC1 and PC2 as input
  PINC |= ((1 << PINC2) | (1 << PINC1) | (1 << PINC0));  // Input pullups enabled
  PORTD = 0xFF; /*  Writing all bits on port D to 0 */
  DDRD = 0xFF;  /*    Port D set as ouput */
  DDRD &= ~(1 << DDD0); //  Setting PDO as input
  PIND = (1 << PIND0);  // Enabled input pullup.
  PORTB = 0b00000000;
  DDRB = 0b11111111 ; /* define port direction for PB0 to PB5 as  output */

  nolight();
}

void loop ()  {
  readSwitch();
}

void readSwitch () {
  //int reading = digitalRead(sw);
  int reading = PINC & (1 << sw);
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;
    }
  }

  //when switch is pressed
  if (buttonState == 0 && flag2 == 0)
  {
    presstime = millis(); //time from millis fn will save to presstime variable
    flag1 = 0;
    flag2 = 1;
    tapCounter++; //tap counter will increase by 1

  }
  //when sw is released
  if (buttonState == 1 && flag1 == 0)
  {
    releasetime = millis(); //time from millis fn will be saved to releasetime var
    flag1 = 1;
    flag2 = 0;

    timediff = releasetime - presstime; //here we find the time gap between press and release and stored to timediff var

  }

  if ((millis() - presstime) > 400 && buttonState == 1) //wait for some time and if sw is in release position
  {
    if (tapCounter == 1) //if tap counter is 1
    {
      if (timediff >= 400) //if time diff is larger than 400 then its a hold
      {
        //Serial.println("Hold");
        hold(); //fn to call when the button is hold
      }
      else //if timediff is less than 400 then its a single tap
      {
        //Serial.println("single tap");
        singleTap(); //fn to call when the button is single taped
      }
    }
    else if (tapCounter == 2 ) //if tapcounter is 2
    {
      if (timediff >= 400) // if timediff is greater than  400 then its single tap and hold
      {
        //Serial.println("single tap and hold");
        tapAndHold(); //fn to call when the button is single tap and hold
      }
      else // if timediff is less than 400 then its just double tap
      {
        //Serial.println("double tap");
        doubleTap(); //fn to call when doubletap
      }
    }
    else if (tapCounter == 3) //if tapcounter is 3 //then its triple tap
    {
      //Serial.println("triple tap");
      tripleTap(); //fn to call when triple tap
    }
    else if (tapCounter == 4) //if tapcounter is 4 then its 4 tap
    {
      //Serial.println("four tap");
      fourTap();//fn to call when four tap
    }
    tapCounter = 0;
  }
  lastButtonState = reading;
}






void nolight()
{
  PORTB |= (1 << PB0);
  PORTB |= (1 << PB1);
  PORTB |= (1 << PB2);
  PORTB |= (1 << PB3);
}
void singleTap()
{
  nolight();
  PORTB &= ~(1 << PB0);
}
void doubleTap()
{
  nolight();
  PORTB &= ~(1 << PB1);
}
void tripleTap()
{
  nolight();
  PORTB &= ~(1 << PB2);
}
void fourTap()
{
  nolight();
  PORTB &= ~(1 << PB3);
}
void hold()
{
  nolight();

}

void tapAndHold()
{
  nolight();
  PORTB &= ~(1 << PB0);
  PORTB &= ~(1 << PB1);
  PORTB &= ~(1 << PB2);
  PORTB &= ~(1 << PB3);
}

The int main(void) style sketch in next post as I've hit the 9000 char limit oops..

This is the code that stops working

//Target AVR ATmega32@16MHz


//www.diyusthad.com
//www.youtube.com/c/diyusthad
//www.facebook.com/diyusthad
//program by diyusthad
//code for programming multiple functions to a single push button
#define sw PC2 //pin at push sw is connected
#define white PB3
#define red PB0
#define green PB1
#define blue PB2

int lastButtonState = HIGH;   // the previous reading from the input pin
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
bool buttonState = HIGH; //saving state of the switch
byte tapCounter; //for saving no. of times the switch is pressed
int timediff; //for saving the time in between each press and release of the switch
bool flag1, flag2; //just two variables
long double presstime, releasetime; //for saving millis at press and millis at release


int main (void) {
  //Serial.begin(9600); //for serial monitor
  //pinMode(sw, INPUT_PULLUP); //setting pin 5 as input with internal pull up resistor
  DDRC &= ~((1 << DDC2) | (1 << DDC1) | (1 << DDC0)); //Set PC0 and PC1 and PC2 as input
  PINC |= ((1 << PINC2) | (1 << PINC1) | (1 << PINC0));  // Input pullups enabled
  PORTD = 0xFF; /*  Writing all bits on port D to 0 */
  DDRD = 0xFF;  /*    Port D set as ouput */
  DDRD &= ~(1 << DDD0); //  Setting PDO as input
  PIND = (1 << PIND0);  // Enabled input pullup.
  PORTB = 0b00000000;
  DDRB = 0b11111111 ; /* define port direction for PB0 to PB5 as  output */

  nolight();

  while (1)  {
    readSwitch();
  }
}

void readSwitch () {
  //int reading = digitalRead(sw);
  int reading = PINC & (1 << sw);
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;
    }
  }

  //when switch is pressed
  if (buttonState == 0 && flag2 == 0)
  {
    presstime = millis(); //time from millis fn will save to presstime variable
    flag1 = 0;
    flag2 = 1;
    tapCounter++; //tap counter will increase by 1

  }
  //when sw is released
  if (buttonState == 1 && flag1 == 0)
  {
    releasetime = millis(); //time from millis fn will be saved to releasetime var
    flag1 = 1;
    flag2 = 0;

    timediff = releasetime - presstime; //here we find the time gap between press and release and stored to timediff var

  }

  if ((millis() - presstime) > 400 && buttonState == 1) //wait for some time and if sw is in release position
  {
    if (tapCounter == 1) //if tap counter is 1
    {
      if (timediff >= 400) //if time diff is larger than 400 then its a hold
      {
        //Serial.println("Hold");
        hold(); //fn to call when the button is hold
      }
      else //if timediff is less than 400 then its a single tap
      {
        //Serial.println("single tap");
        singleTap(); //fn to call when the button is single taped
      }
    }
    else if (tapCounter == 2 ) //if tapcounter is 2
    {
      if (timediff >= 400) // if timediff is greater than  400 then its single tap and hold
      {
        //Serial.println("single tap and hold");
        tapAndHold(); //fn to call when the button is single tap and hold
      }
      else // if timediff is less than 400 then its just double tap
      {
        //Serial.println("double tap");
        doubleTap(); //fn to call when doubletap
      }
    }
    else if (tapCounter == 3) //if tapcounter is 3 //then its triple tap
    {
      //Serial.println("triple tap");
      tripleTap(); //fn to call when triple tap
    }
    else if (tapCounter == 4) //if tapcounter is 4 then its 4 tap
    {
      //Serial.println("four tap");
      fourTap();//fn to call when four tap
    }
    tapCounter = 0;
  }
  lastButtonState = reading;
}






void nolight()
{
  PORTB |= (1 << PB0);
  PORTB |= (1 << PB1);
  PORTB |= (1 << PB2);
  PORTB |= (1 << PB3);
}
void singleTap()
{
  nolight();
  PORTB &= ~(1 << PB0);
}
void doubleTap()
{
  nolight();
  PORTB &= ~(1 << PB1);
}
void tripleTap()
{
  nolight();
  PORTB &= ~(1 << PB2);
}
void fourTap()
{
  nolight();
  PORTB &= ~(1 << PB3);
}
void hold()
{
  nolight();

}

void tapAndHold()
{
  nolight();
  PORTB &= ~(1 << PB0);
  PORTB &= ~(1 << PB1);
  PORTB &= ~(1 << PB2);
  PORTB &= ~(1 << PB3);
}

What am I missing? Need enlightenment from masters!

Perhaps a few serial prints, to figure out whee the code stops working, might be in order?

Which Arduino board are you using ?

UKHeliBob:
Which Arduino board are you using ?

I am compiling this code for an ATmega 32 @16 MHz, using MCUDUDES Mighty core in Arduino IDE 1.8.11

UKHeliBob:
Which Arduino board are you using ?

Did compile it on an Arduino Uno

In the Arduino style code it works flawlessly.

In the Avr C style it doesnt.

Idahowalker:
Perhaps a few serial prints, to figure out whee the code stops working, might be in order?

I compiled the code on an Arduino Uno and with the" Serial.println ("xxxxxx");"

The sketch worked flawlessly in the Arduino style code.

with the compile of code in Avr gcc style even the serial monitor is mute.
This is the code that is working

//For Arduino Uno


//www.diyusthad.com
//www.youtube.com/c/diyusthad
//www.facebook.com/diyusthad
//program by diyusthad
//code for programming multiple functions to a single push button
#define sw PC2 //pin at push sw is connected
#define white PB3
#define red PB0
#define green PB1
#define blue PB2

int lastButtonState = HIGH;   // the previous reading from the input pin
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
bool buttonState = HIGH; //saving state of the switch
byte tapCounter; //for saving no. of times the switch is pressed
int timediff; //for saving the time in between each press and release of the switch
bool flag1, flag2; //just two variables
long double presstime, releasetime; //for saving millis at press and millis at release


void setup () {
  Serial.begin(115200); //for serial monitor
  //pinMode(sw, INPUT_PULLUP); //setting pin 5 as input with internal pull up resistor
  DDRC &= ~((1 << DDC2) | (1 << DDC1) | (1 << DDC0)); //Set PC0 and PC1 and PC2 as input
  PINC |= ((1 << PINC2) | (1 << PINC1) | (1 << PINC0));  // Input pullups enabled
  PORTD = 0xFF; /*  Writing all bits on port D to 0 */
  DDRD = 0xFF;  /*    Port D set as ouput */
  DDRD &= ~(1 << DDD0); //  Setting PDO as input
  PIND = (1 << PIND0);  // Enabled input pullup.
  PORTB = 0b00000000;
  DDRB = 0b11111111 ; /* define port direction for PB0 to PB5 as  output */
  nolight();
}
void loop () {

  //int reading = digitalRead(sw);
  int reading = PINC & (1 << PC2);
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;
    }
  }

  //when switch is pressed
  if (buttonState == 0 && flag2 == 0)
  {
    presstime = millis(); //time from millis fn will save to presstime variable
    flag1 = 0;
    flag2 = 1;
    tapCounter++; //tap counter will increase by 1

  }
  //when sw is released
  if (buttonState == 1 && flag1 == 0)
  {
    releasetime = millis(); //time from millis fn will be saved to releasetime var
    flag1 = 1;
    flag2 = 0;

    timediff = releasetime - presstime; //here we find the time gap between press and release and stored to timediff var

  }

  if ((millis() - presstime) > 400 && buttonState == 1) //wait for some time and if sw is in release position
  {
    if (tapCounter == 1) //if tap counter is 1
    {
      if (timediff >= 400) //if time diff is larger than 400 then its a hold
      {
        Serial.println("Hold");
        hold(); //fn to call when the button is hold
      }
      else //if timediff is less than 400 then its a single tap
      {
        Serial.println("single tap");
        singleTap(); //fn to call when the button is single taped
      }
    }
    else if (tapCounter == 2 ) //if tapcounter is 2
    {
      if (timediff >= 400) // if timediff is greater than  400 then its single tap and hold
      {
        Serial.println("single tap and hold");
        tapAndHold(); //fn to call when the button is single tap and hold
      }
      else // if timediff is less than 400 then its just double tap
      {
        Serial.println("double tap");
        doubleTap(); //fn to call when doubletap
      }
    }
    else if (tapCounter == 3) //if tapcounter is 3 //then its triple tap
    {
      Serial.println("triple tap");
      tripleTap(); //fn to call when triple tap
    }
    else if (tapCounter == 4) //if tapcounter is 4 then its 4 tap
    {
      Serial.println("four tap");
      fourTap();//fn to call when four tap
    }
    tapCounter = 0;
  }
  lastButtonState = reading;


}



void nolight()
{
  PORTB |= (1 << PB0);
  PORTB |= (1 << PB1);
  PORTB |= (1 << PB2);
  PORTB |= (1 << PB3);
}
void singleTap()
{
  nolight();
  PORTB &= ~(1 << PB0);
}
void doubleTap()
{
  nolight();
  PORTB &= ~(1 << PB1);
}
void tripleTap()
{
  nolight();
  PORTB &= ~(1 << PB2);
}
void fourTap()
{
  nolight();
  PORTB &= ~(1 << PB3);
}
void hold()
{
  nolight();

}

void tapAndHold()
{
  nolight();
  PORTB &= ~(1 << PB0);
  PORTB &= ~(1 << PB1);
  PORTB &= ~(1 << PB2);
  PORTB &= ~(1 << PB3);
}

Where are you getting the millis() function from in your non-working code? Without the initialization of TIMER0 that happens in the Arduino main() function before calling setup() or loop(), you'll need additional initialization code to have millis() work "normally." (and even more code to get Serial.xxx() working.)

Yes westfw, you are right. I do not need serial.println(). was using it as a debug tool.

Functions or code with millis in the while loop stops working for the reason given by you. Surprising the compiler is mum.
I think the timer has to be initialised in CTC mode. What will the prescalar be 64 or 256?

https://www.avrfreaks.net/forum/how-do-i-replace-millis-arduino-when-not-using-arduino

I read this and your post at #3 . I will give it a try.
BTW where do I get the math detail from?