金工实习 控制小车
单片机源程序如下:
- #include <SoftwareSerial.h>
- SoftwareSerial mySerial(0, 1); // RX, TX
- #include <Servo.h>
- Servo myservo; // create servo object to control a servo
- int pos=60;
- int p1=2; // variable to store the servo position
- char val='/';
- void setup()
- {
- Serial.begin(9600);
- myservo.attach(p1); // attaches the servo on pin 2 to the servo object
- myservo.write(pos); //初始化所有舵机位置,防止通电抖动。
- delay(1000);
- myservo.detach();
- }
- void loop() // run over and over
- {
- val=Serial.read();
- if(val=='1')
- {
- myservo.attach(p1); // attaches the servo on pin 2 to the servo object
- for(inti=1;i<=30;i++) // goes from 60 degrees to 30 degrees
- {
- pos--;
- myservo.write(pos); // tell servo to go to position in variable 'pos'
- delay(15); // waits 15ms for the servo to reach the position
- }
- If(pos==0)pos=180;
- myservo.detach();
- val='11';
- }
- if(val=='2')
- {
- myservo.attach(p1);
- for(pos=30;pos<=60;pos+= 1) // goes from 30 degrees to 60 degrees
- { // in steps of 1 degree
- myservo.write(pos); // tell servo to go to position in variable 'pos'
- delay(15); // waits 15ms for the servo to reach the position
- }
- myservo.detach();
- val='22';
- }
- }
复制代码
所有资料51hei提供下载:
遥控单舵机(180度)2连续运动20171227.docx
(16.04 KB, 下载次数: 6)
|