Code
/* Double Transducer: Proximity to Light to Angle Kaitlin McTigue, Nathan Naylor This code 1. Mesures distance with an IR sensor 2. Maps that distance to a scale of 0-5 3. Lights an LED with brightness based on the IR value 4. Uses a photoreceptor to measure the amount of light emitted by the LED 5. Maps that light value to a number between 1 and 170 6. Spins a servo to that angle The angle of the servo represents the closeness of an object to the IR sensor! Inputs: 1 IR proximity sensor 1 photoreceptor 1 potentiometer Outputs: 1 LED 1 servometer Serial Monitor Feedback */ #include <Servo.h> const int LEDPIN = 3; //digital const int PHOTOPIN = A1; //analog const int IRPIN = A0; //analog Servo outservo; void setup() { outservo.attach(2); // attaches the servo on pin pinMode(IRPIN, INPUT); pinMode(LEDPIN, OUTPUT); pinMode(PHOTOPIN, INPUT); Serial.begin(9600); // Open serial monitor at 9600 } void loop() { delay(50); int irVal = analogRead(IRPIN); //read distance int ledVal = map(100, 500, irVal, 0, 5); //convert IR input to a scale 0 to 5 volts analogWrite(LEDPIN, ledVal); //brighten LED as IR distance is closer int photoVal = analogRead(PHOTOPIN); //red LED brightness int servoVal = map(400, 500, photoVal, 170, 0); //convert photoreceptor input to a scale of 170 to 0 outservo.write(servoVal); //move servo between 0 and 170 degrees }
Comments are closed.