4 PRAKTEK Rev 3agustus17
4 PRAKTEK Rev 3agustus17
4 PRAKTEK Rev 3agustus17
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);
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/* 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
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop()
{
1
/* PRAKTEK 3 - REDUP / Fade
*/
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
*/
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
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
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
*/
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);
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/* 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
*/
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
*/
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);
// selesai kalibrasi
digitalWrite(13, LOW);
}
void loop() {
// baca nilai sensor :
sensorValue = analogRead(sensorPin);
// contrain mebuat sensor value dibatas nya...> 255 jadi 255,jika <0
jadi 0
sensorValue = constrain(sensorValue, 0, 255);
analogWrite(ledPin, sensorValue);
}
6
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
led akan nyala dalam bentuk BAR sesuai dgn nilai analog inputnya
analog inputnya kita pakai potentiometer
http://www.arduino.cc/en/Tutorial/BarGraph
*/
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);