kAi_homework_1

I tried to switch LEDs by button.

One LED is with fading, controlled by code. The other one is just physics. Some magical thing was happened that I could turn on/off the LED by air…. LOL

My code:

const int buttonPin = 7; // the number of the pushbutton pin
const int ledPin = 9; // the number of the LED pin
int brightness = 0;
int fadeAmount = 5;

// variables will change:
int buttonState; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == LOW) {
// turn LED on:
digitalWrite(ledPin, HIGH);
analogWrite(ledPin, brightness);
brightness = brightness + fadeAmount;

if(brightness == 0 || brightness == 255){
fadeAmount = -fadeAmount;
}
delay(15);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}

}

 

Leave a Reply

Your email address will not be published.