Is there an easy way to create a better waveform then always on or always off?

I am trying to create code that will output a signal roughly mimicking the first image below. (The waveform is from an electrical muscle stimulation unit) The frequency is around 24Hz with a duty cycle of 0.6. Is there an easy way to create a better waveform then what I have in the second image? The relevant code below seems to be sending things in binary (all or nothing) but I thought the PWM inputs could emulate analog by 255 degrees. Am I wrong about that? Thanks.

http://www.otherrealm.org/img/biofeedback/walkaidewaveform.png

http://www.otherrealm.org/img/biofeedback/arduinotestwaveform.png

import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
int t=0;
void draw() {
  arduino.analogWrite(9,waveform(t));
}
int waveform(int x){
  return int(abs(sin(x*.01)*250));
}

To get that variable output level, you need to use a DAC. PWM won't do it.
PWM only makes 0-5V square waves of varying width.

I have an original Arduino Mega so apparently it doesn't have DAC… Oh well, one more incentive to by a new board.

Use an external DAC. Send it data via SPI, easy to do.

The Teensy 3.1 has a DAC, and is almost entirely Arduino compatible.

Probably simpler to add an external DAC, though.

You can make a crude DAC using pwm output and a low pass filter.

Crude PWM+filter won’t make that waveform tho.

CrossRoads:
Crude PWM+filter won't make that waveform tho.

What about an R2R ladder DAC? Just throwing it out there; I'm not knowledgeable enough in this are to really know...

:smiley:

Still a DAC tho, and not filtered PWM.

otherrealm:
I have an original Arduino Mega so apparently it doesn't have DAC

The new Arduino Megas don't have DACs either.
The only Arduino I know that does is the Due and possibly the Yun ( don't know about that one for sure )