Minggu, 26 Juli 2020

Kodingan Dasar Pada Arduino Mudah



Postingan kali ini yaitu tentang Koding Dasar Untuk Arduino yuk langsung saja disimak:


1. LED Blink
 Codingnya:

int ledPin = 8;
void setup()
{
  pinMode(ledPin,OUTPUT);
}
void loop()
{
  digitalWrite(ledPin, HIGH);
  delay(100);
  digitalWrite(ledPin, LOW);
  delay(100);
}

2. Push Button

 Codingnya:

int inPin = 2;
int outPin = 13;
int state = HIGH;
int reading;
int previous = LOW;
long time = 0;
long debounce = 200;
void setup ()
{
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
}
void loop(){
  reading = digitalRead(inPin);
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
    state = LOW;
    else
    state = HIGH;
    time = millis();
  }
  digitalWrite(outPin, state);
  previous = reading;
  }

3. Lampu Berjalan

  Codingnya:

int timer = 100;

int pins[] = {4, 5, 6, 7};

int numPins = 4;

void setup() {

  int i;

  for (i=0; i<numPins; i++)

  pinMode(pins[i], OUTPUT);

}

void loop() {
  int i;
  for (i=0; i<numPins; i++) {
    digitalWrite(pins[i], HIGH);
    delay(timer);
    digitalWrite(pins[i], LOW);
  }
  for (i=numPins-1; i>=0; i--) {
    digitalWrite(pins[i], HIGH);
    delay(timer);
    digitalWrite(pins[i], LOW);
  }
}
 

4. Menerangkan dan Meredupkan lampu LED dengan 2 Push Button

Codingnya:

const int pinBt1 = 2;
const int pinBt2 = 3;
const int pinLED = 9;
void setup() {
  pinMode(pinBt1, INPUT);
  pinMode(pinBt2, INPUT);
  pinMode(pinLED, OUTPUT);
  digitalWrite(pinBt1, HIGH);
  digitalWrite(pinBt2, HIGH);
}
int brightness = 0;
void loop() {
  if(digitalRead(pinBt1) == LOW) {
        brightness++;
  } else if(digitalRead(pinBt2) == LOW) {
        brightness--;
  }
  brightness = constrain(brightness, 0, 255);
  analogWrite(pinLED, brightness);
  delay(20);
}

5. Lampu LED hidup berurutan (loop)


Codingnya:

const int pinLED1 = 3;
const int pinLED2 = 4;
const int pinLED3 = 5;
const int pinLED4 = 6;
const int pinLED5 = 7;
void setup() {
  pinMode(pinLED1, OUTPUT);
  pinMode(pinLED2, OUTPUT);
  pinMode(pinLED3, OUTPUT);
  pinMode(pinLED4, OUTPUT);
  pinMode(pinLED5, OUTPUT);
}
void loop() {
  for( int i=0; i<6; i++ ) {
    if( i==1 ) {
      digitalWrite(pinLED1, HIGH);
      digitalWrite(pinLED2, LOW);
      digitalWrite(pinLED3, LOW);
      digitalWrite(pinLED4, LOW);
      digitalWrite(pinLED5, LOW);
    } else if( i==2 ) {
      digitalWrite(pinLED1, HIGH);
      digitalWrite(pinLED2, HIGH);
      digitalWrite(pinLED3, LOW);
      digitalWrite(pinLED4, LOW);
      digitalWrite(pinLED5, LOW);
    } else if( i==3 ) {
      digitalWrite(pinLED1, HIGH);
      digitalWrite(pinLED2, HIGH);
      digitalWrite(pinLED3, HIGH);
      digitalWrite(pinLED4, LOW);
      digitalWrite(pinLED5, LOW);
    } else if( i==4 ) {
      digitalWrite(pinLED1, HIGH);
      digitalWrite(pinLED2, HIGH);
      digitalWrite(pinLED3, HIGH);
      digitalWrite(pinLED4, HIGH);
      digitalWrite(pinLED5, LOW);
    } else if( i==5 ) {
      digitalWrite(pinLED1, HIGH);
      digitalWrite(pinLED2, HIGH);
      digitalWrite(pinLED3, HIGH);
      digitalWrite(pinLED4, HIGH);
      digitalWrite(pinLED5, HIGH);
    } 


     else {
      digitalWrite(pinLED1, LOW);
      digitalWrite(pinLED2, LOW);
      digitalWrite(pinLED3, LOW);
      digitalWrite(pinLED4, LOW);
      digitalWrite(pinLED5, LOW);
      delay(4000);
    }
    delay(1000);
  }
}

Terima kasih untuk tutorial kali ini, semoga bermanfaat, jangan lupa kunjungi lanjutan dari blog ini