@Rosco Not sure if the code came through properly. Tip: Use Markdown to “fence” the code in a code block, like this:
```c
(code)
```
(Not sure if that will come out right, here is a screenshot:
)
@Rosco Not sure if the code came through properly. Tip: Use Markdown to “fence” the code in a code block, like this:
```c
(code)
```
(Not sure if that will come out right, here is a screenshot:
)
I dont really know how to use markdown to do this.
‘’’#include “MeAuriga.h” // Includes the MeAuriga library which contains functions to control MakeBlock’s Auriga board
#include <Wire.h> // Includes the Wire library for I2C communication
#include <SoftwareSerial.h> // Includes the SoftwareSerial library for serial communication using digital pins
MeEncoderOnBoard motor1(SLOT1); // Motor at slot1 // Creates an instance of the MeEncoderOnBoard class for motor1, connected to slot 1
MeEncoderOnBoard motor2(SLOT2); // Motor at slot2 // Creates an instance of the MeEncoderOnBoard class for motor2, connected to slot 2
MeUltrasonicSensor ultraSensor(PORT_10); // Creates an instance of the MeUltrasonicSensor class for an ultrasonic sensor connected to port 10
MeGyro gyro(0, 0x69); // Creates an instance of the MeGyro class for a gyro sensor, initialized with address 0x69
void setup() {
Serial.begin(9600); // Begin serial communication at 9600 baud
gyro.begin(); // Initialize the gyro sensor
}
void stop() {
motor1.setMotorPwm(0);
motor2.setMotorPwm(0); // Sets the PWM values of both motors to 0, stopping the robot
}
void fwd() {
motor1.setMotorPwm(-120);
motor2.setMotorPwm(120); // Sets the PWM values to move the robot forward
}
void turnRight() {
int currentAngle = gyro.getAngleZ(); // Gets the current Z-axis angle from the gyro sensor
int targetAngle = (int)(currentAngle + 90) % 360; // Calculates the target angle 90 degrees to the right
while (abs(currentAngle - targetAngle) > 5) {
gyro.update(); // Updates the gyro sensor readings
currentAngle = gyro.getAngleZ(); // Gets the updated current Z-axis angle from the gyro sensor
if (targetAngle > currentAngle) {
motor1.setMotorPwm(195);
motor2.setMotorPwm(195); // Sets the PWM values to turn the robot to the right
}
}
stop(); // Stops the robot after completing the turn
gyro.begin(); // Reinitializes the gyro sensor
}
void turnLeft() {
int currentAngle = gyro.getAngleZ(); // Gets the current Z-axis angle from the gyro sensor
int targetAngle = (int)(currentAngle - 90) % 360; // Calculates the target angle 90 degrees to the left
while (abs(currentAngle - targetAngle) > 5) {
gyro.update(); // Updates the gyro sensor readings
currentAngle = gyro.getAngleZ(); // Gets the updated current Z-axis angle from the gyro sensor
if (targetAngle < currentAngle) {
motor1.setMotorPwm(-195);
motor2.setMotorPwm(-195);// Sets the PWM values to turn the robot to the left
}
}
stop(); // Stops the robot after completing the turn
gyro.begin(); // Reinitializes the gyro sensor
}
void turnAround() {
gyro.begin(); // Initializes the gyro sensor
turnRight(); // Turns the robot to the right by 90 degrees
delay(500); // Short delay to ensure the turn completes
turnRight(); // Turns the robot to the right by another 90 degrees to complete a 180-degree turn
}
‘’’
#include <Wire.h> // Includes the Wire library for I2C communication
#include <SoftwareSerial.h> // Includes the SoftwareSerial library for serial communication using digital pins
MeEncoderOnBoard motor1(SLOT1); // Motor at slot1 // Creates an instance of the MeEncoderOnBoard class for motor1, connected to slot 1
MeEncoderOnBoard motor2(SLOT2); // Motor at slot2 // Creates an instance of the MeEncoderOnBoard class for motor2, connected to slot 2
MeUltrasonicSensor ultraSensor(PORT_10); // Creates an instance of the MeUltrasonicSensor class for an ultrasonic sensor connected to port 10
MeGyro gyro(0, 0x69); // Creates an instance of the MeGyro class for a gyro sensor, initialized with address 0x69
void setup() {
Serial.begin(9600); // Begin serial communication at 9600 baud
gyro.begin(); // Initialize the gyro sensor
}
void stop() {
motor1.setMotorPwm(0);
motor2.setMotorPwm(0); // Sets the PWM values of both motors to 0, stopping the robot
}
void fwd() {
motor1.setMotorPwm(-120);
motor2.setMotorPwm(120); // Sets the PWM values to move the robot forward
}
void turnRight() {
int currentAngle = gyro.getAngleZ(); // Gets the current Z-axis angle from the gyro sensor
int targetAngle = (int)(currentAngle + 90) % 360; // Calculates the target angle 90 degrees to the right
while (abs(currentAngle - targetAngle) > 5) {
gyro.update(); // Updates the gyro sensor readings
currentAngle = gyro.getAngleZ(); // Gets the updated current Z-axis angle from the gyro sensor
if (targetAngle > currentAngle) {
motor1.setMotorPwm(195);
motor2.setMotorPwm(195); // Sets the PWM values to turn the robot to the right
}
}
stop(); // Stops the robot after completing the turn
gyro.begin(); // Reinitializes the gyro sensor
}
void turnLeft() {
int currentAngle = gyro.getAngleZ(); // Gets the current Z-axis angle from the gyro sensor
int targetAngle = (int)(currentAngle - 90) % 360; // Calculates the target angle 90 degrees to the left
while (abs(currentAngle - targetAngle) > 5) {
gyro.update(); // Updates the gyro sensor readings
currentAngle = gyro.getAngleZ(); // Gets the updated current Z-axis angle from the gyro sensor
if (targetAngle < currentAngle) {
motor1.setMotorPwm(-195);
motor2.setMotorPwm(-195);// Sets the PWM values to turn the robot to the left
}
}
stop(); // Stops the robot after completing the turn
gyro.begin(); // Reinitializes the gyro sensor
}
void turnAround() {
gyro.begin(); // Initializes the gyro sensor
turnRight(); // Turns the robot to the right by 90 degrees
delay(500); // Short delay to ensure the turn completes
turnRight(); // Turns the robot to the right by another 90 degrees to complete a 180-degree turn
}
I would have edited your previous message instead of sending a new one.
(I see you got it )
As my name implies, I am a coder, so I shouldn’t get too confused .
I have a GitHub here!
I’ll test that code when I get a chance, I have to find all the parts and re-build my mBot Ranger.