Advertisement
pleasedontcode

Bluetooth Motor rev_01

Jun 8th, 2024
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Bluetooth Motor
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-08 10:03:21
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* motors go forward and backward */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <SoftwareSerial.h>
  25. #include <NXTBluetooth.h> //https://github.com/Aidywady/NXTBluetooth
  26.  
  27. /****** FUNCTION PROTOTYPES *****/
  28. void setup(void);
  29. void loop(void);
  30.  
  31. /***** DEFINITION OF Software Serial *****/
  32. const uint8_t bt_HC05_mySerial_PIN_SERIAL_TX_A0 = A0;
  33. const uint8_t bt_HC05_mySerial_PIN_SERIAL_RX_A1 = A1;
  34. SoftwareSerial bt_HC05_mySerial(bt_HC05_mySerial_PIN_SERIAL_RX_A1, bt_HC05_mySerial_PIN_SERIAL_TX_A0);
  35.  
  36. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  37. NXTBluetooth bluetooth(bt_HC05_mySerial_PIN_SERIAL_RX_A1, bt_HC05_mySerial_PIN_SERIAL_TX_A0, 1, 1);
  38.  
  39. /****** MOTOR CONTROL PINS *****/
  40. const uint8_t motorPin1 = 9; // Motor pin 1
  41. const uint8_t motorPin2 = 10; // Motor pin 2
  42.  
  43. void setup(void)
  44. {
  45.     // put your setup code here, to run once:
  46.     bt_HC05_mySerial.begin(9600); // Initialize SoftwareSerial for Bluetooth
  47.     bluetooth.begin(9600); // Initialize NXTBluetooth
  48.     Serial.begin(9600); // Initialize Serial for debugging
  49.     Serial.println("Beginning communication with NXT");
  50.  
  51.     // Initialize motor control pins
  52.     pinMode(motorPin1, OUTPUT);
  53.     pinMode(motorPin2, OUTPUT);
  54. }
  55.  
  56. void loop(void)
  57. {
  58.     // put your main code here, to run repeatedly:
  59.  
  60.     // Example motor control logic
  61.     // Move forward
  62.     digitalWrite(motorPin1, HIGH);
  63.     digitalWrite(motorPin2, LOW);
  64.     delay(2000); // Move forward for 2 seconds
  65.  
  66.     // Move backward
  67.     digitalWrite(motorPin1, LOW);
  68.     digitalWrite(motorPin2, HIGH);
  69.     delay(2000); // Move backward for 2 seconds
  70.  
  71.     // Stop motors
  72.     digitalWrite(motorPin1, LOW);
  73.     digitalWrite(motorPin2, LOW);
  74.     delay(1000); // Stop for 1 second
  75.  
  76.     // Communication with NXT
  77.     String runTime = String((float) millis() / 1000, 3);
  78.     bluetooth.write(runTime, 0);
  79.  
  80.     if (bluetooth.available(0)) {
  81.         Serial.println(bluetooth.read(0, true));
  82.     }
  83.  
  84.     bluetooth.update();
  85. }
  86.  
  87. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement