Robot Control with Ultrasonic Sensor


#include // Librería para el sensor ultrasónico
// Configuración del sensor ultrasónico #define TRIG 8 #define ECHO 9 #define DISTANCIA_MAXIMA 200 // Distancia máxima a medir (en cm) NewPing sonar(TRIG, ECHO, DISTANCIA_MAXIMA);
// Pines para el puente H L298N #define IN1 2 #define IN2 3 #define IN3 4 #define IN4 5
// Distancia para detectar al oponente (en cm) #define DISTANCIA_DETECCION 20
// Tiempo de giro para búsqueda activa (en ms) #define TIEMPO_GIRO 500
void setup() { // Configura los pines del puente H como salidas pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);
// Inicia el robot detenido detener(); }
void loop() { // Mide la distancia al oponente unsigned int distancia = sonar.ping_cm();
// Si detecta al oponente dentro del rango, avanza if (distancia > 0 && distancia = DISTANCIA_DETECCION) { girarIzquierda(); delay(TIEMPO_GIRO); detener(); } }
It looks like you've shared an Arduino sketch for a robot using an ultrasonic sensor and an L298N H-bridge motor driver. How can I help you with this project? For example, would you like to create a corresponding schematic in Flux by adding the related components (e.g., an ultrasonic sensor like HC-SR04 and the L298N) or would you prefer assistance with another aspect of your design?
Project Description
Select Main Components
Set Project Properties
No schematic yet

Talk to Flux to get started.