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