Wednesday, June 15, 2016

Laser pointer servo

/* Sweep
  by BARRAGAN <http://barraganstudio.com>
  This example code is in the public domain.

  modified 8 Nov 2013
  by Scott Fitzgerald
  http://www.arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>
//int pos;
int posit;
Servo servo1;  // create servo object to control a servo
Servo servo2;  // create servo object to control a servo
// twelve servo objects can be created on most boards


void setup() {
  servo1.attach(9);  // attaches the servo on pin 9 to the servo object
  servo2.attach(10);
  Serial.begin(9600);
  servo1.write(5);
  servo2.write(5);
  delay(20);
}

void loop() {
  posit = servo1.read();
 
  long horRand1 = random(30, 150);
  Serial.println(horRand1);
  Serial.println("1");
  if (posit >= horRand1 ) {
     Serial.println("2");
    for (long pos = posit; pos >= horRand1; pos -= 1) {
      servo1.write(pos);
       //Serial.println("3");
      delay(20);
       Serial.println(pos);
    }
     Serial.println("4");
  }
  else {
    for (long pos = posit; pos <= horRand1; pos += 1){
      servo1.write(pos);
      delay(20);
    }
    }


  posit = servo2.read();
  long vertRand2 = random(0, 100);
  Serial.println(vertRand2);
  if (posit >= vertRand2) {
    for (long pos = posit; pos >= vertRand2; pos -= 1) {
      servo2.write(pos);
      delay(20);
    }
  }
  else {
    for (long pos = posit; pos <= vertRand2; pos += 1){
       servo2.write(pos);
      delay(20);
    }
    }

}

No comments:

Post a Comment