Program Light Intensity LED and analysis

            Make a series circuit as shown above. The plan, which is above pushbutton start and increase LED brightness, while the pushbutton down to lower levels of LED brightness and extinguished:


  1.  As usual, set up a LED and its LED resistor. Connect the positive leg to pin 8 Arduino.
  2. Then the negative leg is connected to the LED resistor menujuGND.
  3. Prepare two pushbutton. Pushbutton the first (top) connected to GND and to pin 2 on the Arduino board.
  4. Then the second pushbutton (bottom) is connected to GND and pin 3 on the Arduino board.

Program Intensity LED Light


// Free tutorial Arduino
// www.ioisalman.com
//

// Pin 2 & 3 as a digital input
const int pinBt1 = 2;
const int pinBt2 = 3;
// Remember, pin 9 support PWM
const int pinLED = 9;

void setup () {
   pinMode (pinBt1, INPUT);
   pinMode (pinBt2, INPUT);
   pinMode (pinLED, OUTPUT);
  
   digitalWrite (pinBt1, HIGH);
   digitalWrite (pinBt2, HIGH);
}

int brightness = 0;
void loop () {
   if (digitalRead (pinBt1) == LOW) {
     // If the pushbutton is pressed
     // Add brightness value
     brightness ++;
   } Else if (digitalRead (pinBt2) == LOW) {
     // If pressed pushbutton2
     // Subtract the value of brightness
     brightness--;
   }
  
   // Brightness is limited between 0-255
   // If it is below 0, then replace with 0
   // If above 255, then replace with 255
   brightness = constrain (brightness, 0, 255);
  
   // PinLED rated between 0-255
   analogWrite (pinLED, brightness);
   // Delay so that the changes gradually
   delay (20);
}

            There are three sections in the Sketch on top of which need to be explained in more detail, namely on lines 36, 39, and 41.


// Brightness is limited between 0-255
   // If below 0, then replace with 0
   // If above 255, then replace with 255
   brightness = constrain (brightness, 0, 255);

            At line 36, we find a new function, namely constrain (). Constrain function () is used to keep a fixed value in the specified range. In this case, the specified range is between 0 - 255. Suppose brightness value is smaller than 0, it will be changed to 0, but if the brightness value is greater than 255, it will be changed to 255.

            To understand more about the function constrain (), please note the content of constraint function () below:

int constrain(int value, int min, int max){
    if(value > max){
        value = max;
    }
    if(value < min){
        value = min;
    }
    return value;
}

            The type of the function is int (integer), meaning that the function will return an integer value when executed with a value adjusted value (return value).

            If the note function loop () and setup function () is not int, but the void. Void function type different from the type int, void function does not return any value, so if the note, there is no return command in the function of type void. Yep, so a glimpse of integer function types and void.

            Furthermore, because the brightness value is always between 0-255, then when it is written to pinLED will always lapse between 0-255 (note line 39).


// PinLED rated between 0-255
   analogWrite (pinLED, brightness);


            AnalogWrite function () is used to provide data PWM or analog data. analogWrite () can write data at an interval between 0v to + 5v at pin INPUT. In contrast to digitalWrite () which can only write HIGH or LOW, or + 5v or 0v only.


// Delay so that the changes gradually
   delay (20);

            Delay (20) serves to adjust the duration of the LED light intensity changes. If the delay (20) we lose, then the LED will direct live or die immediately when the button is pressed. If the lower the value, the change in the intensity will be faster, and vice versa, if we exaggerated the value, the change in intensity will be longer with the record we should hold when pressing the pushbutton.

            What happens if we press both buttons at the same pushbutton? What will happen with the LED? Please find answers and explanations

Subscribe to receive free email updates:

0 Response to "Program Light Intensity LED and analysis"

Post a Comment