Bila Tombol 1 ditekan maka Lampu 1 menyala, dan bila Tombol 2 ditekan maka Lampu 2 menyala.
Bila Tombol 1 ditekan lagi maka Lampu 1 padam, dan bila Tombol 2 ditekan lagi maka Lampu 2 padam.
const int buttonPin1 = A0;
const int buttonPin2= A1;
const int ledPin1 = 2;
const int ledPin2 = 3;
int x = 1;
int xb = 1;
// This will store the last known state of the button
int oldButtonState1 = LOW;
int oldButtonState2 = LOW;
void setup() {
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
void loop()
{
// Get the current state of the button
int newButtonState1 = digitalRead(buttonPin1);
int newButtonState2 = digitalRead(buttonPin2);
// Has the button gone high since we last read it?
if (newButtonState1 == HIGH && oldButtonState1 == LOW) {
if (x == 0) {
// Toggle on
digitalWrite(ledPin1, HIGH);
x = 1;
} else {
// Toggle off
digitalWrite(ledPin1, LOW);
x = 0;
}
}
if (newButtonState2 == HIGH && oldButtonState2 == LOW) {
if (xb == 0) {
// Toggle on
digitalWrite(ledPin2, HIGH);
xb = 1;
} else {
// Toggle off
digitalWrite(ledPin2, LOW);
xb = 0;
}
}
// Store the button's state so we can tell if it's changed next time round
oldButtonState1 = newButtonState1;
oldButtonState2 = newButtonState2;
}
Selamat bereksperimen !
ConversionConversion EmoticonEmoticon