Arduino
/// Control de temperatura, contador de pulsos y monitor LCD
//fecha 28/08/2009
//version 2.0
// Nicolas Gravel
#include <LCD4Bit_mod.h>
//create object to control an LCD.
//number of lines in display=1
LCD4Bit_mod lcd = LCD4Bit_mod(2);
#include </usr/lib/avr/include/avr/interrupt.h>
#include </usr/lib/avr/include/avr/io.h>
#define ATMEGA328
#define INIT_TIMER_COUNT 6
#define RESET_TIMER2 TCNT2 = INIT_TIMER_COUNT
//some messages to display on the LCD
char welcome[15]=”Termocontrol”;
char welcome1[15]=”v.1″;
char sval[6]=” “;
//this is for the termistor
int term = 0; // select the input pin for the termistor
int ledPin = 13; // select the pin for the LED
int watero=13; //
int Val= 0;
int sensor;
int promedio;
int index = 0; // the index of the current reading
int total = 0; // the running total
int contador = 0 ; //cuenta de loops
int umbral=23;
int lpm;
// for Timer
int pulsador=1;
int minutos;
long elapsedTime ; // elapsed time for stop watch
long initInterval;
volatile int pulsos = 0;
//minutos
int newMinute=0;
int oldMinute=0;
int vueltas=0;
//Rutina de interrupcion interna de timer2
int int_counter = 0;
volatile int second = 0;
int oldSecond = 0;
long starttime = 0;
//Here we define the basic ISR associated with TIMER2 (overflow) linked to vector(4)
// Aruino runs at 16 Mhz, so we have 1000 Overflows per second…
// 1/ ((16000000 / 64) / 256) = 1 / 1000
ISR(TIMER2_OVF_vect) {
RESET_TIMER2;
int_counter += 1;
if (int_counter == 1000) {
second+=1;
int_counter = 0;
}
};
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT); //pin watero
// esto es para el segundero usando los registros del TIMER2 y la libreria avr
Serial.println(“Initializing timerinterrupt”);
//Timer2 Settings: Timer Prescaler /64,
TCCR2B |= (1<<CS22); // turn on CS22 bit
TCCR2B &= ~((1<<CS21) | (1<<CS20)); // turn off CS21 and CS20 bits
// Use normal mode
TCCR2A &= ~((1<<WGM21) | (1<<WGM20)); // turn off WGM21 and WGM20 bits
// Use internal clock – external clock not used in Arduino
ASSR |= (0<<AS2);
//Timer2 Overflow Interrupt Enable
TIMSK2 |= (1<<TOIE2) | (0<<OCIE2A); //Timer2 Overflow Interrupt Enable
RESET_TIMER2;
sei();
starttime = millis();
lcd.init();
lcd.clear();
lcd.printIn(welcome);
lcd.cursorTo(2,0);
lcd.printIn(welcome1);
delay(3000);
lcd.leftScroll(20, 50);
lcd.clear();
lcd.cursorTo(1,0);
lcd.printIn(“TMP=”);
lcd.cursorTo(1,9);
lcd.printIn(“LPM=”);
lcd.cursorTo(2,0);
lcd.printIn(“ANS=”);
lcd.cursorTo(2,9);
lcd.printIn(“——-”);
elapsedTime = millis() ; //contador milisegundos desde inicio arduino
initInterval=elapsedTime;
attachInterrupt(1, rsi, RISING);
}
void loop() {
minutos = ((elapsedTime-initInterval) / 1000L)/60; //conversor a minutos
lcd.cursorTo(2,5);
itoa(minutos,sval,10);
lcd.printIn(sval);
//for termistor smoothing
promedio=0;
index=0;
do {
sensor=analogRead(term);
promedio=promedio + sensor;
index=index+1;
}
while(index < 10);
//* < >
promedio =promedio/10;
Val = ( promedio – 362)/9.05;
//Serial.println(Val);
itoa(Val,sval,10);
lcd.cursorTo(1,5);
lcd.printIn(sval);
Serial.print(sensor,DEC); // Envia valor del sebnsor en formato ASCII decimal
if (Val < umbral) {
digitalWrite(watero,HIGH);
}
if (Val > umbral+2) {
digitalWrite(watero,LOW);
}
elapsedTime = millis() ;
if(analogRead(1)>500) {
initInterval=elapsedTime;
lcd.cursorTo(2,5);
lcd.printIn(“ “);
}
// contador de pulsos
/* itoa(pulsos,sval,10);
lcd.cursorTo(1,13);
lcd.printIn(sval);
// contador pulsos por minutos simple
/*
if(oldMinute != minutos) {
itoa(pulsos,sval,10);
lcd.cursorTo(1,13);
lcd.printIn(sval);
pulsos=0;
oldMinute=minutos;
}*/
//contador pulsos por minutos con timer2
if (oldSecond != second) {
// Serial.print(vueltas);
vueltas++;
// Serial.print(second);
// Serial.print(“. ->”);
oldSecond = second;
}
if (vueltas == 60) {
vueltas=0;
lpm=pulsos;
//Serial.print(lpm);
itoa(lpm,sval,10);
lcd.cursorTo(1,13);
lcd.printIn(sval);
pulsos=0;
}
lcd.cursorTo(2,12);
lcd.printIn(“-”);
if (pulsos!=contador) {
lcd.cursorTo(2,12);
lcd.printIn(“^”);
contador=pulsos; }
}
//**************************************************
void rsi()
{
pulsos = pulsos + 1;
}