Advertisement
Victoryus82

esp32_l293d_dc_teszt

Jul 10th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Motor A
  2. int motor1Pin1 = 13;
  3. int motor1Pin2 = 12;
  4. //int enable1Pin = 14;
  5.  
  6. // Setting PWM properties
  7. const int freq = 30000;
  8. const int pwmChannel = 0;
  9. const int resolution = 8;
  10. int dutyCycle = 200;
  11.  
  12. void setup() {
  13.   // sets the pins as outputs:
  14.   pinMode(motor1Pin1, OUTPUT);
  15.   pinMode(motor1Pin2, OUTPUT);
  16.   //pinMode(enable1Pin, OUTPUT);
  17.  
  18.   // configure LED PWM functionalitites
  19.   ledcSetup(pwmChannel, freq, resolution);
  20.  
  21.   // attach the channel to the GPIO to be controlled
  22.   //ledcAttachPin(enable1Pin, pwmChannel);
  23.     ledcAttachPin(motor1Pin1, pwmChannel);
  24.  
  25.   Serial.begin(115200);
  26.  
  27.   // testing
  28.   Serial.print("Testing DC Motor...");
  29. }
  30.  
  31. void loop() {
  32.   // Move the DC motor forward at maximum speed
  33.   Serial.println("Moving Forward");
  34.   digitalWrite(motor1Pin1, LOW);
  35.   digitalWrite(motor1Pin2, HIGH);
  36.   delay(2000);
  37.  
  38.   // Stop the DC motor
  39.   Serial.println("Motor stopped");
  40.   digitalWrite(motor1Pin1, LOW);
  41.   digitalWrite(motor1Pin2, LOW);
  42.   delay(1000);
  43.  
  44.   // Move DC motor backwards at maximum speed
  45.   Serial.println("Moving Backwards");
  46.   digitalWrite(motor1Pin1, HIGH);
  47.   digitalWrite(motor1Pin2, LOW);
  48.   delay(2000);
  49.  
  50.   // Stop the DC motor
  51.   Serial.println("Motor stopped");
  52.   digitalWrite(motor1Pin1, LOW);
  53.   digitalWrite(motor1Pin2, LOW);
  54.   delay(1000);
  55.  
  56.   // Move DC motor forward with increasing speed
  57.   digitalWrite(motor1Pin1, HIGH);
  58.   digitalWrite(motor1Pin2, LOW);
  59.   while (dutyCycle <= 255){
  60.     ledcWrite(pwmChannel, dutyCycle);  
  61.     Serial.print("Forward with duty cycle: ");
  62.     Serial.println(dutyCycle);
  63.     dutyCycle = dutyCycle + 5;
  64.     delay(500);
  65.   }
  66.   dutyCycle = 200;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement