4 PRAKTEK Rev 3agustus17

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

/* PRAKTEK 1- KEDIP dengan delay

Arduino
*/

// 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; // will store last time LED was


updated

const long interval = 200; // interval at which to blink


(milliseconds)

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; // the pin that the LED is attached to


int brightness = 0; // how bright the LED is
int fadeAmount = 5; // 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; // the number of the pushbutton pin


const int ledPin = 13; // the number of the LED pin

int buttonState = 0; // variable for reading the pushbutton status

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop() {
buttonState = digitalRead(buttonPin);

if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

2
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

/* PRAKTEK 5 – PUSH BUTTON STATE


Led akan nyala setiap push button di tekan tiga kali

*/

const int buttonPin = 2; // the pin that the pushbutton is attached


to
const int ledPin = 13; // the pin that the LED is attached to

int buttonPushCounter = 0; // counter for the number of button presses


int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button

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

void loop() {
buttonState = digitalRead(buttonPin);

if (buttonState != lastButtonState) {

if (buttonState == HIGH) {

buttonPushCounter++;

}
else {

}
delay(50);
}

lastButtonState = buttonState;

if (buttonPushCounter % 3 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}

3
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/* PRAKTEK 6 – ANALOG SENSOR
Membaca tegangan input analog di A0
Input diperaga dengan potentio meter
*/

int sensorValue = 0; // value read from the pot


int outputValue = 0; // value output to the PWM (analog out)
int analogOutPin = 13;
int analogInPin = 1;

void setup() {
// Serial.begin(9600);
pinMode(analogOutPin, Output);
pinMode(analogInPin, Input);
}

void loop() {
sensorValue = analogRead(analogInPin);
outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(analogOutPin, outputValue);

// print the results to the serial monitor:


// Serial.print("sensor = " );
// Serial.print(sensorValue);
// Serial.print("\t output = ");
// Serial.println(outputValue);

// wait 2 milliseconds before the next loop


// for the analog-to-digital converter to settle
// after the last reading:
delay(2);
}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/* PRAKTEK 7 – FADE LED

pin10 sampai pin 13 akan nyala pelahan lahan dan redup pelahan lahan
bergantian
*/
const int lowestPin = 10;
const int highestPin = 13;

void setup() {
// set pins 2 through 13 as outputs:
for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}

void loop() {
// iterate over the pins:

4
for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++)
{
// fade the LED on thisPin from off to brightest... bertambah nyala :
for (int brightness = 0; brightness < 255; brightness+=5)
{
analogWrite(thisPin, brightness);
delay(20);
}
// fade the LED on thisPin from brithstest to off: bertambah redup
for (int brightness = 255; brightness >= 0; brightness-=5) {
analogWrite(thisPin, brightness);
delay(20);
}
// pause between LEDs:
delay(100);
}
}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/* PRAKTEK 7b – FADING LED hanya satu led

*/

int ledPin = 11; // LED connected to digital pin 13

void setup() {
// nothing happens in setup
}

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

*/

// These constants won't change:

5
const int sensorPin = A0; // pin that the sensor is attached to
const int ledPin = 13; // pin that the LED is attached to

// variables:
int sensorValue = 0; // the sensor value
int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value

void setup() {
// led pin 13 nyal pada st mulai klibrasi (waktu 5detik)
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);

// calibrate during the first five seconds


while (millis() < 5000)
{
sensorValue = analogRead(sensorPin);

// record the maximum sensor value


// misalnya selama 5 detik sensor valuenya 5 sampai 10
// sesormax =10 sensormin=5
if (sensorValue > sensorMax)
{
sensorMax = sensorValue;
}

// record the minimum sensor value


if (sensorValue < sensorMin)
{
sensorMin = sensorValue;
}
}

// 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);
}

6
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

/* 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
*/

// these constants won't change:


const int analogPin = A0; // the pin that the potentiometer is attached
to
const int ledCount = 10; // jumlah led nya

int ledPins[] = {
2, 3, 4, 5, 6, 7, 8, 9, 10, 11
}; // disini kita pakai 10 led

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 sesui 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);
}
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

You might also like