SHARE
TWEET

Untitled

a guest May 31st, 2017 560 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Servo.h>
  2. Servo rightVictor;// create servo object to control a servo
  3. Servo leftVictor;
  4. int buttonPin1 = 8; //button 1 pin
  5. int leftVictorPin = 10;
  6. int rightVictorPin = 11; // twelve servo objects can be created on most boards
  7. const int XMIN = 4611;
  8. const int XMID = 4900;
  9. const int XMAX = 5191;
  10.  
  11. const int xIn = 7;              // X output
  12. const int yIn = 6;              // Y output
  13. int buttonState1 = 0;
  14. int maxX = 0;
  15. int maxY = 0;
  16. bool start = true;
  17. int minX;
  18. int minY;
  19. int tilt = 90;
  20. int xTilt = XMID;
  21. int yTilt;
  22.  
  23. void setup() {
  24.   Serial.begin(9600);
  25.   // initialize the pushbutton pin as an input:
  26.   pinMode(buttonPin1, INPUT);
  27.   rightVictor.attach(rightVictorPin);  // attaches the servo on pin 10 to the servo object
  28.   leftVictor.attach(leftVictorPin);
  29. }
  30.  
  31. int getXTilt() {return pulseIn(xIn, HIGH);} // Read X pulse
  32. int getYTilt() {return pulseIn(yIn, HIGH);} // Read X pulse
  33.  
  34. void runMotor(int value)
  35. {
  36.   rightVictor.write(value);
  37.   leftVictor.write(value);
  38. }
  39.  
  40. int scale(int value){return (pow((value - 4900), 3)) / 280000 + 90;} //map(value, XMIN,XMAX,0,179);}// //turns accelerometer # into motor #
  41.  
  42. void valueTest()
  43. {
  44.   for (int i = 90; i <= 180; i++)
  45.   {
  46.     runMotor(i);
  47.     Serial.println(i);
  48.     delay(500);
  49.   }
  50.   for (int i = 180; i >= 0; i--)
  51.   {
  52.     runMotor(i);
  53.     Serial.println(i);
  54.     delay(500);
  55.   }
  56.  
  57. }
  58. void accelerate(int currentVal)
  59. {
  60.   for (int i = currentVal; i < 180; i++)
  61.   {
  62.     runMotor(i);
  63.     delay(50);
  64.   }
  65. }
  66.  
  67. void slow(int currentVal)
  68. {
  69.   Serial.println("SLOWING");
  70.   buttonState1 = digitalRead(buttonPin1);
  71.   if (currentVal < XMID)
  72.     for (int i = currentVal; i <= XMID; i += 4)
  73.     {
  74.       xTilt = i;
  75.       tilt = scale(xTilt);
  76.       runMotor(tilt);
  77.       delay(10);
  78.       debug();
  79.       if (buttonState1 == HIGH)
  80.         break;
  81.     }
  82.   else if (currentVal > XMID)
  83.     for (int i = currentVal; i >= XMID; i -= 4)
  84.     {
  85.       xTilt = i;
  86.       tilt = scale(xTilt);
  87.       runMotor(tilt);
  88.       delay(10);
  89.       debug();
  90.       if (buttonState1 == HIGH)
  91.         break;
  92.     }
  93.   else
  94.   {
  95.     debug();
  96.     runMotor(90);
  97.   }
  98. }
  99.  
  100. void debug()
  101. {
  102.   Serial.print("Tilt= ");
  103.   Serial.print(tilt);
  104.   Serial.print("\t");
  105.   Serial.print("xTilt= ");
  106.   Serial.print(xTilt);
  107.   Serial.print("\t");
  108.   Serial.print("buttonstate= ");
  109.   Serial.print(buttonState1);
  110.   Serial.println();
  111. }
  112.  
  113. void loop() {
  114.   //valueTest();
  115.   buttonState1 = digitalRead(buttonPin1);
  116.   debug();
  117.   if (buttonState1 == HIGH)
  118.   {
  119.     xTilt = getXTilt();
  120.     tilt = scale(xTilt);//map(xTilt, XMIN,XMAX,0,179);
  121.     runMotor(tilt);
  122.   }
  123.   else
  124.     slow(xTilt);
  125. }
  126.  
  127. void accellDebug() {
  128.   int pulseX = getXTilt();
  129.   int pulseY = getYTilt();
  130.   if (start)
  131.   {
  132.     minX = pulseX;
  133.     minY = pulseY;
  134.     start = false;
  135.   }
  136.   if (pulseX < minX)
  137.     minX = pulseX;
  138.   if (pulseY < minY)
  139.     minY = pulseY;
  140.   if (pulseX > maxX)
  141.     maxX = pulseX;
  142.   if (pulseY > maxY)
  143.     maxY = pulseY;
  144.   Serial.println(rightVictor.read());
  145.  
  146.   Serial.print(pulseX);         // Display X and Y values
  147.   Serial.print("\t");
  148.   Serial.print(pulseY);
  149.   Serial.print("\t");
  150.   Serial.print("X Max = ");
  151.   Serial.print(maxX);
  152.   Serial.print("\t");
  153.   Serial.print("Y Max = ");
  154.   Serial.print(maxY);
  155.   Serial.print("\t");
  156.   Serial.print("X Min = ");
  157.   Serial.print(minX);
  158.   Serial.print("\t");
  159.   Serial.print("Y Min = ");
  160.   Serial.print(minY);
  161.   Serial.println();
  162.   delay(20);
  163. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top