/* PRAKTEK 1- KEDIP Arduino */
dengan delay
// the setup function runs once when you press reset or power the board void setup() { // initialize digital pin 13 as an output. pinMode(13, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level) delay(500); // wait for a second digitalWrite(13, LOW); // turn the LED off by making the voltage LOW delay(500); // wait for a second }
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ /* PRAKTEK 2 - Blink tidak memakai delay ,memakai millis() */ const int ledPin = 13; // the number of the LED pin int ledState = LOW; // ledState used to set the LED unsigned long previousMillis = 0; updated const long interval = 200; (milliseconds)
// will store last time LED was // interval at which to blink
void setup() { pinMode(ledPin, OUTPUT); } void loop() { unsigned long currentMillis = millis();// number of millisecond if(currentMillis - previousMillis >= interval) { previousMillis = currentMillis; if (ledState == LOW) ledState = HIGH; else ledState = LOW; digitalWrite(ledPin, ledState); } } ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1
/* PRAKTEK 3 - REDUP / Fade */ int led = 13; int brightness = 0; int fadeAmount = 5;
// the pin that the LED is attached to // how bright the LED is // how many points to fade the LED by
void setup() { pinMode(led, OUTPUT); } void loop() { analogWrite(led, brightness); brightness = brightness + fadeAmount; if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount ; } delay(30); } ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ /* PRAKTEK 4 – PUSH BUTTON Melihat hubungan input push botton dgn output led */ const int buttonPin = 2; const int ledPin = 13; int buttonState = 0;
// push botton di pin 2 // LED di pin 13 // variable buttonstate
void setup() { pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT); } void loop() { buttonState = digitalRead(buttonPin); // baca state input pin 2 if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); else { // turn LED off: digitalWrite(ledPin, LOW); }
// pin2 high pin13 output juga High
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
/* PRAKTEK 5 – PUSH BUTTON STATE Led akan nyala setiap push button di tekan tiga kali */ const int buttonPin = 2; const int ledPin = 13;
// push botton di pin2 // led dipin13
int buttonPushCounter = 0; int buttonState = 0; int lastButtonState = 0;
// counter utk pushbotton // catatan state pushbotton // catatan state pushbutton seblmnya
void setup() { pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT); } void loop() { buttonState = digitalRead(buttonPin); // baca input if (buttonState != lastButtonState) {
// jika H ke L counte++
if (buttonState == HIGH) { buttonPushCounter++; } else { } delay(50); } lastButtonState = buttonState; if (buttonPushCounter % 3 == 0) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); }
// modulo tiga (sisa hsl bagi)
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ /* PRAKTEK 6 – ANALOG SENSOR Membaca tegangan input analog di A0 Input diperaga dengan potentio meter
3
*/ int int int int
sensorValue = 0; outputValue = 0; analogOutPin = 13; analogInPin = 1;
// nilai dari input analog // nilai output
void setup() { // Serial.begin(9600); pinMode(analogOutPin, Output); pinMode(analogInPin, Input); } void loop() { sensorValue = analogRead(analogInPin); outputValue = map(sensorValue, 0, 1023, 0, 255);// di MAPping analogWrite(analogOutPin, outputValue); //print hasil ke serial monitor // aktifkan serial monitor dgn IDE arduino di TOOL serial monitor Serial.print("sensor = " ); Serial.print(sensorValue); Serial.print("\t output = "); Serial.println(outputValue); delay(2); } ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ /* PRAKTEK 7 – FADE LED pin10 sampai pin 13 akan nyala pelahan lahan bergantian */ const int lowestPin = 10; const int highestPin = 13;
dan redup pelahan lahan
void setup() { // set pins 2 through 13 outputs: for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++) { pinMode(thisPin, OUTPUT); } } void loop() { for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++) { // proses terang for (int brightness = 0; brightness < 255; brightness+=5) { analogWrite(thisPin, brightness); delay(20); }
4
// Proses redup for (int brightness = 255; brightness >= 0; brightness-=5) { analogWrite(thisPin, brightness); delay(20); } delay(100); } } ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ /* PRAKTEK 7b – FADING LED hanya satu led */ int ledPin = 13;
// LED connected to digital pin 13
void setup() { pinMode(ledPin, OUTPUT); } void loop() { for (int brightness = 0; brightness < 255; brightness++) { analogWrite(ledPin, brightness); delay(20); } for (int brightness = 255; brightness >= 0; brightness--) { analogWrite(ledPin, brightness); delay(20); } } ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ /* PRAKTEK 8 - Calibration kalibrasi nilai sensor yg dalam hal ini potentiometer kalibrasi dilakukan dalam waktu 5detik pada saat di on kan */ const int sensorPin = A0; const int ledPin = 13;
// pin that the sensor is attached to // pin that the LED is attached to
// variables: int sensorValue = 0; int sensorMin = 1023; int sensorMax = 0;
// the sensor value // minimum sensor value // maximum sensor value
5
void setup() { // led pin 13 nyala pada saat mulai klibrasi (waktu 5detik) pinMode(13, OUTPUT); digitalWrite(13, HIGH); // loop kalibrasi 5detik while (millis() < 5000) { sensorValue = analogRead(sensorPin); // misalnya selama 5 detik sensor valuenya // sensormax =10 sensormin=5 if (sensorValue > sensorMax) { sensorMax = sensorValue; } if (sensorValue < sensorMin) { sensorMin = sensorValue; }
5 sampai 10
} // selesai kalibrasi digitalWrite(13, LOW); } void loop() { // baca nilai sensor : sensorValue = analogRead(sensorPin); // instuksi map merubah nilai sensor min ...sensor max 0-255 sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255); // contrain mebuat sensor value dibatas nya...> 255 jadi 255,jika <0 jadi 0 sensorValue = constrain(sensorValue, 0, 255); analogWrite(ledPin, sensorValue); }
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ /* PRAKTEK 9 - LED bar graph led akan nyala dalam bentuk BAR sesuai dgn nilai analog inputnya analog inputnya kita pakai potentiometer http://www.arduino.cc/en/Tutorial/BarGraph */
6
const int analogPin = A0; const int ledCount = 10;
// the pin analog input // jumlah led nya
int ledPins[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; // disini kita pakai 10 led array void setup() { // set pin2 sd 11 sebagai output for (int thisLed = 0; thisLed < ledCount; thisLed++) { pinMode(ledPins[thisLed], OUTPUT); } } void loop() { // baca potentiometer: int sensorReading = analogRead(analogPin); // map hasil nya sesuai dgn jumlah led: int ledLevel = map(sensorReading, 0, 1023, 0, ledCount); // nyalakan led sesuai pembacaan sensornya: for (int thisLed = 0; thisLed < ledCount; thisLed++) { // nyala kan pin yg lebih kecil dari led level if (thisLed < ledLevel) { digitalWrite(ledPins[thisLed], HIGH); } // off kan pin yg lebih besar atau sama led level: else { digitalWrite(ledPins[thisLed], LOW); } } } ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7