Antarmuka Switch Pada Arduino
1. Push Button
Source Code:
int ledPin = 13; // choose the pin for the LED int switchPin = 2; // choose the input pin (for a pushbutton int val = 0; // variable for reading the pin status void setup() { pinMode(ledPin, OUTPUT); // declare LED as output pinMode(switchPin, INPUT); // declare pushbutton as input } void loop() { val = digitalRead(switchPin); // read input value if (val == LOW) { // check if the input is LOW digitalWrite(ledPin, LOW); // turn LED OFF: } else { digitalWrite(ledPin, HIGH); // turn LED ON: } }2. Swtich to Blink or Fade
Source Code:
/* Blink or fade switch Choose between Blink or Fade the LED connected to digital pin 9, when pressing a pushbutton attached to pin 2. The circuit: * LED attached from pin 9 to ground * pushbutton attached to pin 2 from +5V * 10K resistor attached to pin 2 from ground */ int ledPin = 9; // choose the pin for the LED int switchPin = 2; // choose the input pin (for a pushbutton int val = 0; // variable for reading the pin status int fadeval = 0; void setup() { pinMode(ledPin, OUTPUT); // declare LED as output pinMode(switchPin, INPUT); // declare pushbutton as input } void loop() { val = digitalRead(switchPin); // read input value if (val == HIGH) { // pushed button means do blinking digitalWrite(ledPin, LOW); // turn LED OFF: delay(50); digitalWrite(ledPin,HIGH); //turn LED ON: delay(50); } else { // else button isn't pressed so do fading for(fadeval = 0 ; fadeval <= 255 ; fadeval +=5) { // fade in analogWrite(ledPin, fadeval); // sets the value delay(10); } for(fadeval = 255; fadeval >= 0 ; fadeval -=5) { // fade out analogWrite(ledPin, fadeval); } } }3. Toggle
Source Code:
// Toggle switch /* The circuit: * LED attached from pin 13 to ground * pushbutton attached to pin 8 from +5V * 10K resistor attached to pin 8 from ground */ int switchPin = 8; int ledPin = 13; boolean lastButton = LOW; boolean ledOn = false; void setup() { pinMode(switchPin, INPUT); pinMode(ledPin, OUTPUT); } void loop() { if (digitalRead(switchPin) == HIGH && lastButton == LOW) { ledOn = !ledOn; lastButton = HIGH; } else { lastButton = digitalRead(switchPin); } digitalWrite(ledPin, ledOn); }
4. PWM Switch
Source Code:
// PWM Switch (with debounce) int switchPin = 8; int ledPin = 3; boolean lastButton = LOW; boolean currentButton = LOW; int ledLevel = 0; void setup() { pinMode(switchPin, INPUT); pinMode(ledPin, OUTPUT); } boolean debounce(boolean last) { boolean current = digitalRead(switchPin); if (last != current) { delay(5); current = digitalRead(switchPin); } return current; } void loop() { currentButton = debounce(lastButton); if (lastButton == LOW && currentButton == HIGH) { ledLevel = ledLevel + 51; } lastButton = currentButton; if (ledLevel > 255) ledLevel = 0; analogWrite(ledPin, ledLevel); }
5. Interactive Traffic Lights
Source Code:
// Interactive Traffic Lights int carRed = 12; // assign the car lights int carYellow = 11; int carGreen = 10; int pedRed = 9; // assign the pedestrian lights int pedGreen = 8; int button = 2; // button pin int crossTime = 5000; // time allowed to cross unsigned long changeTime; // time since button pressed void setup() { pinMode(carRed, OUTPUT); pinMode(carYellow, OUTPUT); pinMode(carGreen, OUTPUT); pinMode(pedRed, OUTPUT); pinMode(pedGreen, OUTPUT); pinMode(button, INPUT); // button on pin 2 digitalWrite(carGreen, HIGH); // turn on the green light digitalWrite(pedRed, HIGH); } void loop() { int state = digitalRead(button); // check if button is pressed and it is over 5s since last button press if (state == HIGH && (millis() - changeTime) > 5000) { changeLights(); // Call the function to change the lights } } void changeLights() { digitalWrite(carGreen, LOW); // green off digitalWrite(carYellow, HIGH); // yellow on delay(2000); // wait 2 seconds digitalWrite(carYellow, LOW); // yellow off digitalWrite(carRed, HIGH); // red on delay(1000); // wait 1 second till its safe digitalWrite(pedRed, LOW); // ped red off digitalWrite(pedGreen, HIGH); // ped green on delay(crossTime); // wait for preset time period // flash the ped green for (int x=0; x<10; x++) { digitalWrite(pedGreen, HIGH); delay(250); digitalWrite(pedGreen, LOW); delay(250); } // turn ped red on digitalWrite(pedRed, HIGH); delay(500); digitalWrite(carYellow, HIGH); // yellow on digitalWrite(carRed, LOW); // red off delay(1000); digitalWrite(carGreen, HIGH); digitalWrite(carYellow, LOW); // yellow off changeTime = millis(); // record the time since last change of // lights }
0 Comment:
Posting Komentar