Go Down

Topic: PWM sine wave without "delay" (Read 813 times) previous topic - next topic

anderscrib

I am trying to incorporate a "Throb" or sine wave PWM pulse to this code so that outside of a predetermined length, the lights will pulse. The first part of the code I did as a version of "blink without delay" and it works great. Would the "delay" in many sine wave codes affect the serial monitor output just as blink with delay does when using If/Else?
Code: [Select]
/*
* EZ rangefinder Distance Sensor
* prints distance and changes LED flash rate
* depending on distance from sensor
* if sensor range is greater than 500 cm then..all on
*/

const int sensorPin = 5;
const int ledPinA = 9; //pin connected to LED
const int ledPinB = 10; //pin connected to LED

long value = 0;
int cm = 0;

// Variables will change:
 
int ledState = LOW;             // ledState used to set the LED BWOD
long previousMillis = 0;        // will store last time LED was updated BWOD

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = cm;           // interval at which to blink (milliseconds) BWOD

void setup()
{
  Serial.begin(9600);
  pinMode(ledPinA, OUTPUT);   
  pinMode(ledPinB, OUTPUT); 
}

void loop()
{
  value = pulseIn(sensorPin, HIGH);
  cm = value / 20;   // pulse width is 58 microseconds per cm but modify
                     //this number to adjust blink speed relationship
  Serial.print(cm);
  Serial.print(',');
 
  if (cm < 500)
  {
   // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
   unsigned long currentMillis = millis();
 
   if(currentMillis - previousMillis > cm)
   {
     
    previousMillis = currentMillis; // save the last time you blinked the LED   
     if (ledState == LOW)// if the LED is off turn it on and vice-versa:
       ledState = HIGH;
     else
       ledState = LOW;

    // set the LED with the ledState of the variable:
     digitalWrite(ledPinA, ledState);
     digitalWrite(ledPinB, ledState);
   }
}

  else
  {
    digitalWrite(ledPinA, HIGH);
     digitalWrite(ledPinB, HIGH);
   
  }

  }


holmes4

Yes, after getting the right idea from blink without, do all the other stuff without delays as well.

Mark

PaulS

Quote
Would the "delay" in many sine wave codes affect the serial monitor output just as blink with delay does when using If/Else?

I'm going to disagree with holmes4. Using delay() does not "affect the serial monitor output". If affects WHEN you can send data to the output buffer, but that data is sent even during delay(), because sending and receiving serial data is interrupt driven, and delay() doesn't block interrupts from happening.

During an if/else or any other time.

This is not to say that delay() is a good idea, except for very short delays for debouncing switches. Even there, there are alternatives. Whether they are worth the effort depends on how time-critical your sketch is.

holmes4

Quote
I'm going to disagree with holmes4. Using delay() does not "affect the serial monitor output". If affects WHEN you can send data to the output buffer, but that data is sent even during delay(), because sending and receiving serial data is interrupt driven, and delay() doesn't block interrupts from happening.


Yes BUT Serial.print etc do act as delays. The processing of the sketch does not continue until Serial has completed the output.

Mark

PaulS

Quote
The processing of the sketch does not continue until Serial has completed the output.

Not on 1.0+, unless the output buffer gets full. Whether or not that happens has nothing to do with if/else statements. It was that contention that you appeared to agree with that I was disagreeing with.

anderscrib

Ok, Great info. Thank you Holmes4 and PaulS! My project needs to be quick as it is based on an objects changing distance.
All of the "breathing" or "sleep" codes I have seen have a delay in them..Any thoughts on a code without it?

PaulS

Quote
Any thoughts on a code without it?

Sure. Use the blink with delay philosophy.

Go Up