Unidad 6
Programa 1
15/11/2022 "blink"
/*
Blink
Turns an LED on for one second, then off for one second, repeatedly.
Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
the correct LED pin independent of which board is used.
If you want to know what pin the on-board LED is connected to on your Arduino
model, check the Technical Specs of your board at:
https://www.arduino.cc/en/Main/Products
modified 8 May 2014
by Scott Fitzgerald
modified 2 Sep 2016
by Arturo Guadalupi
modified 8 Sep 2016
by Colby Newman
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/
// the setup function runs once when you press reset or power the board
int led=13;
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(13, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(10000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
}
Programa 2
16/11/2022 "hola1"
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.print("Hola");
}
void loop() {
// put your main code here, to run repeatedly:
}
Programa 3
16/11/2022 "hola2"
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("Hola loop");
}
Programa 4
16/11/2022 "Variable1"
//global
int a=42;
char c='m';
float raiz;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
raiz=sqrt(2.0);
Serial.println(a);
Serial.println(c);
Serial.println(raiz);
}
void loop() {
// put your main code here, to run repeatedly:
}
Programa 5
16/11/2022 "Variable2"
//global
int a=42;
char c='m';
float raiz;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
raiz=sqrt(2.0);
Serial.println(a);
Serial.println(c);
Serial.println(raiz);
}
void loop() {
// put your main code here, to run repeatedly:
}
Programa 6
16/11/2022 "Variable3"
int a=42;
char c='m';
float raiz;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
raiz=sqrt(2.0);
Serial.println(a);
Serial.println(c);
Serial.println(raiz);
}
Programa 7
16/11/2022 "Variable4"
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
float r=0.75;
float c=2.0*PI*r;
Serial.print("CIRCUNFERENCIA= ");
Serial.println(c);
}
void loop() {
// put your main code here, to run repeatedly:
Programa 8
16/11/2022 "Variable5"
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
float r=0.75;
float a=PI*r*r;
Serial.print("AREA= ");
Serial.println(a);
}
void loop() {
// put your main code here, to run repeatedly:
}
Programa 9
16/11/2022 "Constante 1"
const int VAR1=1;
const int VAR2=10;
const int BAU=9600;
//CALVO, PELO LARGO, SOMBRERITO CALVO
void setup() {
// put your setup code here, to run once:
Serial.begin(BAU);
for(int i=VAR1;i<=VAR2;i++)
{
Serial.println(i);
delay(500);
}
Serial.print("TERMINO");
}
void loop() {
// put your main code here, to run repeatedly:
}
Programa 10
17/11/2022 "If1"
const int BAU=9600;
void setup() {
// put your setup code here, to run once:
Serial.begin(BAU);
int a=42;
int b=42;
if(a>b)
{
Serial.print ("A es mayor");
}
else if(a<b)
{
Serial.print("A no es mayor que B");
}
else
{
Serial.print ("A es igual a B");
}
}
void loop() {
// put your main code here, to run repeatedly:
}
Serial.println(raiz);
}
void loop() {
// put your main code here, to run repeatedly:
}
Programa 11
17/11/2022 "If2"
const int BAU=9600;
void setup() {
// put your setup code here, to run once:
Serial.begin(BAU);
int a=42;
int b=42;
if(a>b)
{
Serial.print ("A es mayor");
}
else if(a<b)
{
Serial.print("A no es mayor que B");
}
else
{
Serial.print ("A es igual a B");
}
}
void loop() {
// put your main code here, to run repeatedly:
}
Programa 12
17/11/2022 "If3"
const int BAU=9600;
void setup() {
// put your setup code here, to run once:
Serial.begin (BAU);
int a=89;
int b=42;
if ((a>50)&&(b<50)){
Serial.print("Rango normal");
}
else Serial.print("Rango anormal");
}
void loop() {
// put your main code here, to run repeatedly:
}
Programa 13
17/11/2022 "If4"
const int BAU=9600;
void setup() {
// put your setup code here, to run once:
Serial.begin(BAU);
int a=101;
int b=-2;
if ((a>100) || (b<0)){
Serial.print("Wow puede ser peligroso");
}
}
void loop() {
// put your main code here, to run repeatedly:
}
Programa 14
17/11/2022 "If5"
const int BAU=9600;
void setup() {
// put your setup code here, to run once:
Serial.begin(BAU);
int a=101;
int b=101;
if((a>100)||(b<0)){
Serial.print("WOW! puede ser peligroso");
}
else if (a==b){
Serial.print("Tal vez debas correr");
}
else
Serial.print("A salvo");
}
void loop() {
// put your main code here, to run repeatedly:
}
Programa 15
17/11/2022 "If6"
const int BAU=9600;
void setup() {
// put your setup code here, to run once:
Serial.begin(BAU);
int a=56;
int b=56;
if ((a>100) || (b<0)){
Serial.print("Wow puede ser peligroso");
}
else if (a==b)
{
Serial.print("Tal vez debes de correr");
}
else
Serial.print("A SALVO");
}
void loop() {
// put your main code here, to run repeatedly:
}
Programa 16
17/11/2022 "If7"
byte x=50;
const int BAU=9600;
void setup() {
// put your setup code here, to run once:
Serial.begin(BAU);
if (x>=10 && x<=20){
Serial.print("Frase 1 ");
}
if (x>=10 || x<=20){
Serial.print("Frase 2 ");
}
if (x>10 && !(x<=20)){
Serial.print("Frase 3 ");
}
}
void loop() {
// put your main code here, to run repeatedly:
}
Programa 17
18/11/2022 "if8 "
String cad1="Hola";
String cad2="Holus";
const int BAU=9600;
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
Programa 18
18/11/2022 "If9"
int numero;
const int BAU=9600;
void setup() {
// put your setup code here, to run once:
Serial.begin (BAU);
}
void loop() {
// put your main code here, to run repeatedly:
delay(500);
if (Serial.available()>0){
numero=Serial.parseInt ();
if (numero=69){
Serial.print("Le atinaste");
}
else if(numero<69){
Serial.print("Frio");
}
else (Serial.print("Caliente"));}
}
Programa 19
18/11/2022 "while 1"
byte x=1;
const int BAU=9600;
void setup() {
// put your setup code here, to run once:
Serial.begin(BAU);
}
void loop() {
// put your main code here, to run repeatedly:
while (x<=50){
Serial.println (x++);
x=x+1;
delay(250);
}
}
Programa 20
18/11/2022 "while 2"
byte x=1;
const int BAU=9600;
void setup() {
// put your setup code here, to run once:
Serial.begin(BAU);
}
void loop() {
// put your main code here, to run repeatedly:
while (true){
Serial.println (++x);
if (x==7){
Serial.print
}
Serial.print("tan tan");
break;
delay(250);
}
}
Programa 21
18/11/2022 "while 3"
byte x=1;const int BAU=9600;void setup() {// put your setup code here, to run once:Serial.begin(BAU);while(true){Serial.println(++x);if (x==7){Serial.print("TAN TAN");break;}delay(250);}}
void loop() {// put your main code here, to run repeatedly:/*while(true){Serial.println(++x);if (x==7){Serial.print("TAN TAN");break; //NO TIENE SENTIDO USAR BREAK EN UN LOOP}delay(250);}*/}
byte x=1;
const int BAU=9600;
void setup() {
// put your setup code here, to run once:
Serial.begin(BAU);
while(true)
{
Serial.println(++x);
if (x==7)
{
Serial.print("TAN TAN");
break;
}
delay(250);
}
}
void loop() {
// put your main code here, to run repeatedly:
/*while(true)
{
Serial.println(++x);
if (x==7)
{
Serial.print("TAN TAN");
break; //NO TIENE SENTIDO USAR BREAK EN UN LOOP
}
delay(250);
}*/
}
Programa 22
18/11/2022 "for 1"
byte x=1;
const int BAU=9600;
void setup() {
// put your setup code here, to run once:
Serial.begin(BAU);
for (int i=1; i<=10; i++){
Serial.println(i);
delay(250);
}
Serial.println("Tan tan del for");
}
void loop() {
// put your main code here, to run repeatedly:
}
Practicas con el Arduino
Programa 1
22/11/2022 "LED 1"
const int BAU=9600;
void setup() {
// put your setup code here, to run once:Serial.begin (BAU);pinMode (13,OUTPUT);pinMode (12,OUTPUT);pinMode (11,OUTPUT);}void loop() { // put your main code here, to run repeatedly:digitalWrite (13,HIGH);delay (500);digitalWrite (13,LOW);delay (250);digitalWrite (12,HIGH);delay (500);digitalWrite (12,LOW);delay (250);digitalWrite (11,HIGH);delay (500);digitalWrite (11,LOW);delay (250);}
Programa 2
28/11/2022 "LED 2"
int lr=13;
int lv=12;
int la=11;
int num;
void setup() {
// put your setup code here, to run once:
Serial.begin (9600);
pinMode (lr,OUTPUT);
pinMode (lv,OUTPUT);
pinMode (la,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available()>0){
Serial.println("1.Rojo");
Serial.println("2.Amarillo");
Serial.println("3.Azul");
Serial.println("4.Salir");//Aqui nunca sale
num=Serial.parseInt ();
Serial.print("Numero");
Serial.println(num);
if (num==0)
{
digitalWrite(lr,LOW);
digitalWrite(lv,LOW);
digitalWrite(la,LOW);
}
else
{ if(num==1)
{
digitalWrite(lr,HIGH);
digitalWrite(lv,LOW);
digitalWrite(la,LOW);
delay (250);
}
else
{ if(num==2)
{
digitalWrite(lr,LOW);
digitalWrite(lv,HIGH);
digitalWrite(la,LOW);
delay (250);
}
else
{ if(num==3)
{
digitalWrite(lr,LOW);
digitalWrite(lv,LOW);
digitalWrite(la,HIGH);
delay (250);
} }
}
}
}
}
Programa 3
28/11/2022 "LED RGB 1"
const int redPin=11;
const int greenPin=9;
const int bluePin=10;
void setup() {
// put your setup code here, to run once:
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(bluePin,OUTPUT);
Serial.begin(9600);
Serial.println("Dame colores en RGB (ej. 255 100 200)");
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()){
int red=Serial.parseInt();
int green=Serial.parseInt();
int blue=Serial.parseInt();
analogWrite(redPin,red);
analogWrite(greenPin,green);
analogWrite(bluePin,blue);
}
}
Programa 4
29/11/2022 "LED RGB 2"
const int redPin=11;
const int greenPin=9;
const int bluePin=10;
int num;
int sel;
void setup() {
// put your setup code here, to run once:
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(bluePin,OUTPUT);
Serial.begin(9600);
Serial.println("Escoge el color que te sea de tu preferencia: ");
Serial.println("1. Rojo ");
Serial.println("2. Morado");
Serial.println("3. Verde");
Serial.println("4. Azul ");
Serial.println("5. Rosa ");
Serial.println("6. Salir");
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()){
int red=Serial.parseInt();
int green=Serial.parseInt();
int blue=Serial.parseInt();
analogWrite(redPin,red);
analogWrite(greenPin,green);
analogWrite(bluePin,blue);
}
sel=Serial.parseInt();
if (sel==1){
analogWrite(redPin,220);
analogWrite(greenPin,000);
analogWrite(bluePin,000);
}
if (sel==2){
analogWrite(redPin,200);
analogWrite(greenPin,200);
analogWrite(bluePin,000);
}
if (sel==3){
analogWrite(redPin,153);
analogWrite(greenPin,051);
analogWrite(bluePin,255);
}
if (sel==4){
analogWrite(redPin,000);
analogWrite(greenPin,255);
analogWrite(bluePin,128);
}
if (sel==5){
analogWrite(redPin,255);
analogWrite(greenPin,032);
analogWrite(bluePin,032);
}
if (sel==6){
analogWrite(redPin,000);
analogWrite(greenPin,000);
analogWrite(bluePin,000);
}
} es el mismo circuito para ambos led rgb
Programa 5
29/11/2022 "Sensor de Aproximacion 1"
#define echoPin 7
#define triggerPin 8
#define ledPin 13
int delaymili = 500;
int maximumRangeCm = 200;
int minimumRangeCm = 0;
long duration, distanceCm;
void setup() {
Serial.begin(9600);
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
initTrigger();
duration = pulseIn(echoPin, HIGH);
distanceCm = microsecCm(duration);
if (distanceCm >= maximumRangeCm || distanceCm <= minimumRangeCm){
Serial.println("Out of range");
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
Serial.print("Distance in cm: ");
Serial.println(distanceCm);
delay(delaymili);
}
}
long microsecCm(long microsecond) {
return microsecond / 58;
}
void initTrigger() {
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
}
es este circuto solamente que sin los leds
Programa 6
29/11/2022 "Sensor de Aproximacion 2"
#define echoPin 7
#define triggerPin 8
#define ledPin 13
int delaymili = 500;
int maximumRangeCm = 200;
int minimumRangeCm = 0;
long duration, distanceCm;
const int lr=12;
const int lv=11;
const int la=10;
void setup() {
Serial.begin(9600);
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(lr,OUTPUT);
pinMode(lv,OUTPUT);
pinMode(la,OUTPUT);
}
void loop() {
initTrigger();
duration = pulseIn(echoPin, HIGH);
distanceCm = microsecCm(duration);
if (distanceCm >= maximumRangeCm || distanceCm <= minimumRangeCm){
Serial.println("Out of range");
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
Serial.print("Distance in cm: ");
Serial.println(distanceCm);
delay(delaymili);
}
if(distanceCm>=0 && distanceCm<=8){
Serial.print("!!PELIGRO!! ") ;
digitalWrite(lr,HIGH);
digitalWrite(lv,LOW);
digitalWrite(la,LOW);
}
else if(distanceCm>8 && distanceCm<=16){
Serial.print("Precaucion! ") ;
digitalWrite(lr,LOW);
digitalWrite(lv,LOW);
digitalWrite(la,HIGH);
}
else if(distanceCm>16 && distanceCm<200){
Serial.print("Zona segura ") ;
digitalWrite(lr,LOW);
digitalWrite(lv,HIGH);
digitalWrite(la,LOW);
}
}
long microsecCm(long microsecond) {
return microsecond / 58;
}
void initTrigger() {
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
}
Programa 7
01/12/2022 "Sensor de Humedad 1"
#include <SimpleDHT.h>
//VCC: 5V O 3V
//GND: GND
//DATA: 2
int pdatos=2;//Advertencia
SimpleDHT11 dht11;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("*==============+++==============*");
Serial.println("Ejemplo de temperatura y humedad ");
//lean datos
byte temperatura=0;
byte humedad=0;
byte data[40]={0};
if(dht11.read(pdatos, &temperatura, &humedad, data)){
Serial.print("Error de comunicacion");
}
Serial.println("Muestra");
Serial.print((int)temperatura); Serial.println("°C");
Serial.print((int)humedad); Serial.println("%");
delay(5000);
}
Programa 8
02/12/2022 "Sensor de Humedad 2"
#include <SimpleDHT.h>
//VCC: 5V O 3V
//GND: GND
//DATA: 2
int pdatos=2;//Advertencia
SimpleDHT11 dht11;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("==============+++==============");
Serial.println("Ejemplo de temperatura y humedad ");
//lean datos
byte temperatura=0;
byte humedad=0;
byte data[40]={0};
if(dht11.read(pdatos, &temperatura, &humedad, data)){
Serial.print("Error de comunicacion");
}
Serial.println("Muestra");
Serial.print((int)temperatura); Serial.println("°C");
Serial.print((int)humedad); Serial.println("%");
if (temperatura<-10 && temperatura>15){
Serial.println("Frio");
Serial.println(" () ");
Serial.println(" (_) ");
Serial.println(" (___) ");
Serial.println(" (____) ");
Serial.println(" (______) ");
Serial.println(" \/\/\/\/\/");
Serial.println(" \/\/\/\/");
Serial.println(" \/\/\/");
Serial.println(" \/\/");
Serial.println(" \/");
else{
if(temperatura<20 && temperatura>50){
Serial.println("Calor");
Serial.println(" O , U ");
Serial.println(" |_ ______ _|__");
Serial.println("|_|| |||__");
Serial.println("_||| ) ' ||_|");
Serial.println(" ||| ( ()\( |||_");
Serial.println("_||| ( ,|,(X)' ||_#");
Serial.println("|_|| /,)/|`\``\\\ |||_/\");
}
}
e igual que en los anteriores el circuito era el mismo simplemente cambiándole cosas al código
ACTIVIDADES DEL PDF
Actividad 1
"Mapa conceptual"
Tema: 6.1 Tipos de puertos de comunicación
6.2 Especificaciones de los puertos de comunicación
Fecha: 06/12/2022
Nombre: Edgar Omar Claderón Meza
Elabore un mapa conceptual solo con los terminos de los temas
Actividad 2
"Programa"
Tema: 6.3 Envió y recepción de datos a través de un puerto de comunicación
6.2 Especificaciones de los puertos de comunicación
Fecha: 06/12/2022
Nombre: Edgar Omar Calderón Meza
Elabore un programa que encienda el color de la luz dependiendo de los rango numéricos que del usuario 1-10 Azul, 11-20 verde, 21-30 rosa, 31-40 Amarillo, 41 en delante rojo
int la=13;
int lv=12;
int lb=10;
int ly=9;
int lr=8;
int num;
void setup() {
// put your setup code here, to run once:
Serial.begin (9600);
pinMode (la,OUTPUT);
pinMode (lv,OUTPUT);
pinMode (lb,OUTPUT);
pinMode (ly,OUTPUT);
pinMode (lr,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available()>0){
Serial.println("Ingresa un numero");
num=Serial.parseInt ();
Serial.println(num);
if (num>=1 && num<=10)
{
digitalWrite(la,HIGH);
digitalWrite(lv,LOW);
digitalWrite(lb,LOW);
digitalWrite(ly,LOW);
digitalWrite(lr,LOW);
}
else
{ if(num>=11 && num<=20)
{
digitalWrite(la,LOW);
digitalWrite(lv,HIGH);
digitalWrite(lb,LOW);
digitalWrite(ly,LOW);
digitalWrite(lr,LOW);
delay (250);
}
else
{ if(num>=21 && num<=30)
{
digitalWrite(la,LOW);
digitalWrite(lv,LOW);
digitalWrite(lb,HIGH);
digitalWrite(ly,LOW);
digitalWrite(lr,LOW);
delay (250);
}
else
{ if(num>=31 && num<=40)
{
digitalWrite(la,LOW);
digitalWrite(lv,LOW);
digitalWrite(lb,LOW);
digitalWrite(ly,HIGH);
digitalWrite(lr,LOW);
delay (250);
}
else{
if (num>=41) {
digitalWrite(la,LOW);
digitalWrite(lv,LOW);
digitalWrite(lb,LOW);
digitalWrite(ly,LOW);
digitalWrite(lr,HIGH);
}
}
}
}
}
}
}





Comentarios
Publicar un comentario