PDA

View Full Version : Need help with analogWrite and digitalWrite on same pin



keithg
01-18-2013, 06:25 PM
Group,

I have ported a working sketch from Arduino to Teensy 3.0. All of it works beautifully except when I try to get a PWM output from a pin. It seems I am behind the times because this simple code does nothing on the pin when I try to analog write to it. I am running Beta11 on Ubuntu 12.04. My code is pretty complicated, but if I can get this simple sketch working, I think I can get this put into the code... I will need a digitalWrite and a PWM on the pin for the sketch to function correctly.

adapted from the statechange example. If I do this, the pin will flop from high to low:


#include <Bounce.h>
#define BUTTON 0
#define LED 13
#define pwm_output 8

int ledValue = LOW;
int pwmValue = 100;
// This example changes the state of the LED everytime the button is pushed
// Build the circuit indicated here: http://arduino.cc/en/Tutorial/Button


Bounce bouncer = Bounce( BUTTON, 15 );

void setup() {
pinMode(BUTTON,INPUT);
pinMode(LED,OUTPUT);
pinMode(pwm_output, OUTPUT);
}

void loop() {

if ( bouncer.update() ) {
if ( bouncer.read() == HIGH) {
if ( ledValue == LOW ) {
ledValue = HIGH;
pwmValue = 100;
} else {
ledValue = LOW;
pwmValue = 5;
}
digitalWrite(LED,ledValue);
digitalWrite(pwm_output, ledValue);
}
}
}

if I change the last line to:



analogWrite(pwm_output, pwmValue);

The pin stays at LOW and nothing happens.

Thanks,

Keith

keithg
01-18-2013, 07:08 PM
D'oh!

Sorry for the sideshow. I was using a pin which is not PWM capable...

Now I can get it to work, but have to redefine the pin when I use it as digital or PWM. I get it now.

Keith