Generation of Triangular wave with a plateau

{
  Setpoint = p;
  if (Setpoint == 120 || pchange < 0) {
  
    p = p - 1;
  }
  if (Setpoint == -120 || pchange > 0) {
    p = p + 1;
  }
  pchange = p - lastp;
  lastp = p;
}

I have included the piece of code that generates a triangular wave with peak crest and trough but I need to generate the triangular wave having flat crest and trough. I am new in Arduino Uno so any help and suggestions are greatly appreciated. This is my first time posting and will apologize for any rule violation.

Ratu1234:

{

Setpoint = p;
 if (Setpoint == 120 || pchange < 0) {
 
   p = p - 1;
 }
 if (Setpoint == -120 || pchange > 0) {
   p = p + 1;
 }
 pchange = p - lastp;
 lastp = p;
}




I have included the piece of code that generates a triangular wave with peak crest and trough but I need to generate the triangular wave having flat crest and trough. I am new in Arduino Uno so any help and suggestions are greatly appreciated. This is my first time posting and will apologize for any rule violation.

We need more details. Like the rest of the program. And what is p? Give that variable a more descriptive name. It seems like it is an important variable.

A picture of the waveform you want would be nice too.

int Setpoint;
int p;
int pchange;
int lastp;

void setup() {
Serial.begin(230400); // Initialize serial interface to computer (i.e. serial monitor or plotter)

}

void loop() {

OSC_SET(); // Oscillate the setpoint up and down

}

void OSC_SET() {
Setpoint = p;
if (Setpoint == 120 || pchange < 0) {

p = p - 1;
}
if (Setpoint == -120 || pchange > 0) {
p = p + 1;
}
pchange = p - lastp;
lastp = p;
}

Hi, thank you for your reply. This is the code that generates a triangular waveform with peaked crest and trough and I am looking for the flatted crest and trough triangular waveform.

All of those variables will always have default value (0) because you never change them. Are you supposed to change those variables somewhere?

There's no timing in the code. It just runs as fast as it can, which will be somewhat variable. Sometimes it will run fast and sometimes slower.

There's no output. You can't know if it is doing anything. If you add output statements, it will slow down.

What frequency do you actually need? What is the rise time and the duration of the flat top? Is it symmetrical around zero?

Please use code tags to post code.

A triangular wave with a plateau is not a triangular wave.