Back to Parent

int ledPin = D0;
int photoCellPin = A0;
int laser = D2;
int servoPin = A5;
int lightReading = 0;
int lightThreshold = 400;
Servo myServo; //create servo object
int Pos = 0;

void setup() {
 pinMode(ledPin, OUTPUT);
 pinMode(photoCellPin, INPUT);
 myServo.attach(A5);
 pinMode(laser, OUTPUT);
 Serial.begin(9600);
 Particle.variable("light", &lightReading, INT);
}

void loop() {
 lightReading = analogRead(photoCellPin); //current reading
 Serial.println(lightReading); //print out reading
 if (lightReading > lightThreshold) {
   digitalWrite(ledPin, HIGH); //LED always on
   digitalWrite(laser, HIGH); //laser on
   for(Pos = 0; Pos < 45; Pos += 5) // goes from 0 degrees to 180 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
}
for(Pos = 45; Pos>=1; Pos-=5)  // goes from 180 degrees to 0 degrees
{
myServo.write(Pos);    // tell servo to go to position in
         // variable 'pos'
delay(15);      // waits 15ms for the servo to reach
         // the position
}
 }else{
   digitalWrite(laser, LOW);
   for(int i = 0; i <=50; i += 2) {
     analogWrite(ledPin, i);
     delay(100);
   }
   for(int i = 50; i >=0; i -= 2) {
     analogWrite(ledPin, i);
     delay(100);
   }
 }
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0