0% found this document useful (0 votes)
15 views

Codigos Retos ESP32

Uploaded by

keditol182
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Codigos Retos ESP32

Uploaded by

keditol182
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

## 1.

LED Simple
```cpp
const int LED = 15;
bool estado = false;

void setup() {
pinMode(LED, OUTPUT);
}

void loop() {
estado = !estado;
digitalWrite(LED, estado);
delay(500);
}
```

## 2. Control de Intensidad
```cpp
const int LED = 16;
const int POT = 35;

void setup() {
ledcAttachPin(LED, 0);
ledcSetup(0, 1000, 8);
}

void loop() {
int valor = analogRead(POT) / 16;
ledcWrite(0, valor);
delay(50);
}
```

## 3. LED Velocidad Variable


```cpp
const int LED = 17;
const int SENSOR = 36;

void setup() {
pinMode(LED, OUTPUT);
}

void loop() {
int velocidad = analogRead(SENSOR);
digitalWrite(LED, HIGH);
delay(velocidad);
digitalWrite(LED, LOW);
delay(velocidad);
}
```

## 4. Secuencia Simple
```cpp
const int LEDS[] = {21, 22, 23};

void setup() {
for(int i = 0; i < 3; i++) {
pinMode(LEDS[i], OUTPUT);
}
}

void loop() {
for(int i = 0; i < 3; i++) {
digitalWrite(LEDS[i], HIGH);
delay(300);
digitalWrite(LEDS[i], LOW);
}
}
```

## 5. LED con Botón


```cpp
const int LED = 25;
const int BTN = 26;
int anterior = HIGH;

void setup() {
pinMode(LED, OUTPUT);
pinMode(BTN, INPUT_PULLUP);
}

void loop() {
int actual = digitalRead(BTN);
if(actual != anterior && actual == LOW) {
digitalWrite(LED, !digitalRead(LED));
}
anterior = actual;
delay(50);
}
```

## 6. LED Táctil
```cpp
const int LED = 27;
const int TOUCH = T0;
void setup() {
pinMode(LED, OUTPUT);
}

void loop() {
digitalWrite(LED, touchRead(TOUCH) < 40);
delay(100);
}
```

## 7. Contador Binario
```cpp
const int PINES[] = {1, 3, 5, 18, 19, 21, 22, 23};
byte valor = 0;

void setup() {
for(int i = 0; i < 8; i++) {
pinMode(PINES[i], OUTPUT);
}
}

void loop() {
for(int i = 0; i < 8; i++) {
digitalWrite(PINES[i], valor & (1 << i));
}
valor++;
delay(500);
}
```

## 8. Semáforo Básico
```cpp
enum LED {ROJO = 32, AMARILLO = 33, VERDE = 34};
const LED LUCES[] = {ROJO, AMARILLO, VERDE};
const int TIEMPOS[] = {3000, 1000, 3000};

void setup() {
for(int i = 0; i < 3; i++) {
pinMode(LUCES[i], OUTPUT);
}
}

void loop() {
for(int i = 0; i < 3; i++) {
digitalWrite(LUCES[i], HIGH);
delay(TIEMPOS[i]);
digitalWrite(LUCES[i], LOW);
}
}
```

## 9. Control Motor Simple


```cpp
const int MOTOR = 13;
const int CONTROL = 39;

void setup() {
ledcAttachPin(MOTOR, 1);
ledcSetup(1, 500, 8);
}

void loop() {
int velocidad = analogRead(CONTROL) / 16;
ledcWrite(1, velocidad);
delay(100);
}
```

## 10. Robot Básico


```cpp
const int MOTORES[] = {4, 5, 6, 7};
const int BOTONES[] = {8, 9, 10};

void setup() {
for(int i = 0; i < 4; i++) pinMode(MOTORES[i], OUTPUT);
for(int i = 0; i < 3; i++) pinMode(BOTONES[i], INPUT_PULLUP);
}

void mover(bool m1, bool m2) {


digitalWrite(MOTORES[0], m1);
digitalWrite(MOTORES[1], !m1);
digitalWrite(MOTORES[2], m2);
digitalWrite(MOTORES[3], !m2);
}

void loop() {
if(!digitalRead(BOTONES[0])) mover(1, 0); // Izquierda
else if(!digitalRead(BOTONES[1])) mover(1, 1); // Adelante
else if(!digitalRead(BOTONES[2])) mover(0, 1); // Derecha
else mover(0, 0); // Parar
delay(100);
}
```

You might also like