Sawtooth Wave Parameters

int ledPin = 13; 

void setup() {} 

void loop() 
{
for (int i=0; i<=255; i++) 
 {
 analogWrite(ledPin, i); 
 delay(10); 
 } 
for (int i=255; i>=0; i--) 
 {
 analogWrite(ledPin, i); 
 delay(10);
 }
}

I am trying to edit the above code (triangular wave with 2.6- second rising edge and 2.6-second duration falling edge) so that it produces a sawtooth wave with 20-ms duration in rising edge and 2.6-second duration in falling edge. I am not sure where to even start with this as I am new to Arduino. Thank you!

Please explain how this code makes a wave of any kind. What Arduino board are you using?

Arduino Mega 2560
Pin #13 is connected to an external LED in series with a 220 Ω resistor

Hello
Add Serial.println(i) to monitor the wave form by using the serial plotter function of the IDE and LedPin 13 isn´t a PWM output at the Arduino UNO.

Have a nice day and enjoy coding in C++.
Дайте миру шанс!

Also, there's an RC circuit with 10 kOhm resistor and 1 microF capacitor

How are you going to determine the output waveform you get? Is your RC filter providing the correct analog output?

You will be better served by using micros() instead of millis() for the leading edge.

So 256 steps with 10 millisecond delay take 2600 milliseconds. 2600 - 2560 = 40 milliseconds. I think if you remove the delay completely you will get about 40 milliseconds for your rising edge. Try removing the delay AND changing "i++" to "i += 2". That should get you close to 20 milliseconds

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.