Project LED Arduino UNO

16.27 Posted by Ali Mahfud , No comments
Praktikum Arduino UNO dengan LED

1. Blink
/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

2. Fade


// Fading LED
int value = 0; // variable to keep the actual value
int ledpin = 9; // light connected to digital pin 9
void setup()
{
// nothing for setup
}
void loop()
{
  for(value = 0 ; value <= 255; value+=5) // fade in (from min to max)
  {
  analogWrite(ledpin, value); // sets the value (range from 0 to 255)
  delay(30); // waits for 30 milli seconds to see the dimming effect
}
for(value = 255; value >=0; value-=5) // fade out (from max to min)
{
  analogWrite(ledpin, value);
  delay(30);
  }
}

3. Candle Light

/*
* CandleLight
*
* Use random numbers to emulate a flickering candle with a
* PWM'd LED
*
*/
int ledPin = 6; // select the pin for the LED
int val = 0; // variable that holds the current LED brightness
int delayval = 0; // variable that holds the current delay time
void setup() 
{
  randomSeed(0); // initialize the random number generator
  pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
}

void loop() 
{
  val = random(100,255); // pick a random number between 100 - 255
  analogWrite(ledPin, val); // set the LED brightness
  delayval = random(50,150); // pick a random num between 50, 150
  delay(delayval); // delay that many milliseconds
}

4. Moodlight

/*
* RGB_LEDs sketch
* RGB LEDs driven from analog output ports
*/
const int redPin = 6; // choose the pin for each of the LEDs
const int greenPin = 5;
const int bluePin = 3;
const boolean invert = false; // set true if common anode, false if
// common cathode
int color = 0; // a value from 0 to 255 representing the hue
int R, G, B; // the Red Green and Blue color components

void setup()
{
// pins driven by analogWrite do not need to be declared as outputs
}

void loop()
{
  int brightness = 255; // 255 is maximum brightness
  hueToRGB( color, brightness); // function to convert hue to RGB
  // write the RGB values to the pins
  analogWrite(redPin, R);
  analogWrite(greenPin, G);
  analogWrite(bluePin, B );
  color++; // increment the color
  if(color > 255)
  color = 0;
  delay(10);
}

// function to convert a color to its Red, Green, and Blue components.
void hueToRGB( int hue, int brightness)
{
  unsigned int scaledHue = (hue * 6);
  unsigned int segment = scaledHue / 256; // segment 0 to 5 around
  // the color wheel
  unsigned int segmentOffset = scaledHue - (segment * 256); // position
  // within the seg
  unsigned int complement = 0;
  unsigned int prev = (brightness * ( 255 - segmentOffset)) / 256;
  unsigned int next = (brightness * segmentOffset) / 256;
  if(invert)
  {
    brightness = 255-brightness;
    complement = 255;
    prev = 255-prev;
    next = 255-next;
  }
  
  switch(segment ) {
        case 0: // red
    R = brightness;
    G = next;
    B = complement;
    break;
    
    case 1: // yellow
    R = prev;
    G = brightness;
    B = complement;
    break;
    
    case 2: // green
    R = complement;
    G = brightness;
    B = next;
    break;
    
    case 3: // cyan
    R = complement;
    G = prev;
    B = brightness;
    break;
    
    case 4: // blue
    R = next;
    G = complement;
    B = brightness;
    break;
    
    case 5: // magenta
    default:
    R = brightness;
    G = complement;
    B = prev;
    break;
  }
}


Sumber : Catatan Praktikum

0 Comment:

Posting Komentar