Pages: [1]   Go Down
Author Topic: PWM sine wave without "delay"  (Read 769 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
* 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);
   
  }

  }

Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 2214
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Mark
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 2214
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Any thoughts on a code without it?
Sure. Use the blink with delay philosophy.
Logged

Pages: [1]   Go Up
Jump to: