Sign up ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

I'm trying to figure out how to create some non-blocking code to strobe some RGB leds with analogwrite 7 times a second; I'm really not to sure how to go about this. If anybody could give me some help that'd be great! :)

share|improve this question
    
Which part of the code did you want to unblock? –  Ignacio Vazquez-Abrams Jun 1 '14 at 18:08
    
By non-blocking I mean not using the delay function. And the question is stated above, perhaps you don't understand? –  Sam W Jun 1 '14 at 18:11
    
analogWrite() doesn't call delay(). –  Ignacio Vazquez-Abrams Jun 1 '14 at 18:18
    
Look at the BlinkWithoutDelay example –  sachleen Jun 1 '14 at 18:22
    
Blink with out delay uses boolean which just turns it on or off if I understand correctly. I need to change the current state which could be 0-255 to the specified new one, and back. I'm not sure how I would use boolean variables then. –  Sam W Jun 1 '14 at 19:46

2 Answers 2

up vote 1 down vote accepted

What you want can be achieved with your MCUs timers and their associated interrupts. Timers are basically counters that run inside your hardware and they run decoupled from your code. Since one usually knows how fast these timers tick (e.g. how long it takes to count from one to two and so on) you can specify events that happen when a certain amount of timer ticks passed.

I suggest you have a look at SimpleTimer and Timer1 (the second page is only for ATmega168/328)

Both pages include some code samples where they specify some method that will be called every x milliseconds.

share|improve this answer
    
SimpleTimer isn't a hardware timer solution. In fact, it won't work if you have blocking code... –  Annonomus Penguin Aug 7 '14 at 14:02

Timers are the best way to go, (much) less overhead and you don't have to worry about it elsewhere in your code (other than managing variable use between your main loop and your ISRs).

But I do have a habit of doing things the hard way in code first and then move to timers once I've got it the way I'd like.

Here is sample code to flash the on board LED X times per second, I have calculated it to mean, ON 7 times per second and OFF 7 times as well (meaning, there is a change of state 14 times per second).

This code isn't efficient, it doesn't take into consideration that the value is already set or not, it just writes the value that it should be right now.

Finally, to go to analogWrite and use PWM, just change:

digitalWrite(LED, HIGH);

to

analogWrite(NEW_PIN, PWM_VALUE);

code:

#define LED               13
#define TIMES_PER_SECOND  7

const int M1 = 1000 / TIMES_PER_SECOND;
const int M2 = 1000 / TIMES_PER_SECOND / 2;

void setup(){
  pinMode(LED,OUTPUT);
}

void loop(){
  if(millis()%M1<M2){
    digitalWrite(LED, HIGH);
  } else {
    digitalWrite(LED, LOW);
  } 
}

Remember there is overhead with digital (and analog) writes, so (as I mentioned) you could check to see if the value is set already or not and skip the write.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.