Skip to content

analogWrite in loop() seems to interfere with digitalWrite in serialEvent() [imported] #1091

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cmaglie opened this issue Nov 15, 2012 · 3 comments
Assignees
Labels
Type: Invalid Off topic for this repository, or a bug report determined to not actually represent a bug

Comments

@cmaglie
Copy link
Member

cmaglie commented Nov 15, 2012

This is Issue 1091 moved from a Google Code project.
Added by 2012-11-01T02:46:10.000Z by [email protected].
Please review that bug for more context and additional comments, but update this bug.

Original labels: Type-Defect, Priority-Medium

Original description

/* serial_pin_control_with_breathing - take commands from serial and light or extinguish leds based on it

Demonstration of taking serial commands while doing something else.

*/



int max_led=7;
int min_led=2;
int state=0;
int position=0;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  for (int pin=min_led;pin<=max_led;pin++) 
  {
    pinMode(pin, OUTPUT);     
    digitalWrite(pin,LOW);
  }
  Serial.begin(115200);
  pinMode(11, OUTPUT);
}

void loop(){
  // breathing effect from http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/ to test using serialEvent to 
  // "multitask".
  float val = (exp(sin(millis()/2000.0*PI)) - 0.36787944)*108.0;
  analogWrite(11, val);
  delay(1); // needed so that the digitalWrites in serialEvent() go through.  I do not know why this is needed!
}

void serialEvent() {

 Serial.println(state);
 switch(state) {
   case 0: {
     char inChar = (char)Serial.read();
     if (inChar!='\n') { // ignore newlines
       position=atoi(&inChar);
       state++;
     }
     break;
   }
   case 1: {
     char inChar = (char)Serial.read();

     if (inChar=='+') {
       digitalWrite(min_led+position,HIGH);
     } else if (inChar=='-') {
       digitalWrite(min_led+position,LOW);
     }
   state=0;
   Serial.println("command done");
   break;
   }

  }

}

If I remove delay(1) from the loop, the serial routine works, but the LEDs do not go on or off. If I remove both delay(1) and analogWrite(11, val) then the LEDs work. The LEDs also work if loop() is blank.

@tadam777
Copy link

This is maybe related to a previous bug with digitalWrite : if digitalWrite is used in the main loop and at the exact same time in an interrupt, there was an issue.

This seems to be the case in your code, as serialEvent is interrupt driven.

This was apparently solved a long time ago, but maybe not completely :

https://code.google.com/p/arduino/issues/detail?id=146

About the 1ms delay, it just makes it much more unlikely to happen, as the mail loop will then make a digitalWrite only +-0.1% of the time rather than +-90% of the time.

@NicoHood
Copy link
Contributor

Serialevent is not interrupt driven. One more reason to remove it.
#4328

@matthijskooijman
Copy link
Collaborator

I just tried this code with a recent build from git, and it works just fine, even when I remove the delay. I suspect that there was either a bug that has been fixed already, or the problem was in the original reporters test setup. In any case, I'm closing this bug. If someone can reproduce it after all, just leave a comment and we can reopen.

@matthijskooijman matthijskooijman added the Type: Invalid Off topic for this repository, or a bug report determined to not actually represent a bug label Dec 28, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Invalid Off topic for this repository, or a bug report determined to not actually represent a bug
Projects
None yet
Development

No branches or pull requests

5 participants