Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- #include <DFRobotDFPlayerMini.h>
- #include <SoftwareSerial.h>
- SoftwareSerial fxSerial(10, 11); // RX, TX...connect rx to tx of df player from arduino and vice versa
- DFRobotDFPlayerMini fxPlayer;
- Servo myServo;
- int servoPin = A0;
- int potPin = A1; // Potentiometer pin
- int ledPin = A2;
- const int stepsPerRevolution = 200;
- const int stepPin = 5;
- const int dirPin = 2;
- const int enPin = 8;
- String cmd;
- int screenWidth = 1280;
- int screenHeight = 720;
- int currentPosition = 0;
- int currentPotPosition = 0;
- int currentServoPosition = 90;
- bool pot_ON = false;
- bool ledState = false; // Track the LED state
- bool gestureDetected = false; // Track gesture detection
- void setup() {
- Serial.begin(115200);
- fxSerial.begin(9600); // Keep the baud rate for SoftwareSerial at 9600
- fxPlayer.begin(fxSerial);
- fxPlayer.volume(30);
- pinMode(stepPin, OUTPUT);
- pinMode(dirPin, OUTPUT);
- pinMode(enPin, OUTPUT);
- pinMode(ledPin, OUTPUT);
- digitalWrite(enPin, LOW);
- digitalWrite(stepPin, LOW);
- digitalWrite(dirPin, LOW);
- myServo.attach(servoPin);
- myServo.write(currentServoPosition);
- Serial.println("Setup complete");
- }
- void loop() {
- if (Serial.available()) {
- cmd = Serial.readStringUntil('\n');
- gestureDetected = true;
- if (cmd == "LED_ON" && !ledState) { // Ensure state change
- digitalWrite(ledPin, HIGH); // Turn on LED
- fxPlayer.play(13); // Play bang sound
- ledState = true;
- } else if (cmd == "LED_OFF" && ledState) { // Ensure state change
- digitalWrite(ledPin, LOW); // Turn off LED
- ledState = false;
- } else {
- int delimiterIndex = cmd.indexOf(',');
- if (delimiterIndex > 0) { // Validate input
- int centerX = cmd.substring(0, delimiterIndex).toInt();
- int centerY = cmd.substring(delimiterIndex + 1).toInt();
- int servoPosition = map(centerY, 0, screenHeight, 70, 115); // 360-screen Height
- if (servoPosition != currentServoPosition) { // Only move if different
- myServo.write(servoPosition);
- currentServoPosition = servoPosition;
- }
- int mappedValue = map(centerX, 0, screenWidth, -stepsPerRevolution / 8, stepsPerRevolution / 8); // 640-screen Width
- int stepsToMove = mappedValue - currentPosition;
- if (stepsToMove != 0) { // Only move if different
- moveStepper(stepsToMove);
- currentPosition = mappedValue;
- }
- delay(10);
- }
- }
- } else {
- if (!gestureDetected) { // No gesture detected
- digitalWrite(enPin, LOW); // Disable the stepper driver
- digitalWrite(stepPin, LOW);
- digitalWrite(dirPin, LOW);
- Serial.println("enPin set to LOW");
- }
- }
- potentiometer();
- gestureDetected = false; // Reset gesture detection
- }
- void moveStepper(int steps) {
- digitalWrite(dirPin, steps > 0 ? HIGH : LOW); // Set direction
- steps = abs(steps);
- for (int i = 0; i < steps; i++) {
- digitalWrite(stepPin, HIGH);
- delayMicroseconds(500); // Adjust delay to suit the motor's specifications
- digitalWrite(stepPin, LOW);
- delayMicroseconds(500);
- pot_ON = false;
- }
- }
- void movePotentiometer(int steps) {
- digitalWrite(dirPin, steps > 0 ? HIGH : LOW); // Set direction
- steps = abs(steps);
- for (int i = 0; i < steps; i++) {
- digitalWrite(stepPin, HIGH);
- delayMicroseconds(500);
- digitalWrite(stepPin, LOW);
- delayMicroseconds(500);
- pot_ON = true;
- }
- }
- void potentiometer() {
- int potValue = analogRead(potPin); // Read potentiometer value
- int mappedPotSteps = map(potValue, 0, 1023, -stepsPerRevolution / 8, stepsPerRevolution / 8);
- int stepsPotToMove = mappedPotSteps - currentPotPosition;
- movePotentiometer(stepsPotToMove);
- currentPotPosition = mappedPotSteps;
- delay(10);
- pot_ON = true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement