Go Down

Topic: Stop PWM signal (Read 1 time) previous topic - next topic

goan

Hi, may somebody explain me why I'm not able to stop a PWM signal once it's started? Here's the code:

Code: [Select]
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(11,OUTPUT);
  digitalWrite(11,LOW);
}

void loop() {
  // put your main code here, to run repeatedly:
  while(!Serial.available());
  switch(Serial.read()){
  case 'i':
    analogWrite(11,127);
    break;
  case 'o':
    digitalWrite(11,LOW);
    break;
  }
}


I'm only able to stop it using
Code: [Select]
analogWrite(11,0);

even if inside the reference you'll find
Quote
After a call to analogWrite(), the pin will generate a steady square wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite() on the same pin)

muddy

Not sure, but your switch() statement seems to be incomplete. You should add
default: break;

goan

This is only a minimal code to explain my problem, but it's not inside a switch statement in my program and although it doesn't work..

AWOL

#3
Aug 08, 2013, 03:55 pm Last Edit: Aug 08, 2013, 03:58 pm by AWOL Reason: 1
The default isn't required.

This works perfectly for me. What board and version of the IDE are you using?
Code: [Select]
const byte PWMpin = 11;
void setup() {
 Serial.begin(9600);
}

void loop() {
 while(!Serial.available());
 switch(Serial.read()){
 case 'i':
   analogWrite(PWMpin, 127);
   break;
 case 'o':
   digitalWrite(PWMpin, LOW);
   break;
 }
}
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

goan

Arduino DUE with 1.5.2 IDE

Palliser

Hello goan,

analogWrite puts PWMpin into a PWM mode. digitalWrite will not work on that mode, thus, before use it you must set up PWMpin as output. I am not sure if the Arduino team is working on this. Regards!

Code: [Select]
case 'i':
    analogWrite(PWMpin, 127);
    break;
  case 'o':
    pinMode(PWMpin,OUTPUT);
    digitalWrite(PWMpin, LOW);
    break;

goan

Hi Palliser, thank for your suggestion. Your workaround let me stop PWM, but then I'm not able to start it again.. This is an unpleasant behaviour, the only way I found to start and stop it, is to change the dutycycle. If someone has other idea, please let me know.
Is this problem due to arduinoDUE? Does anybody had in the past the need to start and stop PWM signal? I can't believe I'm the first... :-)

AWOL

Quote
I'm only able to stop it using
Code:
analogWrite(11,0);

I confess to being puzzled as to why this is a problem - it seems an obvious approach to me
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

goan

Don't you think that it's a problem not being able to change a pin mode once you use analogWrite to a pin?
So, the only way to change that pin functionality is to rewrite the firmware? It seems quite ridiculous..

AWOL

Quote
Don't you think that it's a problem not being able to change a pin mode once you use analogWrite to a pin?

Possibly, but since there's an effective workaround (analogWrite (pin, 0) / analogWrite (pin, 255) ), I can't get too agitated about it.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

goan

Ok, thanks anyway... I would only know if it was a limit of mine in making it work, beacause inside the reference it's written you can..

Go Up