/* IRdetect on analog input with LED light and Servo Reads an analog input on pin 0, prints the result to the serial monitor, and moves a servo. Attach the IR photo diode to analog pin 0 input, LED to digital pin 13 output, the outside pins to +5V, and ground. Attach servo control yellow (or orange) wire to pin 9, red to +5V, and black (or brown) to ground. */ // Initialize #include Servo chestservo; // Create servo object to control servo. int pos = 0; // Variable to store the servo position. int led13Pin = 13; // Initialize variable as 13 for pin 13. // Setup pins and variables void setup() { Serial.begin(9600); // Setup serial communication at 9600 bits per second. pinMode(led13Pin,OUTPUT); //Setup led13Pin as an output pin. chestservo.attach(9); // Attaches servo to pin 9 to the servo object. } // Function Loop void loop() { int sensorValue = 0; // Initialize variable to 0. sensorValue = analogRead(A0); // Sets variable as the read input on analog pin 0. Serial.println(sensorValue, DEC); // Print out the value you read. delay(5); // Delay in msec between reads for stability. if (sensorValue > 2) { // IF greater than value, THEN light the LED, move servo, // and return serial output. Serial.println("Yes HIGH"); // Serial output. digitalWrite(led13Pin, HIGH); // 2nd output set to HIGH or ON. //for(pos = 0; pos < 180; pos += 1) { // FOR loop goes from 0 to 180 degrees // in 1 degree steps. Chest opens. // Serial.print(" POS = "); // Serial.print(pos); // Serial output value of 'pos'. // chestservo.write(pos); // Servo moves to variable 'pos'. // delay(25); // Delay in msec to allow servo to move. //} for(pos = 180; pos >= 1; pos -= 1) { // FOR loop goes from 180 to 0 degrees // in 1 degree steps if the servo is put in backwards. Chest opens. Serial.print(" POS = "); Serial.print(pos); // Serial output value of 'pos'. chestservo.write(pos); // Servo moves to variable 'pos'. delay(25); // Delay in msec to allow servo to move. } Serial.println(" "); delay(2400); // Delay in msec chest is open. for(pos = 1; pos < 180; pos += 1) { // FOR Loop goes from 0 to 180 degrees // in 1 degree steps if the servo is put in backwards. Chest closes. Serial.print(" POS = "); Serial.print(pos); // Serial output value of 'pos'. chestservo.write(pos); // Servo moves to variable 'pos'. delay(25); // Delay in msec to allow servo to move. } //for(pos = 180; pos >= 1; pos -= 1) { // FOR Loop goes from 180 to 0 degrees // in 1 degree steps. Chest closes. // Serial.print(" POS = "); // Serial.print(pos); // Serial output value of 'pos'. // chestservo.write(pos); // Servo moves to variable 'pos'. // delay(25); // Delay in msec to allow servo to move. //} Serial.println(" "); delay(200); // Delay in msec light on. digitalWrite(led13Pin, LOW); // 2nd output set to LOW or OFF. }else{ digitalWrite(led13Pin, LOW); // Sets default LED output to LOW or OFF. int sensorValue = 0; // Set integer variable back to 0. } // End of IF statement. } // End of LOOP.