Pages: [1]   Go Down
Author Topic: Stop PWM signal  (Read 1062 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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:
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)
Logged

Eindhoven Netherlands
Offline Offline
Jr. Member
**
Karma: 2
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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..
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 303
Posts: 26354
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The default isn't required.

This works perfectly for me. What board and version of the IDE are you using?
Code:
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;
  }
}
« Last Edit: August 08, 2013, 08:58:35 am by AWOL » Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Arduino DUE with 1.5.2 IDE
Logged

Venezuela
Offline Offline
God Member
*****
Karma: 17
Posts: 522
Ground, ground, always ground
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
case 'i':
    analogWrite(PWMpin, 127);
    break;
  case 'o':
    pinMode(PWMpin,OUTPUT);
    digitalWrite(PWMpin, LOW);
    break;
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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... :-)
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 303
Posts: 26354
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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..
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 303
Posts: 26354
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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..
Logged

Pages: [1]   Go Up
Jump to: