Created
February 19, 2017 07:48
-
-
Save fishkingsin/3c7e568663e0c873c613041fb849ff4f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//以15毫秒一步的速度转动 | |
#include <Servo.h> | |
Servo servo1; // 建立Servo实例 | |
Servo servo2; | |
const int servo1Pin = 8; // servo1 接 Pin 8 | |
const int servo2Pin = 9; // servo2 接 Pin 9 | |
const int laserPin = 10; | |
const int every1 = 15; | |
const int every2 = 15; | |
const int laserDelay = 1; | |
int servo1Target = 0; | |
int fadeValueTarget = 0; | |
int servo1Dir = 1; | |
int fadeDir = 1; | |
int posOfServo1 = 90; | |
int posOfServo2 = 90; | |
int posOfFadeValue = 0; | |
void setup() { | |
analogWrite(laserPin, posOfFadeValue); | |
servo1.attach(servo1Pin, 500, 2500); // pin8, | |
servo2.attach(servo2Pin, 500, 2500); // pin9 | |
Serial.begin(9600); | |
servo1.write(posOfServo1); | |
servo2.write(posOfServo2); | |
Serial.println("2 Servo test"); | |
delay(500); | |
} | |
void loop( ) { | |
checkServo1(); | |
checkLaser(); | |
} | |
void checkServo1( ) { | |
static uint32_t Timer; | |
if (Timer > millis()) | |
return; | |
Timer = millis() + every1; | |
if (servo1Target > posOfServo1) { | |
posOfServo1 += servo1Dir; | |
} else if (servo1Target == posOfServo1) { | |
return; | |
} else { | |
posOfServo1 += -servo1Dir; | |
} | |
servo1.write(posOfServo1); | |
} | |
void checkLaser() { | |
//依Servo1,servo2,以同样方法添加更多舵机 | |
static uint32_t Timer; | |
if (Timer > millis()) | |
return; | |
Timer = millis() + laserDelay; | |
if (fadeValueTarget > posOfFadeValue) { | |
posOfFadeValue += fadeDir; | |
} else if (fadeValueTarget == posOfFadeValue) { | |
return; | |
} else { | |
posOfFadeValue += -fadeDir; | |
} | |
analogWrite(laserPin, posOfFadeValue); | |
} | |
void serialEvent() { | |
char bytes[3]; | |
if (Serial.available() == 3) { | |
servo1Target = int(Serial.read()); | |
fadeValueTarget = int(Serial.read()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment