How can i make counter <=5 in mbed

#include “mbed.h”
#include “Servo.h”

PwmOut buzzer(PB_5);
Servo myservo (PB_7);
DigitalOut myLed1 (PB_0);
DigitalOut myLed2 (PB_1);
DigitalIn IR1 (PA_12);
DigitalIn IR2 (PA_11);

int main() {

IR1.mode(PullNone);
IR2.mode(PullNone);
int counter;
int n=0;
int m=0;
for (n=0; n<5; n++)
{
wait_ms(1000); // 1sec delay
myLed1 = !myLed1; // Blinking LED1
}
myLed1 = 0; // LED is OFF

for (m=0; m<5; m++)
{
wait_ms(1000); // 1sec delay
myLed2 = !myLed2; // Blinking LED1
}
myLed2= 0; // LED is OFF

while(1){

counter=counter;
myLed1=0;
myLed2=0;
buzzer=0.0;
myservo=0; // go to 0 degree position
wait (0.1);

if (IR1.read() == 0) {
while(counter<=5){
counter=counter++;
myLed1=1;
myservo =1; // go to 180 degree position
wait(7);
}
}

if (IR2.read() == 0) {

 while(counter>5){
     
 counter=counter--;
 
myLed2=1;

buzzer.period(1.0/659.0);
buzzer=0.5;
wait(1);
buzzer=0.0; // turn off audio 

 for (float i=0; i<26; i=i+5) {
        buzzer.period(1.0/969.0);
        buzzer = i/50.0f;
        wait(0.5);
}
}
}   

}
}

Hello,

please be so kind and place these symbold ``` before and afer your code for better formating/view (edit your post).

Your question makes no sense to me. That has nothing together with Mbed itself, this come from C++ definition and I believe is is OK.

But your manipulation with counter variable is strange

  • int counter; maybe default value is needed - int counter = 0; (that is also the main reason of your issue I think)
  • counter = counter; has no effect
  • counter = counter++; not needed like this, better is counter++;

BR, Jan