0% found this document useful (0 votes)
17 views18 pages

Practicas (01-28) Programaciones

The document contains 12 practices describing Arduino code to control LEDs through various methods. Practice 1 turns an LED on and off with a delay. Practice 2 controls two LEDs independently in the same way. Practice 3 extends this to three LEDs. Later practices control LEDs using arrays, for loops, simulating a car's lights, through keyboard input, printing status, and with a button. The practices progress from basic to more advanced LED control techniques.

Uploaded by

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

Practicas (01-28) Programaciones

The document contains 12 practices describing Arduino code to control LEDs through various methods. Practice 1 turns an LED on and off with a delay. Practice 2 controls two LEDs independently in the same way. Practice 3 extends this to three LEDs. Later practices control LEDs using arrays, for loops, simulating a car's lights, through keyboard input, printing status, and with a button. The practices progress from basic to more advanced LED control techniques.

Uploaded by

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

//PRACTICA 01: "HOLA MUNDO"

#define Led 13

void setup() {

pinMode(Led, OUTPUT);
}

void loop() {
digitalWrite(Led, HIGH);
delay(2000);
digitalWrite(Led, LOW);
delay(2000);
}
------------------------------------------------------------

/PRACTICA 02: "CONTROL DE 2 LED´S"

#define Led1 13
#define Led2 12

void setup() {
pinMode(Led1, OUTPUT);
pinMode(Led2, OUTPUT);
}

void loop() {
// Primer Led
digitalWrite(Led1, HIGH);
delay(2000);
digitalWrite(Led1, LOW);
delay(2000);
// Segundo Led
digitalWrite(Led2, HIGH);
delay(2000);
digitalWrite(Led2, LOW);
delay(2000);
}
---------------------------------------------------------------

/PRACTICA 03: "CONTROL DE 3 LED´S"

#define Led1 13
#define Led2 12
#define Led3 11

void setup() {
pinMode(Led1, OUTPUT);
pinMode(Led2, OUTPUT);
pinMode(Led3, OUTPUT);
}

void loop() {
// Primer Led
digitalWrite(Led1, HIGH);
delay(5000);
digitalWrite(Led1, LOW);
delay(0000);
// Segundo Led
digitalWrite(Led2, HIGH);
delay(5000);
digitalWrite(Led2, LOW);
delay(0000);
//Tercer Led
digitalWrite(Led3, HIGH);
delay(5000);
digitalWrite(Led3, LOW);
delay(0000);
}
-------------------------------------------------------------------

//PRACTICA 04: "CONTROL DE SEMÁFORO"

const byte Led[] = {10, 9, 8};

void setup() {
pinMode(Led[0], OUTPUT);
pinMode(Led[1], OUTPUT);
pinMode(Led[2], OUTPUT);
}

void loop() {
digitalWrite(Led[0], HIGH);
delay(5000);
digitalWrite(Led[0], LOW);
delay(250);
digitalWrite(Led[0], HIGH);
delay(250);
digitalWrite(Led[0], LOW);
delay(250);
digitalWrite(Led[0], HIGH);
delay(250);
digitalWrite(Led[0], LOW);
delay(250);
digitalWrite(Led[0], HIGH);
delay(250);
digitalWrite(Led[0], LOW);
delay(250);
digitalWrite(Led[0], HIGH);
delay(250);
digitalWrite(Led[0], LOW);
digitalWrite(Led[1], HIGH);
delay(2000);
digitalWrite(Led[1], LOW);
digitalWrite(Led[2], HIGH);
delay(5000);
digitalWrite(Led[2], LOW);
}
---------------------------------------------

/PRACTICA 05: "ENCENDIDO SECUENCIAL DE 5 LED´S"

const byte Led[]={13, 12, 11, 10, 9};

void setup() {
pinMode(Led[0], OUTPUT);
pinMode(Led[1], OUTPUT);
pinMode(Led[2], OUTPUT);
pinMode(Led[3], OUTPUT);
pinMode(Led[4], OUTPUT);
}

void loop() {
digitalWrite(Led[0], HIGH);
delay(200);
digitalWrite(Led[1], HIGH);
delay(200);
digitalWrite(Led[2], HIGH);
delay(200);
digitalWrite(Led[3], HIGH);
delay(200);
digitalWrite(Led[4], HIGH);
delay(500);
digitalWrite(Led[4], LOW);
delay(200);
digitalWrite(Led[3], LOW);
delay(200);
digitalWrite(Led[2], LOW);
delay(200);
digitalWrite(Led[1], LOW);
delay(200);
digitalWrite(Led[0], LOW);
delay(500);
}
-----------------------------------------------------

//PRACTICA 06: "ENCENDIDO SECUENCIAL DE 5 LED´S UTILIZANDO ARREGLOS Y ESTRUCTURA


FOR"

const byte Led[] = {13, 12, 11, 10, 9};

void setup() {
for (byte x = 0; x < 5; x++) {
pinMode(Led[x], OUTPUT);
}
}

void loop() {
for (byte x = 0; x < 5; x++) {
digitalWrite(Led[x], HIGH);
delay(200);
}
delay(300);
for (int x = 4; x >= 0; x--) {
digitalWrite(Led[x], LOW);
delay(200);
}
delay(300);
}
--------------------------------------------------------------------------------

//PRACTICA 07: "CONTROL DE LED´S SIMULANDO EL COMPORTAMIENTO DEL AUTO FANTÁSTICO"

const byte Led[] = {13, 12, 11, 10, 9, 8, 7, 6, 5};

void setup() {
for (byte x = 0; x < 9; x++) {
pinMode(Led[x], OUTPUT);
}
}

void loop() {
for (byte x = 0; x < 9; x++) {
digitalWrite (Led[x], HIGH);
delay (35);
digitalWrite (Led[x], LOW);
}
for (byte x = 7; x >= 1; x--) {
digitalWrite(Led[x], HIGH);
delay (35);
digitalWrite (Led[x], LOW);
}
}
----------------------------------------------------------------------

//PRACTICA 08: "CONTROL DE LED UTILIZANDO EL TECLADO"

const byte Led[] = {13, 12, 11, 10, 9, 8, 7, 6, 5};


int Tecla = 0;

void setup() {
for (byte x = 0; x < 9; x++) {
pinMode(Led[x], OUTPUT);
}
Serial.begin(9600);
}

void loop() {
if (Serial.available()) {
Tecla = Serial.read();
switch (Tecla) {
case '1':
digitalWrite(Led[0], HIGH);
break;
case '2':
digitalWrite(Led[1], HIGH);
break;
case '3':
digitalWrite(Led[2], HIGH);
break;
case '4':
digitalWrite(Led[3], HIGH);
break;
case '5':
digitalWrite(Led[4], HIGH);
break;
case '6':
digitalWrite(Led[5], HIGH);
break;
case '7':
digitalWrite(Led[6], HIGH);
break;
case '8':
digitalWrite(Led[7], HIGH);
break;
case '9':
digitalWrite(Led[8], HIGH);
break;
case '0':
for (byte x = 0; x < 9; x++) {
digitalWrite(Led[x], LOW);
}
break;
}
}
}
-----------------------------------------------------------

/PRACTICA 09: "IMPRIMIR EL NÚMERO DE LED ENCENDIDO"

const byte Led[] = {13, 12, 11, 10, 9, 8, 7, 6, 5};


int Tecla = 0;

void setup() {
for (byte x = 0; x < 9; x++) {
pinMode(Led[x], OUTPUT);
}
Serial.begin(9600);
Serial.println("Se inicia el programa ...");
}

void loop() {
if (Serial.available()) {
Tecla = Serial.read();
switch (Tecla) {
case '0':
for (byte x = 0; x < 9; x++) {
digitalWrite(Led[x], LOW);
Serial.println("Se apago el Led ");
}
break;
case '1':
digitalWrite(Led[0], HIGH);
Serial.println("Se ha encendido la tecla 1");
break;
case '2':
digitalWrite(Led[1], HIGH);
Serial.println("Se ha encendido la tecla 2");
break;
case '3':
digitalWrite(Led[2], HIGH);
Serial.println("Se ha encendido la tecla 3");
break;
case '4':
digitalWrite(Led[3], HIGH);
Serial.println("Se ha encendido la tecla 4");
break;
case '5':
digitalWrite(Led[4], HIGH);
Serial.println("Se ha encendido la tecla 5");
break;
case '6':
digitalWrite(Led[5], HIGH);
Serial.println("Se ha encendido la tecla 6");
break;
case '7':
digitalWrite(Led[6], HIGH);
Serial.println("Se ha encendido la tecla 7");
break;
case '8':
digitalWrite(Led[7], HIGH);
Serial.println("Se ha encendido la tecla 8");
break;
case '9':
digitalWrite(Led[8], HIGH);
Serial.println("Se ha encendido la tecla 9");
break;
}
}
------------------------------------------------------------
//PRACTICA 10: "CONTROL DE UN LED UTILIZANDO EL TECLADO"

const byte Led= 13;


int Tecla = 0;

void setup() {
pinMode(Led, OUTPUT);
Serial.begin(9600);
}

void loop() {
if (Serial.available()) {
Tecla = Serial.read();
switch (Tecla) {
case '1':
if (digitalRead(Led) == LOW) {
digitalWrite(Led, HIGH);
Serial.println("Se enciende el Led 1");
}
else {
Serial.println("El Led 1 ya esta encendido");
}
break;
case '0':
if (digitalRead(Led) == HIGH) {
digitalWrite(Led, LOW);
Serial.println("Se apaga el Led 1");
}
else {
Serial.println("El Led ya esta apagado");
}
break;
}
}
}

---------------------------------------------------------------

//PRACTICA 11: "CONTROL DE ENCENDIDO Y PARPADEO DE LED´S UTILIZANDO EL TECLADO"

byte Led1 = 13, Led2 = 12;


int tecla;
void setup() {
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
Serial.begin(9600);

void loop() {
if (Serial.available()) {
tecla = Serial.read();
switch (tecla) {
case '0':
if (digitalRead(13) == 1) {
digitalWrite(Led1, LOW);
Serial.println("Se apagó el Led 1");
}
else if (digitalRead(Led2) == 1) {
digitalWrite(Led2, LOW);
Serial.println("Se apagó el Led 2");
}
break;
case '1':
if (digitalRead(Led2) == 1) {
digitalWrite(Led2, LOW);
}
if (digitalRead(Led1) == 0) {
digitalWrite(Led1, HIGH);
}
break;
case '2':
if (digitalRead(Led1) == 1) {
digitalWrite(Led1, LOW);
}
if (digitalRead(Led2) == 0) {
digitalWrite(Led2, HIGH);
}
break;
case '3':
if (digitalRead(Led2) == 1); {
digitalWrite(Led2, LOW);
}
for (byte x = 1; x <= 10; x++) {
digitalWrite(Led1, HIGH);
delay(250);
digitalWrite(Led1, LOW);
delay(250);
}
break;
case '4':
if (digitalRead(Led1) == 1); {
digitalWrite(Led1, LOW);
}
for (byte x = 1; x <= 10; x++) {
digitalWrite(Led2, HIGH);
delay(250);
digitalWrite(Led2, LOW);
delay(250);
}
break;
case '5':
if (digitalRead(Led2) == 1) {
digitalWrite(Led2, LOW);
}
byte i = 0;
while (tecla != '7') {
digitalWrite(Led1, HIGH);
delay(250);
digitalWrite(Led1, LOW);
delay(250);
tecla = Serial.read();
i++;
}
break;
}
}
}

----------------------------------------------------------------

/PRACTICA 12: "CONTROL DE UN LED UTILIZANDO UN BOTÓN"

const byte Boton=2;


const byte Led=13;
byte EdoBoton=0;
void setup() {
pinMode (Led, OUTPUT);
pinMode (Boton, INPUT);
Serial.begin(9600);
}
void loop() {
EdoBoton=digitalRead(Boton);
if (EdoBoton ==1){
digitalWrite(Led, HIGH);
Serial.println("Está presionado el botón");
}
else{
digitalWrite(Led,LOW);
Serial.println("El botón está liberado");
}
}
---------------------------------------------------------------

//PRACTICA 13: "CAMBIO DE ESTADO DE UN LED UTILIZANDO UN BOTÓN"

byte Boton=2;
byte Led=13;

byte EdoBoton;
byte EdoPrev;
byte EdoLed;

void setup() {
pinMode (Led, OUTPUT);
pinMode (Boton, INPUT);
Serial.begin(9600);
}
void loop() {
EdoBoton=digitalRead(Boton);
EdoLed=digitalRead(Led);
if (EdoBoton ==HIGH && EdoPrev ==LOW){
if (EdoLed==LOW){
digitalWrite(Led, HIGH);
Serial.println("Se enciende el Led");
}
else{
digitalWrite(Led,LOW);
Serial.println("Se apaga el Led");
}
}
EdoPrev=EdoBoton;
}

---------------------------------------------------------------

//PRACTICA 14: "ENCENDIDO GRADUAL DE UN LED"

const byte Led = 11;

void setup() {
pinMode(Led, OUTPUT);

void loop() {
analogWrite(Led, 0);
delay(500);
analogWrite(Led, 51);
delay(500);
analogWrite(Led, 102);
delay(500);
analogWrite(Led, 153);
delay(500);
analogWrite(Led, 204);
delay(500);
analogWrite(Led, 255);
delay(500);

}
-----------------------------------------------------------------

//PRACTICA 15: "ENCENDIDO Y APAGADO GRADUAL DE UN LED UTILIZANDO LA ESTRUCTURA FOR"

int Led = 11;

void setup() {
Serial.begin(9600);
}

void loop() {
for(int x=0;x<255; x+=5){
analogWrite(Led, x);
Serial.println("Intensidad:" + String(x));
delay(150);
}
for(int x=255;x>=0; x-=5){
analogWrite(Led, x);
Serial.println("Intensidad:" + String(x));
delay(150);

}
}
-------------------------------------------------------

//PRACTICA 16: "CONTROL GRADUAL DE 3 LED´S UTILIZANDO LA ESTRUCTURA FOR"

const byte Led[] = {11, 12, 13};

void setup() {
Serial.begin(9600);
}

void loop() {
for(byte y = 0; y < 3; y++){
for(int x = 0; x < 255; x+=5){
analogWrite(Led[y], x);
delay(150);
Serial.println("intencidad: " + String(y+1) + ":""Led: " + String(x));
}

for(int x=255;x>=0; x-=5){


analogWrite(Led[y], x);
delay(150);
Serial.println("intencidad: " + String(y+1) + ":""Led: " + String(x));
}
}
}
----------------------------------------------------------------------------------
//PRACTICA 17: "ENCENDIDO GRADUAL DE UN LED UTILIZANDO EL POTENCIOMETRO"

const byte Led = 3;


const byte Pot = A0;
int ValorPot = 0;
byte IntLed = 0;

void setup() {
Serial.begin (9600);
}

void loop() {
ValorPot = analogRead (Pot);
IntLed = map (ValorPot, 0, 1023, 0, 255);
analogWrite (Led, IntLed);
Serial.println ("Valor potenciometro: " + String (ValorPot));
Serial.println ("Intensidad Led : " + String (IntLed));
}

----------------------------------------------------------------------------

//PRACTICA 18: "CONTRO DIGITAL DE 5 LED´S UTILIZANDO UN POTENCIOMETRO" 20/10/2022

const byte Led[]={3,4,5,6,7};


const byte Pot= A0;
int ValorPot=0;

void setup() {
for (byte x=0; x<5;x++){
pinMode(Led[x],OUTPUT);
}
Serial.begin(9600);
}

void loop() {
ValorPot= analogRead(Pot);
if (ValorPot==0) {
digitalWrite(Led[0], LOW);
}
else if (ValorPot < 205) {
digitalWrite (Led[1],LOW);
digitalWrite (Led[0], HIGH);
}
else if (ValorPot < 409) {
digitalWrite (Led[0],LOW);
digitalWrite (Led[2], LOW);
digitalWrite (Led[1], HIGH);
}
else if (ValorPot < 614) {
digitalWrite (Led[3],LOW);
digitalWrite (Led[1], LOW);
digitalWrite (Led[2], HIGH);
}
else if (ValorPot < 818) {
digitalWrite (Led[4],LOW);
digitalWrite (Led[2], LOW);
digitalWrite (Led[3], HIGH);
}
else {
digitalWrite (Led[3], LOW);
digitalWrite (Led[4], HIGH);
}
}
--------------------------------------------------------------

//PRACTICA 19: "CONTROL ANALÓGICO DE 5 LED´S UTILIZANDO UN POTENCIOMETRO"


24/10/2022

const byte Led[]={3,4,5,6,7};


const byte Pot= A0;
int ValorPot=0;
byte IntLed=0;

void setup() {

}
void loop() {
ValorPot= analogRead(Pot);
if (ValorPot==0) {
digitalWrite(Led[0], 0);
}
else if (ValorPot < 205) {
IntLed = map(ValorPot, 1, 204, 0, 255);
analogWrite(Led[1], 0);
analogWrite(Led[0], IntLed);
}
else if (ValorPot < 410) {
IntLed = map(ValorPot, 205, 409, 0, 255);
analogWrite(Led[0], 0);
analogWrite(Led[2], 0);
analogWrite(Led[1], IntLed);
}
else if (ValorPot < 614) {
IntLed = map(ValorPot, 410, 613, 0, 255);
analogWrite(Led[1], 0);
analogWrite(Led[3], 0);
analogWrite(Led[2], IntLed);
}
else if (ValorPot < 819) {
IntLed = map(ValorPot, 614, 818, 0, 255);
analogWrite(Led[2], 0);
analogWrite(Led[4], 0);
analogWrite(Led[3], IntLed);
}
else if (ValorPot <= 1023) {
IntLed = map(ValorPot, 819, 1023, 0, 255);
analogWrite(Led[3], 0);
analogWrite(Led[4], IntLed);
}
}
-------------------------------------------------------------------------

//PRACTICA 20: "DETERMINAR LA DISTANCIA DE UN OBJETO CON UN SENSOR ULTRASÓNICO"


24/10/2022

byte Pindisparo = 6;
byte Pineco = 5;
long tiempoMs;
int dist;

void setup() {
Serial.begin(9600);
pinMode (Pindisparo, OUTPUT);
pinMode (Pineco, INPUT);
}

void loop() {
iniciarDisparo();
tiempoMs = pulseIn (Pineco, HIGH);
dist = tiempoMs*0.01715;
if (dist > 10 && dist <= 300){
Serial.println ("Distancia: " + String (dist) + "cm");
}
else{
Serial.println ("FUERA DE RANGO");
}
delay(500);
}

void iniciarDisparo(){
digitalWrite(Pindisparo, LOW);
delayMicroseconds(2);
digitalWrite(Pindisparo, HIGH);
delayMicroseconds(10);
digitalWrite(Pindisparo, LOW);
}
------------------------------------------------------------------------
/PRACTICA 21: "SISTEMA DE ALARMA CON UN SENSOR ULTRASÓNICO"

byte Pineco = 5;
byte Pindisparo = 6;
byte LedR = 13, LedA =12;
int dist;
long tiempoMs;
const int retardo = 500;

void setup() {
pinMode (Pindisparo, OUTPUT);
pinMode (Pineco, INPUT);
pinMode (LedR, OUTPUT);
pinMode (LedA, OUTPUT);
Serial.begin (9600);
}

void loop() {
inicialDisparo ();
tiempoMs = pulseIn (Pineco, HIGH);
dist = tiempoMs * 0.01715;
if (dist <= 75){
if (digitalRead (LedR) == LOW){
digitalWrite (LedR, HIGH);
}
if(digitalRead (LedA) == LOW){
digitalWrite (LedA, HIGH);
}
Serial.println ("El objeto está muy cerca, a: " + String (dist) + "cm");
}
else if (dist > 25 && dist >=250){
if(digitalRead (LedR) == HIGH){
digitalWrite (LedR, LOW);
}
if(digitalRead (LedA) == LOW){
digitalWrite (LedA, HIGH);
}
Serial.println ("El objeto está a: " + String (dist) + "cm");
}
else{
if(digitalRead (LedR) == HIGH){
digitalWrite (LedR, LOW);
}
if(digitalRead (LedA) == HIGH){
digitalWrite (LedA, LOW);
}
Serial.println ("El objeto está lejos");
}
delay;
}

void inicialDisparo(){
digitalWrite (Pindisparo, LOW);
delayMicroseconds (2);
digitalWrite (Pindisparo, HIGH);
delayMicroseconds (10);
digitalWrite (Pindisparo, LOW);
}
------------------------------------------------------------------

//PRACTICA 22: "DETECTAR EL MOVIMIENTO CON SENSOR PIR"

const byte PIR=2;


byte Detecta;

void setup() {
pinMode (PIR, INPUT);
Serial.begin(9600);
}

void loop() {
Detecta=digitalRead(PIR);
if (Detecta==1){
Serial.println("Movimientodetectado");
digitalWrite(PIR,HIGH);
delay(250);
digitalWrite(PIR,LOW);
}
delay(500);
}
---------------------------------------------------------------
//PRACTICA 23: "SISTEMA DE ALARMA UTILIZANDO EL SENSOR PIR"

const byte PIR=2, Led=13;


byte Detecta;

void setup() {
pinMode (PIR, INPUT);
pinMode(Led,OUTPUT);
Serial.begin(9600);
}

void loop() {
Detecta=digitalRead(PIR);
if (Detecta==1){
Serial.println("Movimientodetectado");
digitalWrite(Led,HIGH);
delay(250);
digitalWrite(Led,LOW);
}
delay(500);
}
----------------------------------------------------------------------

//PRACTICA 24: "MEDIR LA TEMPERATURA CON EL SENSOR DS18B20"

#include <OneWire.h>
#include <DallasTemperature.h>
#define Pin 2
OneWire ourWire(Pin);
DallasTemperature sensors(&ourWire);

void setup() {
delay(1000);
Serial.begin(9600);
sensors.begin();
}

void loop() {
sensors.requestTemperatures();
Serial.println("Temperatura leida:");
Serial.print(sensors.getTempCByIndex(0));
Serial.println("grados Centigrados");
Serial.print(sensors.getTempCByIndex(0));
Serial.println("grados Fahrenheit");
delay(1000);
}
------------------------------------------------------------------------

//PRACTICA 25: "SISTEMA DE ALARMA CON UN SENSOR DE TEMPERATURA"

#include <OneWire.h>
#include <DallasTemperature.h>
#define Pin 2
OneWire ourWire(Pin);
DallasTemperature sensors (&ourWire);
byte Led1 = 13, Led2 = 12;
int TempC;

void setup() {
delay(1000);
pinMode (Led1, OUTPUT);
pinMode (Led2, OUTPUT);
Serial.begin(9600);
sensors.begin();
}

void loop() {
sensors.requestTemperatures();
TempC = sensors.getTempCByIndex(0);
Serial.print("Temperatura media:");
Serial.println(String(TempC) + "grados centigrados");
if (TempC >= 32){
if (digitalRead(Led1) == LOW){
digitalWrite (Led1, HIGH);
Serial.println("Se enciende el Led 2");
}
}
else if (TempC >= 27){
if (digitalRead(Led2) == HIGH){
digitalWrite (Led2, LOW);
Serial.println("Se apaga el Led 2");
}
if (digitalRead(Led1) == LOW){
digitalWrite (Led1, HIGH);
Serial.println("Se enciende el Led 1");
}
}
else if (TempC >= 24){
if (digitalRead(Led1) == HIGH){
digitalWrite (Led1, LOW);
Serial.println("Se apaga el Led 1");
}
if (digitalRead(Led2) == LOW){
digitalWrite (Led2, HIGH);
Serial.println("Se enciende el Led 2");
}
}
else {
if (digitalRead(Led1) == HIGH){
digitalWrite (Led1, LOW);
Serial.println("Se apaga el Led 1");
}
if (digitalRead(Led2) == HIGH){
digitalWrite (Led2, LOW);
Serial.println("Se apaga el Led 2");
}
}
delay(1000);
}

----------------------------------------------------------------------

/PRACTICA 26: "DETECTAR SONIDO CON UN SENSOR"

const byte Sensor = 2;


byte Sonido;

void setup() {
Serial.begin(9600);
pinMode(Sensor, INPUT);
Serial.println("Inicializando el sistema...");
delay(1000);
}

void loop() {
Sonido = digitalRead(Sensor);
if(Sonido==1){
Serial.println("Sonido detectado");
}
else{
Serial.println("No se detecta sonido");
}
delay(500);
}
-----------------------------------------------------------------------

//PRACTICA 27: "SISTEMA DE ALARMA CON UN SENSOR DE SONIDO"

const byte Sensor=2, LedR=13,LedV=11; //pines conectados


byte Sonido; //Variable que guarda el nivel del sonido detectado

void setup() {
Serial.begin(9600);
pinMode(Sensor, INPUT); //modo de trabajo del pin donde se conecta el sensor
pinMode(LedR, OUTPUT);
pinMode(LedV, OUTPUT);
Serial.println("Inicializando el sistema...");
delay(1000);
}

void loop() {
Sonido= digitalRead(Sensor); //Guardod el nivel de sonido detectado
Serial.println(Sonido);
if (Sonido==1){
if(digitalRead(LedV)==1){
digitalWrite(LedV,LOW);
}
if(digitalRead(LedR)==0){
digitalWrite(LedR,HIGH);
}
Serial.println("Nivel de sonido alto");
}
else if(Sonido==0){
if (digitalRead(LedR)==1){
digitalWrite(LedR,LOW);
}
if (digitalRead(LedV)==0){
digitalWrite(LedV,HIGH);
}
Serial.println("Nivel de sonido bajo");
}
delay(500);
}
--------------------------------------------------------------------------

//PRACTICA 28: "SISTEMA DE ALARMA CON SENSOR DE SONIDO Y SENSOR ULTRASÓNICO"

byte Sonido = 2;
byte Pineco = 5;
byte Pindisparo = 6;
byte LedV = 13, LedA = 12, LedR = 11;
int ValorS;
int dist;
long tiempoMs;
const int retardo = 500;

void setup() {
pinMode (Pindisparo, OUTPUT);
pinMode (Pineco, INPUT);
pinMode (LedV, OUTPUT);
pinMode (LedA, OUTPUT);
pinMode (LedR, OUTPUT);
pinMode (Sonido, INPUT);
Serial.begin (9600);
}

void loop() {
ValorS = digitalRead (Sonido);
Serial.println (ValorS);
if (ValorS == HIGH){
iniciarDisparo ();
tiempoMs = pulseIn(Pineco, HIGH);
dist = tiempoMs * 0.01715;
Serial.println("El objeto está a: " + String(dist) + "cm");
if (dist <= 90){
digitalWrite(LedV, LOW);
digitalWrite(LedA, LOW);
digitalWrite(LedR, HIGH);
}
else if (dist > 90 && dist < 170){
digitalWrite(LedV, LOW);
digitalWrite(LedR, LOW);
digitalWrite(LedA, HIGH);
}
else if (dist > 170 && dist < 300){
digitalWrite(LedA, LOW);
digitalWrite(LedR, LOW);
digitalWrite(LedV, HIGH);
}
else {
digitalWrite(LedV, LOW);
digitalWrite(LedA, LOW);
digitalWrite(LedR, LOW);
}
}
delay (retardo);
}

void iniciarDisparo(){
digitalWrite (Pindisparo, LOW);
delayMicroseconds(2);
digitalWrite(Pindisparo, HIGH);
delayMicroseconds(10);
digitalWrite(Pindisparo, LOW);
}
----------------------------------------------------------------------------

You might also like