Pulsing LED is adjustable!

Here is the second installment of Arduino projects.

This time I have jumped several stages and am giving you two types of functions, serial monitoring and schematics using tinycad.

As I worked with this sketch I found that using the input values and a millisecond delay made it way to long and interval to brighten and dim.

I changed to microsecond and used the analogRead value as the interval.

I also found that if the delay value became zero, delay behaved badly. To fix this I put a 330 ohm resistor on the ground side of the potentiometer so that the analogRead would never be zero.

You will notice some code left in that appears to be not needed. You are right! Some lines are left over from what I tried to do first;.

If the notes in the sketch are not clear please let me know in the comments. I have left out the video, for now

adjustable_PULSE_LED_schematic

 

/*
Pulse an led on a pin. Go from dark to full bright in an adjustable interval then back down to dark in the same interval. The interval is adjusted via a potentiometer.
*/

int ledPin = 11; // led pin used for LED in entire program
int sensorPin = 0; // pin pot is tied to
int interval = 1000; // interval for change in microsecond, 100000 is one second up and one second down
int sensor1 = 0; // sensor raw
int sensor1a = 0; // sensor expanded
int waitforit = (interval/255); //delay this much between each increment

void setup ()  {
Serial.begin(9600); //start serial comm
pinMode(ledPin, OUTPUT); //initialize ledPin for OUTPUT
pinMode(sensorPin, INPUT); // initialize sensorPin for INPUT
}

void loop()    {

waitforit = readSensor(); //
/*
The Serial.print lines are a debugging aid printing out the value used to count up and down in the bright functions
*/
Serial.print(“waitforit is “);
Serial.println(waitforit, DEC);
brightup(); //call the brightup function to raise the led brightness from 0 to 255
brightdown();  //call the brightdown function to lower the led brightness from 255 to 0

}
/*****************************************************************************************
additional functions defined below
*****************************************************************************************/
int readSensor () {
/* This is the subroutine to read the potentiometer and return the value 0 – 1023

*/

sensor1 = analogRead(sensorPin); // read sensor

/*
The Serial.print lines are a debugging aid printing out the value read from the potentiometer
a 330 ohm resistor was added to the ground leg of the potentiometer as a current limiter and to disallow
a reading of “0”. Zero as a value for delayMicrosecond statement causes the loop to behave abnormally
*/

Serial.print(“Sensor1 is “);
Serial.println(sensor1, DEC);

sensor1a = sensor1 * 1; //stretch the interval out, if needed

sensor1a = sensor1a / 1 ; //divide value by a number for use as the count delay waitforit

return sensor1a; // return the adjusted value for the interval
}

void brightup () {
/* this is the subroutine to raise the brightness from zero to 255 over a specific delay
brightness is declared and initialized in this function
waitforit was declared and initialized for the entire program first
*/

for (int brightness = 0; brightness < 255; brightness ++){

analogWrite(ledPin,brightness); //write the brightness value

delayMicroseconds(waitforit); // delay 1/255 of the interval to maximum brightness
}
}

void brightdown () {
/* this is the subroutine to lower the brightness from zero to 255 over a specific delay
brightness is declared and initialized in this function
waitforit was declared and initialized for the entire program first
*/

for (int brightness = 255; brightness > 0; brightness — ){

analogWrite(ledPin,brightness); //write the brightness value

delayMicroseconds(waitforit); // delay 1/255 of the interval to maximum brightness
}
}

About jcoffey

https://profiles.google.com/jerry.coffey/posts
This entry was posted in Arduino, commentary and tagged , , , . Bookmark the permalink.