This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Teensy Mouse Code

By: a guest on Apr 24th, 2010  |  syntax: C  |  size: 3.46 KB  |  views: 1,561  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. int ledPin = 0;                  // Led Output pin
  3. int inPins[] = {0,1,2,3};        // Analog Input pins for the 4 photoresistors
  4. int bufferPast[] = {0,0,0,0};    // Motion detector buffer. Not used.
  5. int bufferPresent[] = {0,0,0,0}; // Motion detector buffer. Not used.
  6. int calibated[] = {0,0};         // Store the X and Y bias. => Ambiant light noise. used to remove the light noise in the reading.
  7. int is_calibrated = 0;           // 0 = Not calibrated yet. 1 = calibrated. Set to 1 by the calibrate() function.
  8.  
  9. void setup()   {
  10.   pinMode(ledPin, OUTPUT);
  11.   Serial.begin(9600);
  12. }
  13.  
  14. void loop() {
  15.   if (is_calibrated == 0) {
  16.     blinkIt(5,200);                       // Blink the led 5 time slowly to let the user know the calibration is going to start
  17.     Serial.println("---------------");
  18.     Serial.println("Calibrating...");
  19.     digitalWrite(ledPin, HIGH);           // light the LED
  20.     calibrate();                          // Calibrate the sensor. The X and Y bias are stored in calibated[]
  21.     Serial.println("Calibration Done!");
  22.     Serial.print("x bias:");
  23.     Serial.print(calibated[0]);
  24.     Serial.print("\t");
  25.     Serial.print("y bias:");
  26.     Serial.print(calibated[1]);
  27.     Serial.println("");
  28.     Serial.println("---------------");
  29.     blinkIt(10,100);                      // Blink 10 time fast to let the user know the calibration is done
  30.   } else {
  31.     digitalWrite(ledPin, HIGH);           // Light the LED
  32.     Serial.print(x_vector_motion());
  33.     Serial.print(";");
  34.     Serial.print(y_vector_motion());
  35.     Serial.println("");
  36.     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.
  37.     delay(20);
  38.   }
  39. }
  40.  
  41. // Blink n times with a delay of d ms
  42. void blinkIt(int n, int d) {
  43.   for (int i=0;i<n;i++) {
  44.     digitalWrite(ledPin, HIGH);
  45.     delay(d);
  46.     digitalWrite(ledPin, LOW);
  47.     delay(d);
  48.   }
  49. }
  50.  
  51. /*
  52. To calibrate:
  53. Sum the X and Y vectors over a few loops, and get the average vector.
  54. This allows to get the X and Y bias of the ambient location.
  55. Then substract this bias from the X and Y vectors when using the sensors.
  56. */
  57. void calibrate() {
  58.    int x_t = 0;
  59.    int y_t = 0;
  60.    int loops = 100;
  61.    for (int i=0;i<loops;i++) {
  62.      x_t += x_vector_motion();
  63.      y_t += y_vector_motion();
  64.      delay(50);
  65.    }
  66.    calibated[0] = x_t/loops;
  67.    calibated[1] = y_t/loops;
  68.    is_calibrated = 1;
  69. }
  70.  
  71. // Motion detection function. return 0 for no motion and up to 1023 for motion, depending on the intensity of the light changing. really sensitive!
  72. unsigned int motion(int n) {
  73.   int value = analogRead(inPins[n]);
  74.   bufferPresent[n] = value-bufferPast[n];
  75.   if (bufferPresent[n] < 0) {
  76.     bufferPresent[n] = -bufferPresent[n];
  77.   }
  78.   bufferPast[n] = value;
  79.   return (unsigned int) bufferPresent[n];
  80. }
  81.  
  82. // return the raw reading of a photoresistor (0 to 1023)
  83. unsigned int raw_motion(int n) {
  84.   int value = analogRead(inPins[n]);
  85.   return (unsigned int) value;
  86. }
  87.  
  88.  
  89. /* Vectors:
  90. Create an average between 2 vectors to get a stable reading.
  91. */
  92. int x_vector_motion() {
  93.   int vector1 = raw_motion(inPins[0])-raw_motion(inPins[1]);
  94.   int vector2 = raw_motion(inPins[2])-raw_motion(inPins[3]);
  95.   return ((vector1+vector2)/2)-calibated[0];
  96. }
  97.  
  98. int y_vector_motion() {
  99.   int vector1 = raw_motion(inPins[0])-raw_motion(inPins[2]);
  100.   int vector2 = raw_motion(inPins[1])-raw_motion(inPins[3]);
  101.   return ((vector1+vector2)/2)-calibated[1];
  102. }
clone this paste RAW Paste Data