Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////////////////////////////////
- //©2011 bildr
- //Released under the MIT License - Please reuse change and share
- //Simple code for the ADXL335, prints calculated orientation via serial
- //////////////////////////////////////////////////////////////////
- #include <Servo.h>
- Servo myservo;
- //////////////////////////////////////////////////////////////////
- const int xPin = 0;
- const int yPin = 1;
- const int zPin = 2;
- int minValX = 259;
- int maxValX = 394;
- int minValY = 259;
- int maxValY = 392;
- int minValZ = 266;
- int maxValZ = 396;
- double c = 0;
- double x;
- double y;
- double z;
- int xRead;
- int yRead;
- int zRead;
- //////////////////////////////////////////////////////////////////
- void setup(){
- Serial.begin(9600);
- delay(300);
- myservo.attach(9);
- xRead = analogRead(xPin);
- yRead = analogRead(yPin);
- zRead = analogRead(zPin);
- if(xRead > 280 && xRead < 350){
- minValX = xRead - 64;
- maxValX = xRead + 64;
- }
- if(yRead > 280 && yRead < 350){
- minValY = yRead - 64;
- maxValY = yRead + 64;
- }
- if(zRead > 280 && zRead < 350){
- minValZ = zRead - 64;
- maxValZ = zRead + 64;
- }
- }
- //////////////////////////////////////////////////////////////////
- void loop(){
- xRead = analogRead(xPin);
- yRead = analogRead(yPin);
- zRead = analogRead(zPin);
- x = map(xRead, minValX, maxValX, -90, 90);
- y = map(yRead, minValY, maxValY, -90, 90);
- z = map(zRead, minValZ, maxValZ, -90, 90);
- printNumber(x,y,z,xRead,yRead,zRead);
- moveMotor(x);
- }
- //////////////////////////////////////////////////////////////////
- void moveMotor(double x){
- delay(20);
- if(x>5){
- myservo.write(95);
- delay(200);
- }else if(x<-5){
- myservo.write(77);
- delay(200);
- }else{
- c = 0;
- }if(c<4){
- c = c + 0.3;
- }myservo.write(86);
- delay(300);
- }
- //////////////////////////////////////////////////////////////////
- void printNumber(double x, double y, double z,int xRead,int yRead,int zRead){
- Serial.println();
- Serial.print("x: ");
- Serial.print((int)x);
- Serial.print("(v");
- Serial.print(xRead);
- Serial.print(") | y: ");
- Serial.print((int)y);
- Serial.print("(v");
- Serial.print(yRead);
- Serial.print(") |z: ");
- Serial.print((int)z);
- Serial.print("(v");
- Serial.print(zRead);
- Serial.print(")");
- }
- //////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement