A small project I built in 2015 to test an acceleration sensor with my Arduino Mega 2560. The pitch, roll, and yaw of the gun are controlled with the ADXL335 sensor.

Arduino Ballshooter mit Beschleunigungssensor ADXL330KCPZ
Watch this video on YouTube.
By viewing the video, you agree that your data will be transmitted to YouTube and that you have read the privacy policy

Parts

  • Arduino Mega 2560
  • Acceleration Sensor ADXL335
  • Motor driver L293D
  • Lego Technic Power functions M-Motor
  • Two Servos

Circuit

In the following picture you can see the electrical circuit of the ball gun.

Code for Arduino

// import the servo library
#include <Servo.h>

// Outputs
Servo servoPlattform; // servo for vertical tilt
Servo servoRot;       // servo for rotation
int moShot_a = 10;
int moShot_b = 11;

// Inputs
int zAccPin = 2;
int yAccPin = 1;
int xAccPin = 0;

// Variables
int posServoPlatt = 1500;
int posServoRot = 1500;
int zAcc = 0;
int yAcc = 0;
int xAcc = 0;

void setup() 
{ 
  // Outputs
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
  
  // Attach servo variables to pins
  servoPlattform.attach(2);
  servoRot.attach(6);
  
  // servos to middle position 
  servoPlattform.write(posServoPlatt);
  servoRot.write(posServoRot);
  
  Serial.begin(9600);
} 
  
void loop() 
{
  analogWrite(moShot_a,0);
  analogWrite(moShot_b,0);
  while(true){
    // Check values of the acceleration sensor
    zAcc = analogRead(zAccPin);
    yAcc = analogRead(yAccPin);
    xAcc = analogRead(xAccPin);
    zAcc = zAcc * 0.1;
    yAcc = yAcc * 0.1;
    xAcc = xAcc * 0.1;
    // print values 
    Serial.print("  z: ");
    Serial.print(zAcc);
    Serial.print("  y: ");
    Serial.print(yAcc);
    Serial.print("  x: ");
    Serial.print(xAcc);
    Serial.println();
    // control the vertical tilt 
    if(yAcc < 28 && posServoPlatt <= 2200){
      posServoPlatt = posServoPlatt + 10;
      Serial.println(posServoPlatt);
    }
    else if(yAcc > 34 && posServoPlatt >= 1000){
      posServoPlatt = posServoPlatt - 10;
      Serial.print(posServoPlatt);
    }
    servoPlattform.writeMicroseconds(posServoPlatt);
    // control the rotation
    if(zAcc > 34 && posServoRot <= 2400){
      posServoRot = posServoRot + 10;
      Serial.println(posServoRot);
    }
    else if(zAcc < 28 && posServoRot >= 700){
      posServoRot = posServoRot - 10;
      Serial.print(posServoRot);
    }
    servoRot.writeMicroseconds(posServoRot);
    // shot ball
    if(xAcc > 50){
      analogWrite(moShot_a,255);
      analogWrite(moShot_b,0);
      delay(650);
      analogWrite(moShot_a,0);
      analogWrite(moShot_b,0);
    }   
    delay(5);    
  }
}  
    
Share this post

0 Comments

Add a Comment

Your email address will not be published. Required fields are marked *