Advertisement
rhandycan1

Arduino_mouseclick

Feb 15th, 2025
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | Source Code | 0 0
  1. #include <Servo.h>
  2. #include <DFRobotDFPlayerMini.h>
  3. #include <SoftwareSerial.h>
  4.  
  5. SoftwareSerial fxSerial(10, 11); // RX, TX...connect rx to tx of df player from arduino and vice versa
  6. DFRobotDFPlayerMini fxPlayer;
  7.  
  8. Servo myServo;
  9. int servoPin = A0;
  10. int potPin = A1; // Potentiometer pin
  11. int ledPin = A2;
  12.  
  13. const int stepsPerRevolution = 200;
  14. const int stepPin = 5;
  15. const int dirPin = 2;
  16. const int enPin = 8;
  17. String cmd;
  18.  
  19. int screenWidth = 1280;
  20. int screenHeight = 720;
  21. int currentPosition = 0;
  22. int currentPotPosition = 0;
  23. int currentServoPosition = 90;
  24. bool pot_ON = false;
  25. bool ledState = false; // Track the LED state
  26. bool gestureDetected = false; // Track gesture detection
  27.  
  28. void setup() {
  29. Serial.begin(115200);
  30. fxSerial.begin(9600); // Keep the baud rate for SoftwareSerial at 9600
  31. fxPlayer.begin(fxSerial);
  32. fxPlayer.volume(30);
  33. pinMode(stepPin, OUTPUT);
  34. pinMode(dirPin, OUTPUT);
  35. pinMode(enPin, OUTPUT);
  36. pinMode(ledPin, OUTPUT);
  37. digitalWrite(enPin, LOW);
  38. digitalWrite(stepPin, LOW);
  39. digitalWrite(dirPin, LOW);
  40. myServo.attach(servoPin);
  41. myServo.write(currentServoPosition);
  42. Serial.println("Setup complete");
  43. }
  44.  
  45. void loop() {
  46. if (Serial.available()) {
  47. cmd = Serial.readStringUntil('\n');
  48. gestureDetected = true;
  49.  
  50. if (cmd == "LED_ON" && !ledState) { // Ensure state change
  51. digitalWrite(ledPin, HIGH); // Turn on LED
  52. fxPlayer.play(13); // Play bang sound
  53. ledState = true;
  54. } else if (cmd == "LED_OFF" && ledState) { // Ensure state change
  55. digitalWrite(ledPin, LOW); // Turn off LED
  56. ledState = false;
  57. } else {
  58. int delimiterIndex = cmd.indexOf(',');
  59. if (delimiterIndex > 0) { // Validate input
  60. int centerX = cmd.substring(0, delimiterIndex).toInt();
  61. int centerY = cmd.substring(delimiterIndex + 1).toInt();
  62.  
  63. int servoPosition = map(centerY, 0, screenHeight, 70, 115); // 360-screen Height
  64. if (servoPosition != currentServoPosition) { // Only move if different
  65. myServo.write(servoPosition);
  66. currentServoPosition = servoPosition;
  67. }
  68.  
  69. int mappedValue = map(centerX, 0, screenWidth, -stepsPerRevolution / 8, stepsPerRevolution / 8); // 640-screen Width
  70. int stepsToMove = mappedValue - currentPosition;
  71.  
  72. if (stepsToMove != 0) { // Only move if different
  73. moveStepper(stepsToMove);
  74. currentPosition = mappedValue;
  75. }
  76. delay(10);
  77. }
  78. }
  79. } else {
  80. if (!gestureDetected) { // No gesture detected
  81. digitalWrite(enPin, LOW); // Disable the stepper driver
  82. digitalWrite(stepPin, LOW);
  83. digitalWrite(dirPin, LOW);
  84. Serial.println("enPin set to LOW");
  85. }
  86. }
  87. potentiometer();
  88. gestureDetected = false; // Reset gesture detection
  89. }
  90.  
  91. void moveStepper(int steps) {
  92. digitalWrite(dirPin, steps > 0 ? HIGH : LOW); // Set direction
  93. steps = abs(steps);
  94. for (int i = 0; i < steps; i++) {
  95. digitalWrite(stepPin, HIGH);
  96. delayMicroseconds(500); // Adjust delay to suit the motor's specifications
  97. digitalWrite(stepPin, LOW);
  98. delayMicroseconds(500);
  99. pot_ON = false;
  100. }
  101. }
  102.  
  103. void movePotentiometer(int steps) {
  104. digitalWrite(dirPin, steps > 0 ? HIGH : LOW); // Set direction
  105. steps = abs(steps);
  106. for (int i = 0; i < steps; i++) {
  107. digitalWrite(stepPin, HIGH);
  108. delayMicroseconds(500);
  109. digitalWrite(stepPin, LOW);
  110. delayMicroseconds(500);
  111. pot_ON = true;
  112. }
  113. }
  114.  
  115. void potentiometer() {
  116. int potValue = analogRead(potPin); // Read potentiometer value
  117. int mappedPotSteps = map(potValue, 0, 1023, -stepsPerRevolution / 8, stepsPerRevolution / 8);
  118. int stepsPotToMove = mappedPotSteps - currentPotPosition;
  119. movePotentiometer(stepsPotToMove);
  120. currentPotPosition = mappedPotSteps;
  121. delay(10);
  122. pot_ON = true;
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement