Вы находитесь на странице: 1из 9

SENSOR PIR

1.- Lectura y prender led


2.- Agregar foco
3.- Llamado de rutinas cuando se activa

LM35:
4.- Formas de leer lm35
4.1.- Directa con formula

float milivolt;
float temp;
int lectura;
void setup()
{
Serial.begin(9600);
}
void loop()
{
lectura = analogRead(A0);
milivolt = (lectura/1023.0)*5000;
temp = milivolt/10;
Serial.print("Temperatura= ");
Serial.print(temp);
Serial.println(" C");
}

------------------------------------
4.2.- Multiplicando por escalar

float temp;
int lectura;
void setup()
{
Serial.begin(9600);
}
void loop()
{
lectura = analogRead(A0);
temp = lectura*0.488758;
Serial.print("Temperatura= ");
Serial.print(temp);
Serial.println(" C");
}

-----------------------------------------------
5.- Prender led si supera cierta temperatura y dar aviso en monitor serial
6.- Conversion de temperaturas

float centigrados;
float farenheit;
float kelvin;
void setup()
{
Serial.begin(9600);
}
float centi()
{
float lectura;
float temp;
lectura=analogRead(A1);
temp=(500*lectura)/1023.0;
return(temp);
}
float fare(float centi) //entre parentesis float centi porque RECIBIRE la
variable centi
{
float f;
f=(9*centi)/5.0 + 32;
return(f); // Retornamos el valor de f y se guardara en la variable
farenheit
}
float kelv(float centi)
{
float k;
k= centi + 273;
return(k);
}

void loop()
{
centigrados=centi();
farenheit=fare(centigrados);
kelvin=kelv(centigrados);
Serial.print("Centigrados = ");
Serial.println(centigrados);
Serial.print("\t");
Serial.print("Faren= ");
Serial.println(farenheit);
Serial.print("\t");
Serial.print("Kelvin= ");
Serial.println(kelvin);
}
------------------------------------------
7.- Ventilador + temperatura

float lectura; //Long tipo de variable de mas alto rango


float mvoltios=0;
float temp=0;
int pinMotor=6;
int pwmmotor=0;
void setup()
{
pinMode(pwmmotor,OUTPUT);
Serial.begin(9600);
}
void loop()
{
lectura=analogRead(A0);
mvoltios=(5000*lectura)/1023.0;
temp=mvoltios/10;
Serial.println(temp);
pwmmotor=map(temp,26,85,0,255);
pwmmotor=constrain(pwmmotor,0,255);
analogWrite(pinMotor,pwmmotor);
}

--------------------------------------------
8.- CONTROL DE TEMPERATURA CON FOCO ( ON / OFF)

int foco = 10;


int lectura;
int ref;
float temp = 0;
float tempf;
void setup()
{
Serial.begin(9600);
pinMode(foco,OUTPUT);
digitalWrite(foco,LOW);
}
void temperatura()
{
for( int i = 1; i<=1000;i++)
{
lectura = analogRead(A0);
delayMicroseconds(5);
temp = (lectura/1023.0)*500 + temp;
}
tempf = temp/1000.0;
Serial.print("Temperatura = ");
Serial.println(tempf);
temp = 0;
}
void loop()
{
temperatura();
if(Serial.available()>0)
{
ref = Serial.readString().toInt();
Serial.print("Temperatura = ");
Serial.print(tempf);
Serial.print("\t");
Serial.print("SetPoint= ");
Serial.println(ref);
}
if(tempf>ref)
{
digitalWrite(foco,LOW);
}
if(tempf<ref)
{
digitalWrite(foco,HIGH);
}
}
--------------------------------------------
INTERRUPCIONES
9.- un pulsador prende y otro apaga mientras uno
parpadea(EXPLICAR RETARDO DE DELAY)

int red = 9;
int green = 10;
int boton = 2;
void setup()
{
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(boton, INPUT);
attachInterrupt(0, prende, FALLING);
attachInterrupt(1,apaga,FALLING);
}
void loop()
{
digitalWrite(green, HIGH);
delay(1000);
digitalWrite(green, LOW);
delay(200);
}
void prende()
{
digitalWrite(red, HIGH);
}
void apaga()
{
digitalWrite(red, LOW);
}
------------------------------------------------------
10.- Parpadea uno y cambio estado de otro

const int emuPin = 10;


const int LEDPin = 13;
const int intPin = 2;
volatile int state = LOW;
void setup() {
pinMode(emuPin, OUTPUT);
pinMode(LEDPin, OUTPUT);
pinMode(intPin, INPUT);
attachInterrupt(digitalPinToInterrupt(intPin), blink, FALLING);
}
void loop() {
//esta parte es para emular la salida
digitalWrite(emuPin, HIGH);
delay(150);
digitalWrite(emuPin, LOW);
delay(150);
}
void blink() {
state = !state;
digitalWrite(LEDPin, state);
}
---------------------------------------------------
11.- Contador de interrupciones

volatile int contador = 0;


int n = contador ;
void setup()
{ Serial.begin(9600);
attachInterrupt( 0, ServicioBoton, FALLING);
}
void loop()
{
if (n != contador)
{ Serial.println(contador);
n = contador ;
}
}
void ServicioBoton()
{ contador++ ;
}
--------------------------------------------------
12.- Contador de vueltas

volatile int contador = 0;


int n = contador ;
void setup()
{ Serial.begin(9600);
attachInterrupt( 0, ServicioBoton, FALLING);
}
void loop()
{

Serial.println(contador);
}
void ServicioBoton()
{ contador++ ;
}
------------------------------------------
13.- RPM

volatile int contador = 0;


void setup()
{
Serial.begin(57600);
attachInterrupt(0,rpm,RISING);
}
void loop()
{
delay(1000);
Serial.println(contador*30);
contador = 0;
}
void rpm()
{
contador++;
}

Вам также может понравиться