Signal Generator code issue, can anyone help?

I’m making a signal generator with a arduino. It’s working fine, but when I generate the square wave a wild sine wave of period 2pi appears. As show in the images bellow

my code is

int sine[255];
int potenciometro = A2; // Define o potenciometro no pino analogico 2
int f; // Variavel da Frequencia
int modePin = 8; // Botao de selecao de modo
int mode = 0; // Seletor de ondas (Inicial Desligado)
int val = 0; // Pin 13 HIGH/LOW Status
int butState = 0; // Last Button State
int modeState = 0; // Last Mode State

void setup() 
{ 
     pinMode(0, OUTPUT); 
     pinMode(1, OUTPUT); 
     pinMode(2, OUTPUT); 
     pinMode(3, OUTPUT); 
     pinMode(4, OUTPUT); 
     pinMode(5, OUTPUT); 
     pinMode(6, OUTPUT);
     pinMode(7, OUTPUT); 
     pinMode(8, INPUT);
    
     float x; 
     float y; 
     for(int i=0;i<255;i++)
     {
          x=(float)i;
          y=sin((x/255)*2*PI);
          sine[i]=int(y*128)+128;
     }
} 

void loop() {
 val = digitalRead(modePin); 
 if (val != butState && val == HIGH){
 mode++;
 }
 butState = val; // Keep track of most recent button state
 if (modeState != mode){
 switch ( mode ) {
   
  case 1:
 
 f = analogRead(potenciometro);
 digitalWrite(0, HIGH); 
 digitalWrite(1, HIGH);
 digitalWrite(2, HIGH);
 digitalWrite(3, HIGH);
 digitalWrite(4, HIGH);
 digitalWrite(5, HIGH);
 digitalWrite(6, HIGH);
 digitalWrite(7, HIGH);
     delay(f); 
 digitalWrite(0, LOW);
 digitalWrite(1, LOW);
 digitalWrite(2, LOW);
 digitalWrite(3, LOW);
 digitalWrite(4, LOW);
 digitalWrite(5, LOW);
 digitalWrite(6, LOW);
 digitalWrite(7, LOW);
     delay(f);
    
    case 2:
     f = analogRead(potenciometro); // Registra em f o valor do Potenciometro
     for (int i=0;i<255;i++) 
     { 
          PORTD=sine[i]; 
          delayMicroseconds(f); 
     }
      break;

      case 3:
      f = analogRead(potenciometro);
      for (int i=0;i<255;i++) 
     { 
          PORTD=i;
          delayMicroseconds(f);  
     } 
      for (int i=255;i>0;i--) 
     { 
          PORTD=i;
          delayMicroseconds(f);
     }
 break;
 default:
 mode = 1;
 //modeState = mode; // Keep track of mode recent mode value
 delay(10);
}
}
}

What I am doing wrong?

A period of 2pi times what? 2pi is a dimensionless quantity.

aarg: A period of 2pi times what? 2pi is a dimensionless quantity.

well thats irrelevant, but a period 2pi means the angle varies from 0 to 2pi. as the image shows. My question is how to remove it from the square wave?!

Why do you have this commented out?

 //modeState = mode; // Keep track of mode recent mode value