const int motorA1 = 9; const int motorA2 = 8; const int motorPWM = 10; const int potPin = A0; // Pin del potenciómetro const int switchPin = 12; // Pin del interruptor de balancín const int speakerPin = 5; // Pin del altavoz const int ledPin = 6; // Pin del LED void setup() { pinMode(motorA1, OUTPUT); pinMode(motorA2, OUTPUT); pinMode(motorPWM, OUTPUT); pinMode(potPin, INPUT); pinMode(switchPin, INPUT_PULLUP); pinMode(speakerPin, OUTPUT); pinMode(ledPin, OUTPUT); } void loop() { // Leer el estado del interruptor de balancín int switchState = digitalRead(switchPin); // Si el interruptor está cerrado (en "ON") if (switchState == LOW) { // Encender el LED digitalWrite(ledPin, HIGH); // Lee el valor del potenciómetro int potValue = analogRead(potPin); // Convierte el valor leído (0-1023) a un valor de velocidad (0-255) int speed = map(potValue, 0, 1023, 0, 255); // Encender el motor en dirección hacia adelante digitalWrite(motorA1, HIGH); digitalWrite(motorA2, LOW); // Establecer la velocidad del motor analogWrite(motorPWM, speed); // Encender el altavoz cuando la velocidad del motor sea mediana if (speed >= 100 && speed <= 200) { digitalWrite(speakerPin, HIGH); } else { digitalWrite(speakerPin, LOW); } } else { // Si el interruptor está abierto, apagar el motor, el altavoz y el LED digitalWrite(ledPin, LOW); digitalWrite(motorA1, LOW); digitalWrite(motorA2, LOW); analogWrite(motorPWM, 0); digitalWrite(speakerPin, LOW); } }