PUSH BUTTON PART 2. Membahas mengenai bagaimana cara membuat tombol atau push button untuk menyalakan lampu LED dengan delay menggunakan Arduino.
Membuat delay lampu menyala selama 10 detik dan 15 detik setelah tombol ditekan.
Video tutorial dibawah ini:
Untuk Script silahkan Copy paste script dibawah ini:
//set pin numbers
const int ledPin = 2; //const won't change
const int buttonPin1 = 4;
const int buttonPin2 = 5;
//variables will change
int buttonState1 = 0; //variables for reading the pushbutton status
int buttonState2 = 0; //variables for reading the pushbutton status
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); //initialize the LED pin as an output
pinMode(buttonPin1, INPUT); //initialize the pushbutton pin as an output
pinMode(buttonPin2, INPUT); //initialize the pushbutton pin as an output
}
void loop() {
buttonState1 = digitalRead(buttonPin1); //read the state of the pushbutton value
buttonState2 = digitalRead(buttonPin2); //read the state of the pushbutton value
if (buttonState1 == HIGH) { //check if the pushbutton is pressed
//if it is, the buttonState1 is HIGH
digitalWrite(ledPin, HIGH); //turn LED on
Serial.println("LED ON 10 Detik");
delay(10000);
}
else {
digitalWrite(ledPin, LOW); // turn LED off
Serial.println("LED OFF -------");
}
if (buttonState2 == HIGH) { //check if the pushbutton is pressed
//if it is, the buttonState2 is HIGH
digitalWrite(ledPin, HIGH); //turn LED on
Serial.println("LED ON 15 Detik");
delay(15000);
}
else {
digitalWrite(ledPin, LOW); // turn LED off
Serial.println("LED OFF -------");
}
}
Comments
Post a Comment