Introduction: Sling Shot Controller for Angry Birds

Learn how to make a simple sling shot device to play Angry Birds using Arduino Leonardo. Below is the Arduino code with explanation in comments.



/*


This code is in the public domain
Author: alpeshVitha
Date: 18th May 2013

*/


int sPull = 0; // Start Position of the x axis
int ePull = 0; // End position of the x axis
int ysPull = 0; // Start Position of y axis
int yePull = 0; // End position of y axis
int range = 15;            
int responseDelay = 1;     // response delay of the mouse, in ms


// Declaration for smoothing the y axis reading. I found them very noisy, probably because potentiometer is logarithmic in nature.
const int numReadings = 10;
int readings[numReadings];      // the readings from the analog input
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average





void setup() {
  // initialize mouse control:
  Mouse.begin();
  Serial.begin(9600);

  // Smoothing initialisation
    for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;     
}

void loop() {
  // read the initial state of sling at x axis and y axis
  sPull = analogRead(A0); //x axis
  ysPull = analogRead(A1); // y axis

  ePull = sPull; // value of x stored in ePull variable
  delay(100); // wait for 100ms for the finger to slide on the potentiometer
  sPull = analogRead(A0); // read the x axis again
  int pull = (ePull - sPull); // Delta x

   // do the same for y axis to get Delta y
   yePull = ysPull;
   ysPull = analogRead(A1);
   int ypull = (yePull - ysPull); // Delta y

      if (sPull == 0){
           if (Mouse.isPressed(MOUSE_LEFT)) {
      Mouse.release(MOUSE_LEFT);
    }

    }
  if(pull>-200){  // Adjust this value based on your set up
  if(pull<200){//    Adjust this value based on your set up
  // calculate the movement distance
  int  xDistance = (pull/50)*range;


  if (xDistance != 0)  {
        if (!Mouse.isPressed(MOUSE_LEFT)) {
      Mouse.press(MOUSE_LEFT);
    }
    Mouse.move(-xDistance, 0, 0);


  }
  }
  }
  // Smoothing of Y
  ////////////////////////////////////////////////////////////////////////////////////////////////////

     total= total - readings[index];        
  // read from the sensor: 
  readings[index] = ypull;
  // add the reading to the total:
  total= total + readings[index];      
  // advance to the next position in the array: 
  index = index + 1;                   

  // if we're at the end of the array...
  if (index >= numReadings)             
    // ...wrap around to the beginning:
    index = 0;                          

  // calculate the average:
  average = total / numReadings;        

  /////////////////////////////////////////////////////////////////////////////////////////////////



  delay(1);
  int  yDistance = (average)*2;

if (yDistance != 0)  {

  Mouse.move(0, -yDistance, 0);

  }


// a delay so the mouse doesn't move too fast:
  delay(responseDelay);
}

Epilog Challenge V

Participated in the
Epilog Challenge V