ArduinoMega 2560 PWM

I need help pls. Can someone write the code below to programming a pwm 0 to 5
VDC pulse of at least 25 millisecond duration and another for 5 to 15 VDC pulse ?

a PWM waveform is some % (duty cycle) at some constant frequency.

PWM will not generate a single pulse. it might be easier to simply set a pin HIGH and then LOW after a 25 msec delay, delay(25)

the output is typically to the Vcc of the chip, 5V, but a pulse between 5 and 15V requires an external circuit

Basically, if you want someone to write code for you, have this post moved over to gigs and collaborations.

Your specification is not clear. Can you draw a timing diagram of what you want?

image
Width 25ms
0 V to 5V

Output 0 to 5 V*

So you want an output that is LOW (0V) for 25 milliseconds and then HIGH (5V) for 25 milliseconds and repeats? The 'BlinkWithoutDelay' example can be easily modified to do that:

// constants won't change. Used here to set a pin number:
const int outputPin =  LED_BUILTIN; // the number of the LED pin

// Variables will change:
int outputState = LOW;             // outputState used to set the LED
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 25;           // interval at which to blink (milliseconds)

void setup()
{
  // set the digital pin as output:
  pinMode(outputPin, OUTPUT);
}

void loop()
{
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval)
  {
    // save the last time you blinked the LED
    previousMillis += interval;

    // if the LED is off turn it on and vice-versa:
    if (outputState == LOW)
    {
      outputState = HIGH;
    }
    else
    {
      outputState = LOW;
    }

    // set the LED with the outputState of the variable:
    digitalWrite(outputPin, outputState);
  }
}

Thanks!!!

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