Advertisement
Victoryus82

DoIt_l298d_teszt

Jul 10th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Motor A
  2. int motor1Pin1 = 13; //sárga
  3. int motor1Pin2 = 12; //narancs
  4. int motor1En = 26; //zöld
  5.  
  6. // Motor B
  7. int motor2Pin1 = 14; //
  8. int motor2Pin2 = 27;
  9. int motor2En = 25; //kék
  10.  
  11. // Setting PWM properties
  12. const int freq = 30000;
  13. const int pwmChannel = 0;
  14. const int resolution = 8;
  15. int dutyCycle = 200;
  16.  
  17. void setup() {
  18.   // sets the pins as outputs:
  19.   pinMode(motor1Pin1, OUTPUT);
  20.   pinMode(motor1Pin2, OUTPUT);
  21.   pinMode(motor2Pin1, OUTPUT);
  22.   pinMode(motor2Pin2, OUTPUT);
  23.  
  24.   // configure LED PWM functionalitites
  25.   ledcSetup(pwmChannel, freq, resolution);
  26.  
  27.   // attach the channel to the GPIO to be controlled
  28.     ledcAttachPin(motor1En, pwmChannel);
  29.     ledcAttachPin(motor2En, pwmChannel);
  30.  
  31.   Serial.begin(115200);
  32.  
  33.   // testing
  34.   Serial.print("Testing DC Motor...");
  35. }
  36.  
  37. void loop() {
  38. /*
  39.   // Move the DC motor 1 forward at maximum speed
  40.   Serial.println("Moving Forward");
  41.   digitalWrite(motor1Pin1, LOW);
  42.   digitalWrite(motor1Pin2, HIGH);
  43.   delay(2000);
  44.  
  45.   // Stop the DC motor 1
  46.   Serial.println("Motor stopped");
  47.   digitalWrite(motor1Pin1, LOW);
  48.   digitalWrite(motor1Pin2, LOW);
  49.   delay(1000);
  50.  
  51.   // Move DC motor 1 backwards at maximum speed
  52.   Serial.println("Moving Backwards");
  53.   digitalWrite(motor1Pin1, HIGH);
  54.   digitalWrite(motor1Pin2, LOW);
  55.   delay(2000);
  56.  
  57.   // Stop the DC motor 1
  58.   Serial.println("Motor stopped");
  59.   digitalWrite(motor1Pin1, LOW);
  60.   digitalWrite(motor1Pin2, LOW);
  61.   delay(1000);
  62.  
  63.  // Move DC motor 2 forward with increasing speed
  64.   Serial.println("Motor 2 fwd 100");
  65.   digitalWrite(motor2Pin1, HIGH);
  66.   digitalWrite(motor2Pin2, LOW);
  67.  
  68.   ledcWrite(pwmChannel, 100);
  69.   delay(1000);
  70. */
  71.   // Move DC motor 1 forward with increasing speed
  72.   Serial.println("Motor fwd 100");
  73.   digitalWrite(motor1Pin1, HIGH);
  74.   digitalWrite(motor1Pin2, LOW);
  75.   ledcWrite(pwmChannel, 100);
  76.   delay(1000);
  77.  
  78.    Serial.println("Motor stopped");
  79.    digitalWrite(motor1Pin1, LOW);
  80.    digitalWrite(motor1Pin2, LOW);
  81.    delay(1000);
  82.  
  83.    Serial.println("Motor fwd 150");
  84.    digitalWrite(motor1Pin1, HIGH);
  85.    digitalWrite(motor1Pin2, LOW);
  86.    ledcWrite(pwmChannel, 150);
  87.    delay(1000);
  88.  
  89.    Serial.println("Motor stopped");
  90.    digitalWrite(motor1Pin1, LOW);
  91.    digitalWrite(motor1Pin2, LOW);
  92.    delay(1000);
  93.  
  94.    //--------------------motor 2
  95.    // Move DC motor 1 forward with increasing speed
  96.   Serial.println("Motor fwd 100");
  97.   digitalWrite(motor2Pin1, HIGH);
  98.   digitalWrite(motor2Pin2, LOW);
  99.   ledcWrite(pwmChannel, 100);
  100.   delay(1000);
  101.  
  102.    Serial.println("Motor stopped");
  103.    digitalWrite(motor2Pin1, LOW);
  104.    digitalWrite(motor2Pin2, LOW);
  105.    delay(1000);
  106.  
  107.    Serial.println("Motor fwd 150");
  108.    digitalWrite(motor2Pin1, HIGH);
  109.    digitalWrite(motor2Pin2, LOW);
  110.    ledcWrite(pwmChannel, 150);
  111.    delay(1000);
  112.  
  113.    Serial.println("Motor stopped");
  114.    digitalWrite(motor2Pin1, LOW);
  115.    digitalWrite(motor2Pin2, LOW);
  116.    delay(1000);
  117. /*  
  118.   while (dutyCycle <= 200){
  119.     ledcWrite(pwmChannel, dutyCycle);  
  120.     Serial.print("Forward with duty cycle: ");
  121.     Serial.println(dutyCycle);
  122.     dutyCycle = dutyCycle + 5;
  123.     delay(500);
  124.   }
  125.   dutyCycle = 200;
  126. */  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement