Mengubah Arah Motor DC Dengan 1 Push Button Menggunakan Arduino

 


1. Persiapan yang disiapkan dan diperlukan

  • Arduino Uno
  • Motor Driver H-Bridge L298N
  • Motor DC
  • 2 Buah Baterai 18650 di Seri
  • Breadboard
  • Push Button
  • Resistor 1K Ohm
  • Kabel Jumper


2.  Rancangan 




3. Coding

 /*
 *  Mengubah Arah Motor DC 
 */

byte tombol = 2;
boolean nilai_tombol, flag = 0;
int count;

byte in1 = 6;
byte in2 = 5;

void setup() {
  Serial.begin(9600);
  pinMode(tombol, INPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
}

void loop() {
  
  nilai_tombol = digitalRead(tombol);
  if ((nilai_tombol == 1) && (flag = 0)) {
    count++;
    Serial.println("Count: " + String(count));
    if (count == 1) {
      digitalWrite(in1, HIGH);
      digitalWrite(in1, LOW);
    }
    if (count == 2) {
      digitalWrite(in1, LOW);
      digitalWrite(in1, HIGH);
    }
    if (count == 3) {
      digitalWrite(in1, LOW);
      digitalWrite(in1, LOW);
      count = 0;
    }
    flag = 1;
  }
  
  if (nilai_tombol == 0) {
    flag = 0;
  }
  
}