Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int ledPin = 0; // Led Output pin
- int inPins[] = {0,1,2,3}; // Analog Input pins for the 4 photoresistors
- int bufferPast[] = {0,0,0,0}; // Motion detector buffer. Not used.
- int bufferPresent[] = {0,0,0,0}; // Motion detector buffer. Not used.
- int calibated[] = {0,0}; // Store the X and Y bias. => Ambiant light noise. used to remove the light noise in the reading.
- int is_calibrated = 0; // 0 = Not calibrated yet. 1 = calibrated. Set to 1 by the calibrate() function.
- void setup() {
- pinMode(ledPin, OUTPUT);
- Serial.begin(9600);
- }
- void loop() {
- if (is_calibrated == 0) {
- blinkIt(5,200); // Blink the led 5 time slowly to let the user know the calibration is going to start
- Serial.println("---------------");
- Serial.println("Calibrating...");
- digitalWrite(ledPin, HIGH); // light the LED
- calibrate(); // Calibrate the sensor. The X and Y bias are stored in calibated[]
- Serial.println("Calibration Done!");
- Serial.print("x bias:");
- Serial.print(calibated[0]);
- Serial.print("\t");
- Serial.print("y bias:");
- Serial.print(calibated[1]);
- Serial.println("");
- Serial.println("---------------");
- blinkIt(10,100); // Blink 10 time fast to let the user know the calibration is done
- } else {
- digitalWrite(ledPin, HIGH); // Light the LED
- Serial.print(x_vector_motion());
- Serial.print(";");
- Serial.print(y_vector_motion());
- Serial.println("");
- Mouse.move(x_vector_motion()/10, y_vector_motion()/10); // Move the mouse according to the sensor's reading. Values divided by 10 to reduce the speed.
- delay(20);
- }
- }
- // Blink n times with a delay of d ms
- void blinkIt(int n, int d) {
- for (int i=0;i<n;i++) {
- digitalWrite(ledPin, HIGH);
- delay(d);
- digitalWrite(ledPin, LOW);
- delay(d);
- }
- }
- /*
- To calibrate:
- Sum the X and Y vectors over a few loops, and get the average vector.
- This allows to get the X and Y bias of the ambient location.
- Then substract this bias from the X and Y vectors when using the sensors.
- */
- void calibrate() {
- int x_t = 0;
- int y_t = 0;
- int loops = 100;
- for (int i=0;i<loops;i++) {
- x_t += x_vector_motion();
- y_t += y_vector_motion();
- delay(50);
- }
- calibated[0] = x_t/loops;
- calibated[1] = y_t/loops;
- is_calibrated = 1;
- }
- // Motion detection function. return 0 for no motion and up to 1023 for motion, depending on the intensity of the light changing. really sensitive!
- unsigned int motion(int n) {
- int value = analogRead(inPins[n]);
- bufferPresent[n] = value-bufferPast[n];
- if (bufferPresent[n] < 0) {
- bufferPresent[n] = -bufferPresent[n];
- }
- bufferPast[n] = value;
- return (unsigned int) bufferPresent[n];
- }
- // return the raw reading of a photoresistor (0 to 1023)
- unsigned int raw_motion(int n) {
- int value = analogRead(inPins[n]);
- return (unsigned int) value;
- }
- /* Vectors:
- Create an average between 2 vectors to get a stable reading.
- */
- int x_vector_motion() {
- int vector1 = raw_motion(inPins[0])-raw_motion(inPins[1]);
- int vector2 = raw_motion(inPins[2])-raw_motion(inPins[3]);
- return ((vector1+vector2)/2)-calibated[0];
- }
- int y_vector_motion() {
- int vector1 = raw_motion(inPins[0])-raw_motion(inPins[2]);
- int vector2 = raw_motion(inPins[1])-raw_motion(inPins[3]);
- return ((vector1+vector2)/2)-calibated[1];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement