PROGRAMAS Y CLASES DE SISTEMAS MICRO CONTROLADOS
CON
ARDUINO
KEYPAD O MTECLADO 4X4
#include <Keypad.h>
const byte filas = 4 ; // tengo 4 filas
const byte columnas = 4; // tengo 4
columnas
char keys[filas][columnas]={
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte pinfila[filas]={9,8,7,6};
byte pincolumnas[columnas]={5,4,3,2} ;
Keypad keypad = Keypad(makeKeymap(keys), pinfila
,pincolumnas,filas,columnas);
void setup() {
Serial.begin(9600);
}
void loop() {
char tecla = keypad.getKey();
if(tecla!= NO_KEY){
Serial.println(tecla);
}
}
PARA ENCONTRAR UNA RAÍZ CUADRADA
/*#include <Math.h>
#include <stdio.h>
*/
int a;
int b;
int c;
float x1, x2;
void setup() {
Serial.begin(9600);
}
void loop() {
a=1;
b=-8;
c=16;
x1=( -b+sqrt (b*b
-4*a*c))/(2*a);
x2=( -b-sqrt(b*b -4*a*c))/
(2*a);
Serial.print("LA PRIMERA RAIZ ES
= ");
Serial.print(x1);
Serial.print("LA SEGUNDA RAIZ ES
= ");
Serial.print(x2);
delay (15000);
}
PARA ALLAR UNA O MAS FUNCIONES
/*#include <Math.h>
#include <stdio.h>
*/
float x1,x2;
void setup() {
Serial.begin(9600);
}
void loop() {
Raiz_Ecuacion(1,11,28);
Raiz_Ecuacion(2,-7,-15);
Raiz_Ecuacion(3,-7,-6);
delay(15000);
}
float Raiz_Ecuacion(float a, float b, float c){
x1=( -b+sqrt (b*b -4*a*c))/(2*a);
x2=( -b-sqrt(b*b -4*a*c))/
(2*a);
Serial.print("LA PRIMERA RAIZ ES
= ");
Serial.print(x1);
Serial.print("LA SEGUNDA RAIZ ES
= ");
Serial.print(x2);
delay (15000);
}
MULTÍMETRO PARA MEDIR
void setup() {
Serial.begin(9600);
}
void loop() {
int resol =analogRead (0);
float vol =
float(resol)*5.0/1023.0;
Serial.print("el
voltaje es ");
Serial.println(vol,3);
delay(1900);
}
COMO CALCULAR UN TRIANGULO
int a,b,c;
void setup() {
Serial.begin(9600);
}
void loop() {
area_triangulo(20,15,18);
area_triangulo(14,6,13);
area_triangulo(6,4,5);
delay(15000)
}
float area_del_triangulo(float a, float b, float c){
float p=(a+b+c)/(2);
float a2= (sqrt p*(p-a)*(p-b)*(p-c)));
Serial.print("EL PERIMETRO DEL
TRIANGULO ES = ");
Serial.println(p);
Serial.print("EL AREA DEL
TRIANGULO ES = ");
Serial.println(a2);
}
PANTALLA LCD CONFIGURACION CON UN
VALOR PRENDE Y APAGA
#include <LiquidCrystal.h>
/*
* RS => pin12
* E => pin11
* D4 => pin5
* D5 => pin4
* D6 => pin3
* D7 => pin2
*/
LiquidCrystal lcd(12,11,5,4,3,2);
void setup() {
lcd.begin(2,2);
lcd.clear();
}
void loop() {
lcd.setCursor(0,2);
lcd.print("JHANCARLOS");
delay(1000);
}
PANTALLA LCD QUE RECORRA UNA
PALABRA
#include <LiquidCrystal.h>
/*
* RS => pin12
* E => pin11
* D4 => pin5
* D5 => pin4
* D6 => pin3
* D7 => pin2
*/
LiquidCrystal lcd(12,11,5,4,3,2);
void setup() {
lcd.begin(16,2);
lcd.clear();
}
void loop() {
int P =0;
for(P=0; P<16; P++){
lcd.setCursor(P,0);
lcd.print("JHANCARLOS");
delay(1000);
lcd.clear();
}
}
LCD EL MISMO VALOR PERO SE
INACIALIZA EL MISMO VALOR
#include <LiquidCrystal.h>
/*
*RS= pin 13
*E = pin 12
*D4 = pin 5
*D5 = pin 4
*D6 = pin 3
*D7 = pin 2
*/
LiquidCrystal lcd(13,12,5,4,3,2);
void setup() {
lcd.begin(16,2);
lcd.clear();
}
void loop() {
int k;
for(k =0; k<=4; k++){
lcd.setCursor(k,0);
lcd.print("JHANCARLOS ");
delay(1000);
lcd.clear();
}
int p;
for(p =4; p>=0; p--){
lcd.setCursor(p,0);
lcd.print("JHANCARLOS ");
delay(1000);
}
int A;
for(A =0; A<=4; A++){
lcd.setCursor(A,1);
lcd.print("JHANCARLOS ");
delay(1000);
lcd.clear();
}
int B;
for(B =4; B>=0; B--){
lcd.setCursor(B,1);
lcd.print("JHANCARLOS ");
delay(1000);
}
}
LCD MIS DATOS PERSONALES EN
RRECORRIDO
#include <LiquidCrystal.h>
/*
* RS => pin12
* E => pin11
* D4 => pin5
* D5 => pin4
* D6 => pin3
* D7 => pin2
*/
LiquidCrystal lcd(12,11,5,4,3,2);
void setup() {
lcd.begin(16,2);
}
void loop() {
int B = 0;
for(B=0; B<16; B++){
lcd.setCursor(B,0);
lcd.print(" HOLA MUNDO");
delay(1000);
lcd.clear();
}
int R = 0;
for(R=0; R<16; R++){
lcd.setCursor(R,0);
lcd.print(" MI NOMBRE ES");
delay(1000);
lcd.clear();
}
int P =0;
for(P=0; P<16; P++){
lcd.setCursor(P,0);
lcd.print("
JHANCARLOS");
delay(1000);
lcd.clear();
}
int E
= 0;
for(E=0; E<16; E++){
lcd.setCursor(E,0);
lcd.print("RAMOS COTRINA");
delay(1000);
lcd.clear();
}
int D = 0;
for(D=0; D<16; D++){
lcd.setCursor(D,0);
lcd.print("BECARIO EN BECA 18");
delay(1000);
lcd.clear();
}
int F = 0;
for(F=0; F<16; F++){
lcd.setCursor(F,0);
lcd.print("ESTUDIANTE
DE IDAT");
delay(1000);
lcd.clear();
}
int G= 0;
for(G=0; G<16; G++){
lcd.setCursor(G,0);
lcd.print("ELECTRONICA INDUSTRIAL");
delay(1000);
lcd.clear();
}
int H= 0;
for(H=0; H<16; H++){
lcd.setCursor(H,0);
lcd.print("DIRECCION E IDENTIDAD");
delay(1000);
lcd.clear();
}
int I = 0;
for(I=0; I<16; I++){
lcd.setCursor(I,0);
lcd.print("CAJAMARCA-PERU");
delay(1000);
lcd.clear();
}
int J = 0;
for(J=0; J<16; J++){
lcd.setCursor(J,0);
lcd.print("CEL: 9699152572");
delay(1000);
lcd.clear();
}
}
LCD PERO INICIALIZANDO LOS DOS
REGLONES QUE RRECORE
#include <LiquidCrystal.h>
/*
* RS => pin12
* E => pin11
* D4 => pin5
* D5 => pin4
* D6 => pin3
* D7 => pin2
*/
LiquidCrystal lcd(12,11,5,4,3,2);
void setup() {
lcd.begin(16,2);
lcd.clear();
}
void loop() {
int P =0;
for(P=16; P>0; P--){
lcd.setCursor(P,0);
lcd.print(" JHANCARLOS");
lcd.setCursor(P,1);
lcd.print("RAMOS COTRINA");
delay(1000);
lcd.clear();
}
int D =0;
for(D=0; D<16; D++){
lcd.setCursor(D,0);
lcd.print(" JHANCARLOS");
lcd.setCursor(D,1);
lcd.print("RAMOS COTRINA");
delay(1000);
lcd.clear();
}
}
INICIAL IZANDO LOS DOS LINEAS DEL LCD
Y NO RECORRE
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
void setup() {
Serial.begin(9600);
lcd.begin(16,2);
}
void loop() {
lcd.setCursor(0,0);
lcd.print(" JHANCARLOS");
lcd.setCursor(0,1);
lcd.print(" RAMOS COTRINA");
}
PANTALLA LCD QUE CAMBIA SOLA Y BIBRA
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
void setup() {
Serial.begin(9600);
lcd.begin(16,2);
}
void loop() {
lcd.print(" LCD TUTORIAL");
delay(3000);
lcd.setCursor(0,1);
lcd.print(" RAMOS COTRINA");
delay(4000);
lcd.clear();
delay(1500);
lcd.blink ();
delay(3000);
lcd.noBlink();
lcd.print(" VISITA MI CANAL");
delay(3000);
lcd.clear();
lcd.print("WWW.YOUTUBE.COM");
lcd.setCursor(0,1);
lcd.print("/USER/TECNOLOGIA");
delay(5000);
lcd.clear();
lcd.print(" GRACIAS");
lcd.setCursor(0,1);
lcd.print(" MIS AMIGOS");
delay
(5000);
lcd.clear();
delay(2000);
}
Teclado
arduino mas lcd
#include
<LiquidCrystal.h> //incluimos la libreria de la LCD
#include
<Keypad.h> //Incluimos la librería
del teclado
const
byte filas = 4; //poque nuestro teclado tiene 4 filas
const
byte columnas = 4;//poque nuestro teclado tiene 4 columnas
char
teclado[filas][columnas]={ //creamos la
matriz teclado tal como es nuestro teclado
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'},
};
byte
pinFilas[filas]={7,6,5,4}; //en estos pines de nuestro arduino irán conectados
las filas de nuestro teclado
byte
pinColumnas[columnas] = {3,2,A5,A4};
//en estos pines de nuestro arduino irán conectados las columnas de
nuestro teclado
Keypad
keypad=Keypad(makeKeymap(teclado),pinFilas,pinColumnas,filas,columnas); // creamos el objeto keypad
LiquidCrystal
lcd(13,12,11,10,9,8); //Creamos el objeto lcd y en parentecisis van los pines
de nuestro arduino
int
a, b, suma; //Creamos la variable a y b
donde se almacenarán las teclas pulsadas y la variable suma = a+b
int
var=0; //Creamos var para eleguir los
casos y almacenar los valores en a o b
void
setup() {
lcd.begin(16,2);//Inicializamos nuestra LCD de
16x02
lcd.clear();
//Limpiamos nuestra LCD
lcd.setCursor(0,0); //Ubicamos el cursor en la
columna cero y fila cero
}
void
loop() {
char tecla = keypad.getKey(); //Obtenemos la tecla pulsada y lo asignamos
"tecla"
if(tecla != NO_KEY){
lcd.print(tecla); //Imprimimos la tecla
pulsada
var++; //var sumamos en uno cada vez q se
pulsa una tecla
tecla = tecla - 48; //pasamos de char a int
switch(var){ //iniciamos el switch con
variables var
case 1:
//caso cuando var =1
a = tecla; //almacenamos el valor de
tecla en la variable a
break;
case 2:
b = tecla;//almacenamos el valor de
tecla en la variable b
break;
}
if(tecla == -13){ //Entramos al if cuando
se pulsa la tecla "#"
suma = a+b; //asignamos la la suma de a y b a la variable
"suma"
lcd.print(suma); //imprimimos el valor de
suma
var = 0; //reinicio var
}
}
}
Para configurar arduino y comunicarse con
modulo bluetooth
/*************************
http://geekfactory.mx *******************************
*
* Sketch de prueba para modulos de bluetooth
HC-05 y HC-06. Este programa puede
* controlar el led de la tarjeta arduino a
través de la conexión bluetooth. El
* programa puede ampliarse para controlar una
mayor cantiad de salidas.
*
* ESTE SKETCH USA EL PUERTO SERIE (UART),
MISMO QUE ES UTILIZADO PARA
* LA COMUNICACIÓN USB. EL USO DE BLUETOOTH O
USB ES MUTUAMENTE EXCLUSIVO
* (NO SE PUEDEN USAR AMBOS AL MISMO TIEMPO).
*
* PARA USAR LA CONEXIÓN USB Y BLUETOOTH AL
MISMO TIEMPO ES NECESARIO EMULAR UNA
* UART POR SOFTWARE.
*
* El funcionamiento es sencillo:
*
* Enviar un caracter 'A' para encender el led
* Enviar un caracter 'a' para apagar el led
*
*/
char rxChar; // Variable para recibir datos del puerto serie
int ledpin = 13; // Pin donde se encuentra conectado el led (pin 13)
// Configurar el arduino
void setup()
{
//
Pin 13 como salida
pinMode(ledpin,
OUTPUT);
//
Comunicación serie a 9600 baudios
Serial.begin(9600);
}
// Ciclo infinito, programa principal
void loop()
{
//
Si hay datos disponibles en el buffer
if(
Serial.available() )
{
//
Leer un byte y colocarlo en variable
rxChar
= Serial.read();
//
Procesar comando de un solo byte
if( rxChar == 'A' )
{
digitalWrite(ledpin,
HIGH);
Serial.println("ON");
}
else
if ( rxChar == 'a' )
{
digitalWrite(ledpin,
LOW);
Serial.println("OFF");
}
}
//
Podemos hacer otras cosas aquí
delay(100);
}
Programa para sensor de
ultrasonido con arduino
#include <NewPing.h>
/*Aqui se configuran los pines donde
debemos conectar el sensor*/
#define TRIGGER_PIN 12
#define
ECHO_PIN 11
#define MAX_DISTANCE 200
/*Crear el objeto de la clase NewPing*/
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup() {
Serial.begin(9600);
}
void loop() {
//
Esperar 1 segundo entre mediciones
delay(1000);
// Obtener medicion de
tiempo de viaje del sonido y guardar en variable uS
int uS = sonar.ping_median();
//
Imprimir la distancia medida a la consola serial
Serial.print("Distancia:
");
// Calcular la distancia
con base en una constante
Serial.print(uS / US_ROUNDTRIP_CM);
Serial.println("cm");
}
Arduino con Fotoresistencia LDR
/*
* http://www.geekfactory.mx
*
* Ejemplo de medidor de luz con Arduino
utilizando una fotoresistencia LDR y
* un grupo de leds para mostrar el resultado
de las lecturas. Este sketch puede
* servir como base para otros proyectos que
requieren medicion de la intensidad
* de luz con una fotoresistencia.
*
* Mas detalles y la conexion completa para
este sketch en:
*
http://www.geekfactory.mx/tutoriales/tutoriales-arduino/tutorial-arduino-con-fotoresistencia-ldr/
*
*/
// Pin donde se conectan los leds
int pinLed1 = 2;
int pinLed2 = 3;
int pinLed3 = 4;
// Pin analogico de entrada para el LDR
int pinLDR = 0;
// Variable donde se almacena el valor
del LDR
int valorLDR = 0;
void setup()
{
// Configuramos como
salidas los pines donde se conectan los led
pinMode(pinLed1, OUTPUT);
pinMode(pinLed2, OUTPUT);
pinMode(pinLed3,
OUTPUT);
// Configurar el
puerto serial
Serial.begin(9600);
}
void loop()
{
// Apagar todos los leds
siempre que se inicia el ciclo
digitalWrite(pinLed1, LOW);
digitalWrite(pinLed2, LOW);
digitalWrite(pinLed3,
LOW);
// Guardamos el valor leido
del ADC en una variable
// El valor leido por el
ADC (voltaje) aumenta de manera directamente proporcional
// con respecto a la luz
percibida por el LDR
valorLDR=
analogRead(pinLDR);
// Devolver el valor leido
a nuestro monitor serial en el IDE de Arduino
Serial.println(valorLDR);
// Encender los leds
apropiados de acuerdo al valor de ADC
if(valorLDR > 256)
{
digitalWrite(pinLed1,
HIGH);
}
if(valorLDR > 512)
{
digitalWrite(pinLed2,
HIGH);
}
if(valorLDR > 768)
{
digitalWrite(pinLed3,
HIGH);
}
// Esperar unos
milisegundos antes de actualizar
delay(200);
}
Controlar tu arduino desde la PC mediante
comandos de texto
/**
* GeekFactory - "Construye tu propia
tecnologia"
* Distribucion de materiales para el
desarrollo e innovacion tecnologica
* www.geekfactory.mx
*
* Ejemplo de libreria Shell. Este ejemplo
permite controlar el color mostrado
* en un led RGB conectado a los pines PWM
mediante comandos de texto. El programa
* admite 3 parametros de 0 a 255 que
representan la intensidad de cada led (RGB).
*
* Example
for Shell library. This example controls the color displayed on the RGB
* led
connected to PWM pins using text commands. The program accepts 3 parameters
* in a range
from 0 to 255 that represent the intensity of each led.
*/
#include <Shell.h>
void setup()
{
// Prepare serial communication
Serial.begin(9600);
// Wait after reset or power on...
delay(1000);
// Pins to connect RGB led (PWM
outputs)
pinMode(3,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
// Initialize command line interface
(CLI)
// We pass the function pointers to the
read and write functions that we implement below
// We can also pass a char pointer to
display a custom start message
shell_init(shell_reader, shell_writer,
0);
// Add commands to the shell
shell_register(command_rgbled,
"rgbled");
}
void loop()
{
// This should always be called to
process user input
shell_task();
}
/**
* This
function implements the rgbled command and parses the required args
* Type:
rgbled 255 255 255 to set led to maximum brigtness
* Type:
rgbled 0 0 0 to set led to minimum brightess
*/
int command_rgbled(int argc, char** argv)
{
// Variables that store parameter
values
uint8_t val_R, val_G, val_B;
// Parse RGB values and write to PWM
pins
val_R = strtol(argv[1],NULL,0);
analogWrite(3, val_R);
val_G = strtol(argv[2],NULL,0);
analogWrite(5, val_G);
val_B = strtol(argv[3],NULL,0);
analogWrite(6, val_B);
// Print the received falues to the
terminal screen
shell_printf("#RGB-COMPONENTS:%d,%d,%d\r\n",
val_R, val_G, val_B);
// Return success code
return SHELL_RET_SUCCESS;
}
/**
* Function
to read data from serial port
* Functions
to read from physical media should use this prototype:
* int
my_reader_function(char * data)
*/
int shell_reader(char * data)
{
// Wrapper for Serial.read() method
if (Serial.available()) {
*data = Serial.read();
return 1;
}
return 0;
}
/**
* Function
to write data to serial port
* Functions
to write to physical media should use this prototype:
* void
my_writer_function(char data)
*/
void shell_writer(char data)
{
// Wrapper for Serial.write() method
Serial.write(data);
}
Arduino mas un potenciómetro
const int analogPin = A0;
int
value; //variable que almacena la lectura
analógica raw
int position; //posicion
del potenciometro en tanto por ciento
void setup() {
}
void loop() {
value = analogRead(analogPin); //
realizar la lectura analógica raw
position = map(value, 0, 1023, 0,
100); // convertir a porcentaje
//...hacer lo que se quiera, con el
valor de posición medido
delay(1000);
}
Clave con contraseña
#include <LiquidCrystal.h>
#include <Key.h>
#include <Keypad.h>
const byte filas = 4;
const byte columnas = 4;
char teclado[filas][columnas]={
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'},
};
byte pinFilas[filas]={7,6,5,4};
byte pinColumnas[columnas] =
{3,2,A5,A4};
Keypad
keypad=Keypad(makeKeymap(teclado),pinFilas,pinColumnas,filas,columnas);
LiquidCrystal lcd(13,12,11,10,9,8);
int tecla;
int a,b,c,d, suma;
int c1=4,c2=6,c3=7,c4=3;
int var=0;
void setup() {
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
}
void loop() {
char tecla = keypad.getKey();
if(tecla != NO_KEY){
lcd.print(tecla);
var++;
tecla = tecla - 48;
switch(var){
case 1:
a = tecla;
break;
case 2:
b = tecla;
break;
case 3:
c = tecla;
break;
case 4:
d = tecla;
if
(a==c1&&b==c2&&c==c3&&d==c4){
lcd.setCursor(0,0);
lcd.print("clave coerrecta");
delay (5000);
lcd.clear();
}
else{
lcd.setCursor(0,0);
lcd.print("clave incorrecta");
lcd.setCursor(0,1);
lcd.print("intentalo denuevo");
delay(5000);
var = 0;
}
}
}
}
Cambiar que los números aparezcan con
arterisco
#include <LiquidCrystal.h>
#include <Key.h>
#include <Keypad.h>
const byte filas = 4;
const byte columnas = 4;
char teclado[filas][columnas]={
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'},
};
byte pinFilas[filas]={7,6,5,4};
byte pinColumnas[columnas] =
{3,2,A5,A4};
Keypad
keypad=Keypad(makeKeymap(teclado),pinFilas,pinColumnas,filas,columnas);
LiquidCrystal lcd(13,12,11,10,9,8);
int tecla;
int a,b,c,d, suma;
int c1=4,c2=6,c3=7,c4=3;
int var=0;
void setup() {
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(5,1);
}
void loop() {
char tecla = keypad.getKey();
if(tecla != NO_KEY){
lcd.print("*");
var++;
tecla = tecla - 48;
switch(var){
case 1:
a = tecla;
break;
case 2:
b = tecla;
break;
case 3:
c = tecla;
break;
case 4:
d = tecla;
if
(a==c1&&b==c2&&c==c3&&d==c4){
lcd.setCursor(0,0);
lcd.print("detectando clave");
delay (5000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" clave correcta");
delay (5000);
lcd.setCursor(0,1);
lcd.print("puerta abierta");
delay (5000);
lcd.clear();
}
else{
lcd.setCursor(0,0);
lcd.print("detectando clave");
delay (5000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("clave incorrecta");
lcd.setCursor(0,1);
lcd.print("intentalo denuevo");
delay(5000);
var = 0;
}
}
}
}
Comunicación sereal de un led
void setup() {
Serial.begin(9600);
pinMode(12,OUTPUT);
}
void loop() {
int a;
if(Serial.available()>0){
a=Serial.read();
}
if(a== 'n'){
x1:
digitalWrite(12,HIGH);
delay(50);
digitalWrite(12,LOW);
delay(50);
goto x1;
}
if(a == 'r'){
x2:
digitalWrite(12,LOW);
goto
x2;
}
}
Suma resta multiplicación
#include
<LiquidCrystal.h>
#include
<Keypad.h>
/*
*
alumno de idat
*
codigo A1515368
*
monbre: jhancarlos
*
apellido: ramos cotrina
*
seccion: 20402_BE
*/
const
byte filas = 4; //tengo 4 filas
const
byte columnas = 4; // tengo 4 columnas
char
keys[filas][columnas] = { // declaramos todas las filaas ylas columnas del
keypad
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'},
};
byte
pinFilas[filas] = {7,6,5,4}; //
declaramos las filas y salidas del
arduino y entre las del lcd para su funcionamiento
byte
pinColumnas[columnas] = {3,2,A5,A4}; // decaramos las columnas del keypad en el
arduino
Keypad
keypad = Keypad(makeKeymap(keys),pinFilas,pinColumnas,filas,columnas);
LiquidCrystal
lcd(13,12,11,10,9,8); //Creamos el objeto lcd y en parentecisis
int
a, b,c , suma, resta, multiplicacion; //Creamos la variable a y b donde se
amacenaran las teclas cunado tenga que sumar, restar, multiplicar
int
var = 0; //Creamos var para elegir los casos y almacenaran los valores en
void
setup(){
lcd.begin(16,2); // declaramos todas las
filas y columnas del lcd para inicializarse
lcd.clear(); // borramos o limpiamos la
pantalla lcd
lcd.setCursor(0,0); // declaramos las
direcciones de nuestro lcd
}
void loop(){
char tecla = keypad.getKey();
if(tecla != NO_KEY){
{
lcd.print(tecla);
tecla = tecla - 48;
var++;
switch(var)
{
case 1: // indicamos el caso 1
a = tecla; // almacena los datos en la
tecla a
break;// rompe el caso para saltar a la siguiente funcion
case 2:
// declaramos el caso dos
b = tecla;// almacena los datos en el la
tecla b
break; // rompe el caso para saltar a la siguiente funcion
}
if(tecla == 17)
{
suma = a+b; // suma los resultados de a y b
lcd.print(suma); // muestra en la pantalla
la suma
var = 0;
}
if(tecla == 18)
{
resta = a-b; // resta los resultados
de a y b
lcd.print(resta); // muestra los resultados
de la suma el la pantalla lcd
var = 0;
}
if(tecla == 19)
{
multiplicacion = a*b; // multiplica los resultados de a y b
lcd.print(multiplicacion); // muestra
en la pantalla los resultados de la
multiplicacion
var = 0;
}
}
}
}
Con arduino y bluetooth
#include<SoftwareSerial.h>
SoftwareSerial
BT (2,3);//PIN2 RECSPCION Y PIN3 TRANSMICION
char
nombre [8] = "JHAN";
char
pin[5]="1234";
char vel='4';
void setup() {
BT .begin(38400);
pinMode(13,OUTPUT);
pinMode(12,OUTPUT);
/****************/
digitalWrite (13,HIGH);
delay(1000);
digitalWrite (13,LOW);
/***************/
digitalWrite (12,HIGH);
delay(2000);
BT.print("AT\r\n" );
BT.print("AT+NAME" );
BT.print(nombre);
BT.print("\r\t");
BT.print("AT+PSWD:" );
BT.print(pin);
BT.print("\r\t");
BT.print("AT+BAUD:" );
BT.print(vel);
BT.print("\r\t");
}
void loop() {
digitalWrite(13,HIGH);
delay(300);
digitalWrite(13,LOW);
delay(300);
}
Programas de arduino
Contador up con led pines 5,4,3,2
int valor;
byte pin;
void setup() {
for(pin=2;pin<6;pin++){
pinMode(pin,OUTPUT);
}
}
void loop() {
for(valor=0;valor<10;valor++){
for(pin=2;pin<6;pin++){
digitalWrite(pin,bitRead(valor,pin-2));
}
delay(1000);
}
}
Contador up dow
int valor;
byte pin;
void setup() {
for(pin=2;pin<6;pin++){
pinMode(pin,OUTPUT);
}
}
void loop() {
for(valor=0;valor<10;valor++){
for(pin=2;pin<6;pin++){
digitalWrite(pin,bitRead(valor,pin-2));
}
delay(1500);
}
for(valor=10;valor>=0;valor--){
for(pin=2;pin<6;pin++){
digitalWrite(pin,bitRead(valor,pin-2));
}
delay(1500);
}
}
Juego de luces con arduino
int luz[]={1,2,4,8,10};
byte pin,a;
void setup() {
for(pin=2;pin<9;pin++){
pinMode(pin,OUTPUT);
}
}
void loop(){
for(a=0;a<7;a++)
{
for(pin=2;pin<9;pin++)
{
digitalWrite(pin,bitRead(luz[a],pin-2));
}
delay(60);
}
}
TRASMICION DE BLUETOOTH CON
UN VALOR
#include<SoftwareSerial.h>
SoftwareSerial BT(2,3);// rx, tx
int a;
void setup() {
BT. begin(9600);
}
void loop() {
if(BT.available() > 0){
a=BT.read();
}
BT.print(a);
BT.print("HOLA");
}
SUMA
#include
<Key.h>
#include
<Keypad.h> // traer sensor de ultrasonido, DHT11,PIR
#include
<LiquidCrystal.h>
LiquidCrystal
lcd(13,12,11,10,9,8); //(RS,E,D4,D5,D6,D7)
const
byte filas = 4;
const
byte col = 4;// const fila siempre sera 4
char
teclado[filas][col] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'},
};
byte
pinFila[filas]= {7,6,5,4};
byte
pinColumna[col]= {3,2,A7,A6};
Keypad
keypad = Keypad(makeKeymap(teclado),pinFila,pinColumna,filas,col);// testea que
tecla se pulsa
char tecla;
int var = 0;
int a, b, c, d, suma;
void setup() {
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
}
void loop() {
tecla = keypad.getKey();
if(tecla != NO_KEY){
lcd.print(tecla);
tecla
= tecla -48;// esto para que me aparesca numeros enteros y no #s char
var++;
switch(var){
case 1:
a = tecla;// if(var == 1)se puede hacer,
pero es mas complejo
break;
case 2:
b = tecla;
break;
case 3:
c = tecla;
break;
case 4:
d = tecla;
break;
}
if (tecla == -13){// el valor de '*' en char,
le restamos 48
suma = a+b+c+d;
lcd.print(suma);
var = 0;
}
}
if(a==1 && b==2 && c==3
&& d==4){//1234 seria la conytraseña
//codigo
}
}
Programar
un led con bluetoothun led al 12 del arduino tx del blueto con el arduino
num,ero 2 rx y rx
del blueto con tx del arduino numero 3
del arduino
#include<SoftwareSerial.h>
SoftwareSerial BT(2,3);// rx, tx
int a;
void setup() {
BT. begin(9600);
pinMode(12,OUTPUT);
}
void loop() {
int a;
if(BT.available() > 0){
a=BT.read();
}
if(a=='3'){
digitalWrite(12,HIGH);
}
if(a=='4'){
digitalWrite(12,LOW);
}
}
Programar el
potenciómetro con A0 del arduino tx del blueto con el arduino
num,ero 2 rx y rx
del blueto con tx del arduino numero 3
del arduino
#include<SoftwareSerial.h>
SoftwareSerial BT(2,3);// rx, tx
void setup() {
BT. begin(9600);
}
void loop() {
int
a;
a=analogRead(A0);
BT.println(a);
}
PROGRAMACIÓN DE MOTOR DE PASO EN ARDUINO
#include <Stepper.h>
#define STEPS 64 // Cambiar a 100 y a 200
//Secuencia 1-3-2-4
//Stepper motor(STEPS, 8, 10, 9, 11);
Stepper motor(STEPS, 4, 6, 5, 7);
int pasos;
void setup() {
}
void loop() {
motor.setSpeed(150); // velocidad rpm
pasos = 500; // Sentido del reloj + 2000 pasos
motor.step(pasos);
delay(2000);
motor.setSpeed(150); // velocidad rpm
pasos = -500; // Sentido contrario al reloj - 2000 pasos
motor.step(pasos);
delay(2000);
}
ARDUINO UNO MAS LCD MAS KEYPAD Y MOTOR DE PASOS
#include <Stepper.h>
#define STEPS 64 // Cambiar a 100 y a 200
//Secuencia 1-3-2-4
//Stepper motor(STEPS, 8, 10, 9, 11);
Stepper motor(STEPS, 4, 6, 5, 7);
int pasos;
#include <LiquidCrystal.h>
//#include <Key.h>
#include <Keypad.h>
const byte filas = 4;
const byte columnas = 4;
char teclado[filas][columnas]={
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'},
};
byte pinFilas[filas]={3,2,A5,A4};
byte pinColumnas[columnas] = {A3,A2,A1,A0};
Keypad keypad=Keypad(makeKeymap(teclado),pinFilas,pinColumnas,filas,columnas);
LiquidCrystal lcd(13,12,11,10,9,8);
int tecla;
int a,b,c,d, suma;
int c1=4,c2=5,c3=6,c4=7;
int var=0;
void setup() {
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Inserte Clave");
lcd.setCursor(4,1);
lcd.print("Porfavor");
delay(1000);
}
void loop() {
char tecla = keypad.getKey();
if(tecla != NO_KEY){
lcd.clear();
lcd.setCursor(5,0);
//lcd.print(tecla);
lcd.print("*");
lcd.setCursor(0,1);
lcd.print("Grabando Clave");
var++;
tecla = tecla - 48;
switch(var){
case 1:
a = tecla;
break;
case 2:
b = tecla;
break;
case 3:
c = tecla;
break;
case 4:
d = tecla;
if (a==c1&&b==c2&&c==c3&&d==c4){
lcd.setCursor(0,0);
lcd.print("Detectando Clave");
lcd.setCursor(1,1);
lcd.print("Clave Grabada");
delay (5000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Clave Correcta");
delay (5000);
lcd.setCursor(1,1);
lcd.print("Puerta Abierta");
motor.setSpeed(150); // velocidad rpm
pasos = 500; // Sentido del reloj + 2000 pasos
motor.step(pasos);
delay(4000);
lcd.clear();
lcd.setCursor(2,0);
lcd.print(" Ten Cuidado");
delay (5000);
lcd.setCursor(1,1);
lcd.print("Cerrando Puerta");
motor.setSpeed(150); // velocidad rpm
pasos = -500; // Sentido contrario al reloj - 2000 pasos
motor.step(pasos);
delay(2000);
var = 0;
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Inserte Clave");
lcd.setCursor(4,1);
lcd.print("Porfavor");
}
else{
lcd.setCursor(0,0);
lcd.print("Detectando Clave");
lcd.setCursor(1,1);
lcd.print("Clave Grabada");
delay (5000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Clave Incorrecta");
lcd.setCursor(0,1);
lcd.print("Intentalo Denuevo");
delay(5000);
var = 0;
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Inserte Clave");
lcd.setCursor(4,1);
lcd.print("Porfavor");
}
}
}
}
PROGRAMACIÓN DE MOTOR DE PASO EN ARDUINO
#include <Stepper.h>
#define STEPS 64 // Cambiar a 100 y a 200
//Secuencia 1-3-2-4
//Stepper motor(STEPS, 8, 10, 9, 11);
Stepper motor(STEPS, 4, 6, 5, 7);
int pasos;
void setup() {
}
void loop() {
motor.setSpeed(150); // velocidad rpm
pasos = 500; // Sentido del reloj + 2000 pasos
motor.step(pasos);
delay(2000);
motor.setSpeed(150); // velocidad rpm
pasos = -500; // Sentido contrario al reloj - 2000 pasos
motor.step(pasos);
delay(2000);
}
ARDUINO UNO MAS LCD MAS KEYPAD Y MOTOR DE PASOS
#include <Stepper.h>
#define STEPS 64 // Cambiar a 100 y a 200
//Secuencia 1-3-2-4
//Stepper motor(STEPS, 8, 10, 9, 11);
Stepper motor(STEPS, 4, 6, 5, 7);
int pasos;
#include <LiquidCrystal.h>
//#include <Key.h>
#include <Keypad.h>
const byte filas = 4;
const byte columnas = 4;
char teclado[filas][columnas]={
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'},
};
byte pinFilas[filas]={3,2,A5,A4};
byte pinColumnas[columnas] = {A3,A2,A1,A0};
Keypad keypad=Keypad(makeKeymap(teclado),pinFilas,pinColumnas,filas,columnas);
LiquidCrystal lcd(13,12,11,10,9,8);
int tecla;
int a,b,c,d, suma;
int c1=4,c2=5,c3=6,c4=7;
int var=0;
void setup() {
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Inserte Clave");
lcd.setCursor(4,1);
lcd.print("Porfavor");
delay(1000);
}
void loop() {
char tecla = keypad.getKey();
if(tecla != NO_KEY){
lcd.clear();
lcd.setCursor(5,0);
//lcd.print(tecla);
lcd.print("*");
lcd.setCursor(0,1);
lcd.print("Grabando Clave");
var++;
tecla = tecla - 48;
switch(var){
case 1:
a = tecla;
break;
case 2:
b = tecla;
break;
case 3:
c = tecla;
break;
case 4:
d = tecla;
if (a==c1&&b==c2&&c==c3&&d==c4){
lcd.setCursor(0,0);
lcd.print("Detectando Clave");
lcd.setCursor(1,1);
lcd.print("Clave Grabada");
delay (5000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Clave Correcta");
delay (5000);
lcd.setCursor(1,1);
lcd.print("Puerta Abierta");
motor.setSpeed(150); // velocidad rpm
pasos = 500; // Sentido del reloj + 2000 pasos
motor.step(pasos);
delay(4000);
lcd.clear();
lcd.setCursor(2,0);
lcd.print(" Ten Cuidado");
delay (5000);
lcd.setCursor(1,1);
lcd.print("Cerrando Puerta");
motor.setSpeed(150); // velocidad rpm
pasos = -500; // Sentido contrario al reloj - 2000 pasos
motor.step(pasos);
delay(2000);
var = 0;
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Inserte Clave");
lcd.setCursor(4,1);
lcd.print("Porfavor");
}
else{
lcd.setCursor(0,0);
lcd.print("Detectando Clave");
lcd.setCursor(1,1);
lcd.print("Clave Grabada");
delay (5000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Clave Incorrecta");
lcd.setCursor(0,1);
lcd.print("Intentalo Denuevo");
delay(5000);
var = 0;
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Inserte Clave");
lcd.setCursor(4,1);
lcd.print("Porfavor");
}
}
}
}
AUTOR : JHANCARLOS RAMOS COTRINA
#include
ResponderBorrarLiquidCrystal lcd(11,10,9,8,7,6);
// RAMOS COTRINA
int HERVIDOR;
#include
SoftwareSerial BT(4,5);// pin 5 RX y pin 4 TX
void setup() {
Serial.begin(9600);// velocidad estandar
BT.begin(9600);
pinMode(13,OUTPUT);
pinMode(12,OUTPUT);
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(2,0);
lcd.print("ESPERANDO... ");
lcd.setCursor(2,1);
lcd.print("ACTIVACION");
}
void loop() {
int HERVIDOR;
if(BT.available()> 0){ //preguntamos si el modulo esta disponible
HERVIDOR = BT.read();// le asignamos el valor leído del modulo bluetooth al valor de 'a'
}
if(HERVIDOR == '1'){//arranque directo en baja tension
digitalWrite(13,HIGH);
digitalWrite(12,HIGH);
lcd.setCursor(0,0);
lcd.print("HERVIDOR ACTIVO ");
lcd.setCursor(1,1);
lcd.print("HIRVIENDO AGUA ");
//delay(40000);
}
if(HERVIDOR== '2'){ //apagamos pulsando 1 o paramos el motor
lcd.clear();
digitalWrite(13,LOW);
digitalWrite(12,LOW);
lcd.setCursor(0,0);
lcd.print("HERVIDOR APAGADO ");
lcd.setCursor(1,1);
lcd.print("AGUA HERVIDA ");
}
}