微型直流电机控制基本方法 L298N模块 1 //控制板引脚定义 2 int motor_c_ENA=6; 3 int motor_c_IN1=8; 4 int motor_c_IN2=7; 5 6 //子函数定义 7 void go_forward_high_speed() //正转 全速 8 { 9 digitalWrite(motor_c_ENA,1); 10 digitalWrite(motor_c_IN1,0); 11 digitalWrite(motor_c_IN2,1); 12 } 13 14 void go_backward_high_speed() //反转 全速 15 { 16 digitalWrite(motor_c_ENA,1); 17 digitalWrite(motor_c_IN1,1); 18 digitalWrite(motor_c_IN2,0); 19 } 20 21 void stop_with_brake() //制动 22 { 23 digitalWrite(motor_c_ENA,1); 24 digitalWrite(motor_c_IN1,1); 25 digitalWrite(motor_c_IN2,1) ; 26 } 27 28 void stop_free() //自由停车 29 { 30 digitalWrite(motor_c_ENA,0); 31 digitalWrite(motor_c_IN1,0); 32 digitalWrite(motor_c_IN2,0) ; 33 } 34 35 void go_forward_speed_up() //正转 加速 36 { 37 digitalWrite(motor_c_IN1,0); 38 digitalWrite(motor_c_IN2,1); 39 for (int a=100;a<=255;a++) 40 { 41 analogWrite(motor_c_ENA,a); 42 delay(200); 43 } 44 } 45 46 void go_backward_speed_up() //反转 加速 47 { 48 digitalWrite(motor_c_IN1,1); 49 digitalWrite(motor_c_IN2,0); 50 for (int a=100;a<=255;a++) 51 { 52 analogWrite(motor_c_ENA,a); 53 delay(200); 54 } 55 } 56 void go_forward_speed_down() //正转 减速 57 { 58 digitalWrite(motor_c_IN1,0); 59 digitalWrite(motor_c_IN2,1); 60 for (int a=255;a<=100;a–) 61 { 62 analogWrite(motor_c_ENA,a); 63 delay(200); 64 } 65 } 66 67 void go_backward_speed_down() //反转 减速 68 { 69 digitalWrite(motor_c_IN1,1); 70 digitalWrite(motor_c_IN2,0); 71 for (int a=255;a<=100;a–) 72 { 73 analogWrite(motor_c_ENA,a); 74 delay(200); 75 } 76 } 77 78 79 void setup() 80 { 81 pinMode(motor_c_ENA,OUTPUT); //电机C使能和PWM调速口 82 pinMode(motor_c_IN1,OUTPUT); //电机C控制口 83 pinMode(motor_c_IN2,OUTPUT); //电机C控制口 84 } 85 86 void loop() 87 { 88 stop_with_brake(); //制动2秒 89 delay(2000); 90 91 go_forward_speed_up(); //正转 加速 50秒 92 delay(50000); 93 go_forward_high_speed(); //正转 全速10秒 94 delay(10000); 95 go_forward_speed_down(); //正转 减速50秒 96 delay(50000); 97 stop_free(); //自由停车5秒 98 delay(5000); 99 100 go_backward_speed_up(); //反转 加速 50秒 101 delay(50000); 102 go_backward_high_speed(); //反转 全速10秒 103 delay(10000); 104 go_backward_speed_down(); //反转 减速50秒 105 delay(50000); 106 stop_free(); //自由停车5秒 107 delay(5000); 108 }
2024最新激活全家桶教程,稳定运行到2099年,请移步至置顶文章:https://sigusoft.com/99576.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。 文章由激活谷谷主-小谷整理,转载请注明出处:https://sigusoft.com/62586.html