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(){
   // funcion diente de sierra        
   if (hsc >= 256) {
     if (ang>=32) {ang=0;}
   else hsc++;  
   if (pState==0) {
     if (pSignal>=x) { pState=1; } }
     if (pSignal<=-x) { pState=0; } }
   // Sine Pulse Width Modulation
   theta = (2*3.1416*ang)/32.0; // Angulo para la funcion sinusoidal  
    if (mSignal<pSignal) { PWM=0; }
   else { PWM=1; }
      // convert variables

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

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

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,

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