Activity: Bringing the Robot Arm to Life!
You have been learning how the for
loop makes our robot move smoothly instead of jumping suddenly. Please answer
the following 5 questions in your notebook:
1.
The power of the loop: In your own
words, what happens to the motor when we use a for loop with a delay(20)
inside, instead of just telling it "go to 180 degrees"?
2.
Adjusting the speed: If you wanted
the robot to move slower than a snail, which value in the code would you
change, and why?
3.
Understanding limits: The robot
currently moves from 0 to 180 degrees. What do you think would happen if we
changed the condition i < 180 to i < 90?
4.
The brain of the operation: We use
the PCA9685 board to control the motors. Why don't we need to plug each motor
into a different digital pin on the Arduino (like pin 2, 3, or 4)?
5.
Time for the real test: Go to Wokwi,
build a simple circuit with your Arduino, the PCA9685 board, and at least one
servo motor. Copy and paste the code, run it, and see if the movement matches
what you explained in point 1.
WOKWI CODE
#include <ESP32Servo.h>
// 1. Creamos los objetos para cada motor
Servo servoBase;
Servo servoHombro;
Servo servoCodo;
// 2. Definimos los pines PWM (asegúrate de que en Wokwi estén en estos pines)
const int PIN_BASE = 18;
const int PIN_HOMBRO = 19;
const int PIN_CODO = 21;
void setup() {
// 3. Conectamos cada objeto a su pin físico
servoBase.attach(PIN_BASE);
servoHombro.attach(PIN_HOMBRO);
servoCodo.attach(PIN_CODO);
// Posición inicial de los tres motores
servoBase.write(90);
servoHombro.write(90);
servoCodo.write(90);
}
void loop() {
// Mover todos al mismo tiempo con un solo bucle FOR
// Ejemplo: Mover todos desde 90 hasta 180
for (int i = 90; i <= 180; i++) {
servoBase.write(i);
servoHombro.write(i);
servoCodo.write(i);
delay(20);
}
delay(1000);
// Regresar todos a 90
for (int i = 180; i >= 90; i--) {
servoBase.write(i);
servoHombro.write(i);
servoCodo.write(i);
delay(20);
}
delay(1000);
}
| Servomotor | Cable de Señal (Amarillo/Naranja) | Cable Rojo (VCC) | Cable Negro/Marrón (GND) |
| Base | Pin 18 | 5V | GND |
| Hombro | Pin 19 | 5V | GND |
| Codo | Pin 21 | 5V | GND |
_______________________________________________________________________________
ARDUINO CODE
/*
| Componente | Pin del Componente | Pin en Arduino |
| PCA9685 | VCC | 5V |
| PCA9685 | GND | GND |
| PCA9685 | SCL | A5 |
| PCA9685 | SDA | A4 |
| Servos | Cable Rojo (V+) | Fuente externa 5V-6V (+) |
| Servos | Cable Negro/Café (GND) | Fuente externa 5V-6V (-) |
| Servos | Cable Naranja/Blanco (Señal) | Pines 0, 4, 8 de la tarjeta |
No hay comentarios:
Publicar un comentario