You are on page 1of 8

Experiment 1

// C++ code
//
int LEDpin=11;
void setup()
{
pinMode(LEDpin, OUTPUT);
}

void loop()
{
digitalWrite(LEDpin, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LEDpin, LOW);
delay(1000); // Wait for 1000 millisecond(s)
}
const int ledPin = 2; // Built-in LED on most Arduino
boards

pinMode(ledPin,OUTP
UT);

void
loop()
{ //Increasebrightnes
sstepwise
for (int
brightness
0; brightness <= 255; brightness++)
analogWrite(ledPin, brightness); // Set the LED
brightness delay(1000); // 1-second delay

void setup()
{
}

}
}

// Decrease brightness
stepwise
for (int brightness 255; brightness >= 0;
brightness--) {
analogWrite(ledPin, brightness); // Set the LED
brightness delay(1000); // 1-second delay

const int ledPin-ulit-in LED on most Arduino boards


void
setup() {
pinMode(ledPin,OUTP
UT);

Voidloop() {
// Decrease brightness
stepwise

for (int brightness)


255; brightness >=
0; brightness--) {
analogWrite(ledPin, brightness); // Set the LED brightness delay(1500);
// 1.5-second delay
}
const int ledPin
=

// Built-in LED on most Arduino boards


void setup()
{
}

pinMode(ledPin,
OUTPUT);

void loop()
{
// Increase brightness to maximum
level
for (int brightness = 0; brightness <= 255;
brightness++) { analogWrite(ledPin, brightness); //
Set the LED brightness, delay(1000); // 1-second
delay

// Decrease brightness to minimum


level
for (int brightness - 255; brightness >= 0;
brightness--) { analogWrite(ledPin, brightness); //
Set the LED brightness delay(1000); // 1-second
delay
}
}
int led = 3; // the pin that the LED is attached to
int brightness =0; // how bright the LED is
int incrementfactor = 10; // how many points to fade the
LED by
int desired_brightness = 255 ;
int extra_delay = 1000;

void setup() { // declare pin 9 to be an output:


pinMode(led, OUTPUT);
analogWrite(led, desired_brightness);
}
void loop() {
analogWrite(led, desired_brightness);
brightness=brightness+incrementfactor;
if (brightness==desired_brightness) {
delay(extra_delay);
}
}
const int buttonPin = 2; // number of the
button pin
const int ledPin = 9; // number of the
LED pin

float brightness = 0; // brightness of


LED starts at 0
int fadeAmount = 51; // fadeAmount is
equal to 20% of 255(max LED brightness)
float buttonPressedCounter = 0.0; // timer set to
0 whe depressing button
int buttonState = 0;

void setup() {
// set button as input
pinMode(buttonPin, INPUT);
// set LED as output
pinMode(ledPin, OUTPUT);
// init serialize communication at 9600 bits per
second
Serial.begin(9600);
}

void loop() {
// activate when button is being held down
if (buttonState == HIGH) {
// on press, when counter is greater than 33,
assume button is being held down
if (buttonPressedCounter > 33) {
// button was held down for longer than 33
if (brightness < 255) {
Serial.print("increasing brightness: ");
Serial.println(brightness);
brightness = brightness + 5;
analogWrite(ledPin, brightness);
}
} else {
buttonPressedCounter ++;
Serial.print("holding: ");
Serial.println(buttonPressedCounter);
}
} else if (buttonState == LOW) {
// on release if button pressed counter was
less than 33, assume it was a short press
if ((buttonPressedCounter < 33) &
(buttonPressedCounter != 0)) {
Serial.print("lower LED: ");
if (brightness - fadeAmount < 0.0) {
brightness = 0;
} else {
brightness = brightness - fadeAmount;
}
Serial.println(brightness);
analogWrite(ledPin, brightness);
}
buttonPressedCounter = 0;
}
}

You might also like