How to generate HF signals

Hi everyone ,I’m new in arduino and I’ve been looking for a way to generate SPWM using both carrier and sine wave. Could you tell me how to generate a high frecuency triangle signal in arduino mega?.
Below you can find my arduino code used to generate triangle, sine and PWM signal.

int hsc=0;
int PWM=0;
int thetax=0;
int mSignalx=0;
int pSignalx=0;
int pState=0;
float theta=0.0;
float ang=0.0;
float mSignal=0.0;
float pSignal=0.0;
float x=64;
void setup() {
}
void loop(){
while(1)
 {
   // funcion diente de sierra        
   if (hsc >= 256) {
     hsc=0;
     ang++;
     if (ang>=32) {ang=0;}
     }
   else hsc++;  
   if (pState==0) {
     pSignal++;
     if (pSignal>=x) { pState=1; } }
   else{
     pSignal--;  
     if (pSignal<=-x) { pState=0; } }
   // Sine Pulse Width Modulation
   theta = (2*3.1416*ang)/32.0; // Angulo para la funcion sinusoidal  
   mSignal=x*sin(theta);
    if (mSignal<pSignal) { PWM=0; }
   else { PWM=1; }
      // convert variables
   thetax=round(theta);
   mSignalx=round(mSignal);
   pSignalx=round(pSignal);  
}
}

What's wrong with that code? Does it not work?

Delta_G:
What's wrong with that code? Does it not work?

Delta_G:
What's wrong with that code? Does it not work?

The code works, I'd like to know how to increase the frecuency of my triangle signal to 6kHz

Why didn't you say so?

How well do you understand how it works? Where did you get it? Did you write it?

It looks like that code is limited by execution speed. What frequency does it produce now?

One way to speed thing up is to pre-calculate tables rather than calling the sin() function. Your calculations break down to:
mSignal = 64.0 * sin((2 * 3.1416 * ang) / 32.0
And 'ang' is in integer from 0 to 31. That looks like a mistake since I would expect you would want sin(n/32) rather than sin(n)/32. After that is corrected the data looks like:

int sineWave[32] = {0,12,24,35,45,53,59,62,
63,62,59,53,45,35,24,12,
-1,-13,-25,-36,-46,-54,-60,-63,
-64,-63,-60,--54,46,-36,-25,-13};

Your code doesn't actually DO anything with the results of all the calculations. It just throws the results away.