Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- //Servo connection PINs
- int spin[4] = {2,3,4,5};
- int sangle[4] = {0,90,180,90};
- Servo myservo[4]; // create Servo object to control a servo
- char c; //character
- bool d = false; //are new data
- void readChar() {
- if (Serial.available() > 0) {
- c = Serial.read();
- d = true;
- Serial.print("pushed: ");
- Serial.println(c);
- }
- }
- void setservos(int a){
- for (int s = 0;s < 4;s++) {
- Serial.print("servo: ");
- Serial.print(s);
- Serial.print(" Angle: ");
- Serial.println(a);
- myservo[s].write(a); // sets the servo position
- delay(250);
- }
- }
- void stest() {
- for (int i = 0; i < 4; i++){
- setservos(sangle[i]);
- delay(500);
- }
- }
- void setup() {
- Serial.begin(9600);
- Serial.println("Arduino is ready");
- Serial.println("Write leater t-est, r-ight, m-idle, l-eft");
- for (int s = 0;s < 4;s++) {
- myservo[s].attach(spin[s]); // attaches the servos on pins
- myservo[s].write(90); // sets the servo position to midle
- }
- }
- void loop() {
- readChar();
- if (d == true) {
- if (c == 't') {
- Serial.println("Servo test");
- stest();
- }
- if (c == 'r') {
- Serial.println("Servo to right");
- setservos(0);
- }
- if (c == 'm') {
- Serial.println("Servo to midle");
- setservos(90);
- }
- if (c == 'l') {
- Serial.println("Servo to left");
- setservos(180);
- }
- d = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment