Arduino buton ile DC motor kontrol kodu nasıl yazılır?

KaosBeY

Centipat
Katılım
23 Ocak 2023
Mesajlar
59
Daha fazla  
Cinsiyet
Erkek
Merhaba. Arduino üzerinden 2 adet butonlu sağa ve sola döndürülebilen bir DC motor yaptım. Fakat ne kadar döneceğinin süresini ayarlamayı bilmiyorum.


Benim kullandığım kod bu;
Kod:
#İnclude <Keypad.h>
İnt motorpin1 = 5;
İnt motorpin2 = 6;
Const byte rows = 2;
Const byte cols = 1;
Char hexakeys[ROWS][COLS] = {{'2'}, {'1'}};
Byte rowpins[ROWS] = {3, 2};
Byte colpins[COLS] = {4};
Keypad customkeypad = keypad( makekeymap(hexakeys), rowpins, colpins, rows, cols);
Void setup()
{
 Serial. Begin(9600);
}

Void loop()
{
 Char customkey = customKeypad.getKey();

 Switch (customkey)
 {
 Case '1':
 Serial. Println("rotates to left");
 rotateLeft();
 Break;

 Case '2':
 Serial. Println("rotates to right");
 rotateRight();
 Break;
 }
}
Void rotateLeft()
{
 Digitalwrite(motorpin1, hıgh);
 Digitalwrite(motorpin2, Low);
}
Void rotateRight(){
 Digitalwrite(motorpin2, hıgh);
 Digitalwrite(motorpin1, Low);
}

Nasıl bir kod eklersem DC motorun dönmesini süreli hale getirebilirim?
 
Belki bu yardımcı olur.
 

Dosya Ekleri

  • A.png
    A.png
    82,1 KB · Görüntüleme: 62
Kod:
#include <Keypad.h>

int motorPin1 = 5;
int motorPin2 = 6;
const byte rows = 2;
const byte cols = 1;
char hexaKeys[rows][cols] = {{'2'}, {'1'}};
byte rowPins[rows] = {3, 2};
byte colPins[cols] = {4};
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, rows, cols);

void setup()
{
  Serial.begin(9600);
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
}

void loop()
{
  char customKey = customKeypad.getKey();

  switch (customKey)
  {
    case '1':
      Serial.println("Rotates to left");
      rotateLeft();
      break;

    case '2':
      Serial.println("Rotates to right");
      rotateRight();
      break;
  }
}

void rotateLeft()
{
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  delay(5000); // Motoru 5 saniye boyunca döndür
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
}

void rotateRight()
{
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin1, LOW);
  delay(5000); // Motoru 5 saniye boyunca döndür
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
}

ChatGPT ile dönme süresini 5 saniyeye yükselltim. Kullanmak isteyen arkadaşlar kullanabilir.
Sorunumu çözdüğünüz için teşekkür ederim.
 

Technopat Haberler

Geri
Yukarı