rzhernandez
Eternal Poster
ang code ko na ito ay maayos at gumagana...ngayon ang gusto kong mangyare magkaroon ng option na mabagal katamtaman at sobrang bilis na button sa mit app ko at dapat pag pinindot ko ang button na iyon...at babagal o bibilis yung ikot ng dc motor ko...narito ang code ko;
#include "BluetoothSerial.h"
// Define the motor pins for back and front wheels
const int backMotorPin1 = 12; // IN3 (Back motor)
const int backMotorPin2 = 14; // IN4 (Back motor)
const int frontMotorPin1 = 27; // IN1 (Front motor for steering)
const int frontMotorPin2 = 26; // IN2 (Front motor for steering)
const int speedControlPin = 16; // PWM pin for speed control
BluetoothSerial SerialBT; // Create a BluetoothSerial object
String command = ""; // Buffer to store incoming command strings
// PWM settings
const int freq = 5000; // Frequency for motor control
const int pwmChannel = 0;
const int resolution = 8;
int dutyCycle = 170; // Set maximum duty cycle (speed)
void setup() {
SerialBT.begin("RcCar"); // Initialize Bluetooth
// Initialize motor pins as outputs
pinMode(backMotorPin1, OUTPUT);
pinMode(backMotorPin2, OUTPUT);
pinMode(frontMotorPin1, OUTPUT);
pinMode(frontMotorPin2, OUTPUT);
// Initialize speed control pin as output
pinMode(speedControlPin, OUTPUT);
// Initialize serial for debugging
Serial.begin(115200);
Serial.println("Bluetooth Initialized. Waiting for connection...");
// Configure PWM channel
ledcSetup(pwmChannel, freq, resolution); // Setup PWM
ledcAttachPin(speedControlPin, pwmChannel); // Attach pin to PWM channel
}
void loop() {
while (SerialBT.available()) { // Check if Bluetooth data is available
char incomingChar = SerialBT.read(); // Read the incoming character
command += incomingChar; // Append the character to the command string
if (command == "F") { // Move forward
moveForward();
Serial.println("Moving Forward");
command = ""; // Clear the command buffer
} else if (command == "SF") { // Stop forward
stopMovement();
Serial.println("Stopping Forward");
command = ""; // Clear the command buffer
} else if (command == "B") { // Move backward
moveBackward();
Serial.println("Moving Backward");
command = ""; // Clear the command buffer
} else if (command == "SB") { // Stop backward
stopMovement();
Serial.println("Stopping Backward");
command = ""; // Clear the command buffer
} else if (command == "L") { // Turn left
turnLeft();
Serial.println("Turning Left");
command = ""; // Clear the command buffer
} else if (command == "SL") { // Stop turning left
stopSteering();
Serial.println("Stopping Left Turn");
command = ""; // Clear the command buffer
} else if (command == "R") { // Turn right
turnRight();
Serial.println("Turning Right");
command = ""; // Clear the command buffer
} else if (command == "SR") { // Stop turning right
stopSteering();
Serial.println("Stopping Right Turn");
command = ""; // Clear the command buffer
} else if (command.startsWith("SPD")) { // Speed control
int speed = command.substring(3).toInt(); // Extract speed value from the command (e.g., SPD100)
setSpeed(speed);
Serial.print("Setting Speed: ");
Serial.println(speed);
command = ""; // Clear the command buffer
}
}
}
// Forward movement for back motor
void moveForward() {
digitalWrite(backMotorPin1, LOW);
digitalWrite(backMotorPin2, HIGH);
ledcWrite(pwmChannel, dutyCycle); // Set PWM for speed
}
// Backward movement for back motor
void moveBackward() {
digitalWrite(backMotorPin1, HIGH);
digitalWrite(backMotorPin2, LOW);
ledcWrite(pwmChannel, dutyCycle); // Set PWM for speed
}
// Left steering using front motor
void turnLeft() {
digitalWrite(frontMotorPin1, LOW);
digitalWrite(frontMotorPin2, HIGH);
}
// Right steering using front motor
void turnRight() {
digitalWrite(frontMotorPin1, HIGH);
digitalWrite(frontMotorPin2, LOW);
}
// Stop all movement (both forward and backward)
void stopMovement() {
digitalWrite(backMotorPin1, LOW);
digitalWrite(backMotorPin2, LOW);
ledcWrite(pwmChannel, 0); // Stop PWM for speed
}
// Stop steering (left or right)
void stopSteering() {
digitalWrite(frontMotorPin1, LOW);
digitalWrite(frontMotorPin2, LOW);
}
// Set the speed using PWM
void setSpeed(int speed) {
dutyCycle = 255; // Always set to maximum speed
Serial.print("Duty Cycle: ");
Serial.println(dutyCycle); // Debugging line to show the duty cycle value
}
#include "BluetoothSerial.h"
// Define the motor pins for back and front wheels
const int backMotorPin1 = 12; // IN3 (Back motor)
const int backMotorPin2 = 14; // IN4 (Back motor)
const int frontMotorPin1 = 27; // IN1 (Front motor for steering)
const int frontMotorPin2 = 26; // IN2 (Front motor for steering)
const int speedControlPin = 16; // PWM pin for speed control
BluetoothSerial SerialBT; // Create a BluetoothSerial object
String command = ""; // Buffer to store incoming command strings
// PWM settings
const int freq = 5000; // Frequency for motor control
const int pwmChannel = 0;
const int resolution = 8;
int dutyCycle = 170; // Set maximum duty cycle (speed)
void setup() {
SerialBT.begin("RcCar"); // Initialize Bluetooth
// Initialize motor pins as outputs
pinMode(backMotorPin1, OUTPUT);
pinMode(backMotorPin2, OUTPUT);
pinMode(frontMotorPin1, OUTPUT);
pinMode(frontMotorPin2, OUTPUT);
// Initialize speed control pin as output
pinMode(speedControlPin, OUTPUT);
// Initialize serial for debugging
Serial.begin(115200);
Serial.println("Bluetooth Initialized. Waiting for connection...");
// Configure PWM channel
ledcSetup(pwmChannel, freq, resolution); // Setup PWM
ledcAttachPin(speedControlPin, pwmChannel); // Attach pin to PWM channel
}
void loop() {
while (SerialBT.available()) { // Check if Bluetooth data is available
char incomingChar = SerialBT.read(); // Read the incoming character
command += incomingChar; // Append the character to the command string
if (command == "F") { // Move forward
moveForward();
Serial.println("Moving Forward");
command = ""; // Clear the command buffer
} else if (command == "SF") { // Stop forward
stopMovement();
Serial.println("Stopping Forward");
command = ""; // Clear the command buffer
} else if (command == "B") { // Move backward
moveBackward();
Serial.println("Moving Backward");
command = ""; // Clear the command buffer
} else if (command == "SB") { // Stop backward
stopMovement();
Serial.println("Stopping Backward");
command = ""; // Clear the command buffer
} else if (command == "L") { // Turn left
turnLeft();
Serial.println("Turning Left");
command = ""; // Clear the command buffer
} else if (command == "SL") { // Stop turning left
stopSteering();
Serial.println("Stopping Left Turn");
command = ""; // Clear the command buffer
} else if (command == "R") { // Turn right
turnRight();
Serial.println("Turning Right");
command = ""; // Clear the command buffer
} else if (command == "SR") { // Stop turning right
stopSteering();
Serial.println("Stopping Right Turn");
command = ""; // Clear the command buffer
} else if (command.startsWith("SPD")) { // Speed control
int speed = command.substring(3).toInt(); // Extract speed value from the command (e.g., SPD100)
setSpeed(speed);
Serial.print("Setting Speed: ");
Serial.println(speed);
command = ""; // Clear the command buffer
}
}
}
// Forward movement for back motor
void moveForward() {
digitalWrite(backMotorPin1, LOW);
digitalWrite(backMotorPin2, HIGH);
ledcWrite(pwmChannel, dutyCycle); // Set PWM for speed
}
// Backward movement for back motor
void moveBackward() {
digitalWrite(backMotorPin1, HIGH);
digitalWrite(backMotorPin2, LOW);
ledcWrite(pwmChannel, dutyCycle); // Set PWM for speed
}
// Left steering using front motor
void turnLeft() {
digitalWrite(frontMotorPin1, LOW);
digitalWrite(frontMotorPin2, HIGH);
}
// Right steering using front motor
void turnRight() {
digitalWrite(frontMotorPin1, HIGH);
digitalWrite(frontMotorPin2, LOW);
}
// Stop all movement (both forward and backward)
void stopMovement() {
digitalWrite(backMotorPin1, LOW);
digitalWrite(backMotorPin2, LOW);
ledcWrite(pwmChannel, 0); // Stop PWM for speed
}
// Stop steering (left or right)
void stopSteering() {
digitalWrite(frontMotorPin1, LOW);
digitalWrite(frontMotorPin2, LOW);
}
// Set the speed using PWM
void setSpeed(int speed) {
dutyCycle = 255; // Always set to maximum speed
Serial.print("Duty Cycle: ");
Serial.println(dutyCycle); // Debugging line to show the duty cycle value
}