Advertisement
pseud0_

Untitled

Aug 26th, 2022
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.51 KB | None | 0 0
  1. #include "Arduino.h"
  2. #include "AxisJoystick.h"
  3. #include "Servo.h"
  4.  
  5.  
  6. // Pin Definitions
  7. #define JOYSTICK_1_PIN_SW  2
  8. #define JOYSTICK_1_PIN_VRX  A3
  9. #define JOYSTICK_1_PIN_VRY  A1
  10. #define JOYSTICK_2_PIN_SW 3
  11. #define JOYSTICK_2_PIN_VRX  A5
  12. #define JOYSTICK_2_PIN_VRY  A4
  13. #define SERVO360MICRO1_1_PIN_SIG  4
  14. #define SERVO360MICRO2_2_PIN_SIG  5
  15.  
  16.  
  17.  
  18. // Global variables and defines
  19.  
  20. // object initialization
  21. AxisJoystick joystick_1(JOYSTICK_1_PIN_VRX, JOYSTICK_1_PIN_VRY, JOYSTICK_1_PIN_SW);
  22. AxisJoystick joystick_2(JOYSTICK_2_PIN_VRX, JOYSTICK_2_PIN_VRY, JOYSTICK_2_PIN_SW);
  23. Servo servo360Micro1_1;
  24. Servo servo360Micro2_2;
  25.  
  26.  
  27. // define vars for testing menu
  28. const int timeout = 10000;       //define timeout of 10 sec
  29. char menuOption = 0;
  30. long time0;
  31.  
  32. // Setup the essentials for your circuit to work. It runs first every time your circuit is powered with electricity.
  33. void setup()
  34. {
  35.   // Setup Serial which is useful for debugging
  36.   // Use the Serial Monitor to view printed messages
  37.   Serial.begin(9600);
  38.   while (!Serial) ; // wait for serial port to connect. Needed for native USB
  39.   Serial.println("start");
  40.  
  41.  
  42.   menuOption = menu();
  43.  
  44. }
  45.  
  46. // Main logic of your circuit. It defines the interaction between the components you selected. After setup, it runs over and over again, in an eternal loop.
  47. void loop()
  48. {
  49.  
  50.  
  51.   if (menuOption == '1') {
  52.     // PS2 X Y Axis Joystick Module #1 - Test Code
  53.     // Read Joystick X,Y axis and press
  54.     int joystick_1X =  joystick_1.xAxis();
  55.     int joystick_1Y =  joystick_1.yAxis();
  56.     int joystick_1SW =  joystick_1.readSW();
  57.     Serial.print(F("X: ")); Serial.print(joystick_1X);
  58.     Serial.print(F("\tY: ")); Serial.print(joystick_1Y);
  59.     Serial.print(F("\tSW: ")); Serial.println(joystick_1SW);
  60.  
  61.   }
  62.   else if (menuOption == '2') {
  63.     // PS2 X Y Axis Joystick Module #2 - Test Code
  64.     // Read Joystick X,Y axis and press
  65.     int joystick_2X =  joystick_2.xAxis();
  66.     int joystick_2Y =  joystick_2.yAxis();
  67.     int joystick_2SW =  joystick_2.readSW();
  68.     Serial.print(F("X: ")); Serial.print(joystick_2X);
  69.     Serial.print(F("\tY: ")); Serial.print(joystick_2Y);
  70.     Serial.print(F("\tSW: ")); Serial.println(joystick_2SW);
  71.  
  72.   }
  73.   else if (menuOption == '3') {
  74.     // Continuous Rotation Micro Servo - FS90R #1 - Test Code
  75.     // The servo will rotate CW in full speed, CCW in full speed, and will stop  with an interval of 2000 milliseconds (2 seconds)
  76.     servo360Micro1_1.attach(SERVO360MICRO1_1_PIN_SIG);         // 1. attach the servo to correct pin to control it.
  77.     servo360Micro1_1.write(180);  // 2. turns servo CW in full speed. change the value in the brackets (180) to change the speed. As these numbers move closer to 90, the servo will move slower in that direction.
  78.     delay(2000);                              // 3. waits 2000 milliseconds (2 sec). change the value in the brackets (2000) for a longer or shorter delay in milliseconds.
  79.     servo360Micro1_1.write(0);    // 4. turns servo CCW in full speed. change the value in the brackets (0) to change the speed. As these numbers move closer to 90, the servo will move slower in that direction.
  80.     delay(2000);                              // 5. waits 2000 milliseconds (2 sec). change the value in the brackets (2000) for a longer or shorter delay in milliseconds.
  81.     servo360Micro1_1.write(90);    // 6. sending 90 stops the servo
  82.     delay(2000);                              // 7. waits 2000 milliseconds (2 sec). change the value in the brackets (2000) for a longer or shorter delay in milliseconds.
  83.     servo360Micro1_1.detach();                    // 8. release the servo to conserve power. When detached the servo will NOT hold it's position under stress.
  84.   }
  85.   else if (menuOption == '4') {
  86.     // Continuous Rotation Micro Servo - FS90R #2 - Test Code
  87.     // The servo will rotate CW in full speed, CCW in full speed, and will stop  with an interval of 2000 milliseconds (2 seconds)
  88.     servo360Micro2_2.attach(SERVO360MICRO2_2_PIN_SIG);         // 1. attach the servo to correct pin to control it.
  89.     servo360Micro2_2.write(180);  // 2. turns servo CW in full speed. change the value in the brackets (180) to change the speed. As these numbers move closer to 90, the servo will move slower in that direction.
  90.     delay(2000);                              // 3. waits 2000 milliseconds (2 sec). change the value in the brackets (2000) for a longer or shorter delay in milliseconds.
  91.     servo360Micro2_2.write(0);    // 4. turns servo CCW in full speed. change the value in the brackets (0) to change the speed. As these numbers move closer to 90, the servo will move slower in that direction.
  92.     delay(2000);                              // 5. waits 2000 milliseconds (2 sec). change the value in the brackets (2000) for a longer or shorter delay in milliseconds.
  93.     servo360Micro2_2.write(90);    // 6. sending 90 stops the servo
  94.     delay(2000);                              // 7. waits 2000 milliseconds (2 sec). change the value in the brackets (2000) for a longer or shorter delay in milliseconds.
  95.     servo360Micro2_2.detach();                    // 8. release the servo to conserve power. When detached the servo will NOT hold it's position under stress.
  96.   }
  97.  
  98.   if (millis() - time0 > timeout)
  99.   {
  100.     menuOption = menu();
  101.   }
  102.  
  103. }
  104.  
  105.  
  106.  
  107. // Menu function for selecting the components to be tested
  108. // Follow serial monitor for instrcutions
  109. char menu()
  110. {
  111.  
  112.   Serial.println(F("\nWhich component would you like to test?"));
  113.   Serial.println(F("(1) PS2 X Y Axis Joystick Module #1"));
  114.   Serial.println(F("(2) PS2 X Y Axis Joystick Module #2"));
  115.   Serial.println(F("(3) Continuous Rotation Micro Servo - FS90R #1"));
  116.   Serial.println(F("(4) Continuous Rotation Micro Servo - FS90R #2"));
  117.   Serial.println(F("(menu) send anything else or press on board reset button\n"));
  118.   while (!Serial.available());
  119.  
  120.   // Read data from serial monitor if received
  121.   while (Serial.available())
  122.   {
  123.     char c = Serial.read();
  124.     if (isAlphaNumeric(c))
  125.     {
  126.  
  127.       if (c == '1')
  128.         Serial.println(F("Now Testing PS2 X Y Axis Joystick Module #1"));
  129.       else if (c == '2')
  130.         Serial.println(F("Now Testing PS2 X Y Axis Joystick Module #2"));
  131.       else if (c == '3')
  132.         Serial.println(F("Now Testing Continuous Rotation Micro Servo - FS90R #1"));
  133.       else if (c == '4')
  134.         Serial.println(F("Now Testing Continuous Rotation Micro Servo - FS90R #2"));
  135.       else
  136.       {
  137.         Serial.println(F("illegal input!"));
  138.         return 0;
  139.       }
  140.       time0 = millis();
  141.       return c;
  142.     }
  143.   }
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement