Hello,
This script which I use for my DIY weather station is missing pulses!!!!
The sensor are used are IR sensor/course. they counts wholes in a disc.
The read out is done by means of a xbee and display.
Your help is apreciated.
Theo
ps. script placed in attachment was not accepted?????
//Teller programma voor windrichting
//DD 26-01-2011
//xbee_send_serial_Data_teller_01_04
//zonder display !!!!! WERKT NIET ECHT LEKKER MEER TELT NIET PER EEN OP/NEER!!!!!!!!!!!!
// DISPLAY FUNCTIE VERWIJDERD.
//Op een mega board met serial voor xbee te zenden naar coordinater
//probleem interrupt 5 op pin 18 checken nu middels input opgelost
//display dusdanig aansluiten dat interrupt niet in de weg zitten
//vervalt dus ook IR sensor noord set op input 4
//bij opstarten eerst een volle slag maken met e wind vaan.
//#include <ctype.h>
// e.e.a. met windsnelheids meeting interrupt 4 pin 19
#define encoder0PinA 2 // interrupt
#define encoder0PinB 3 // interrupt
#define encoder0PinC 18 // interrupt 5 op pin 18
#define encoder0PinD 19 // interrupt 4 op pin 19
volatile unsigned int encoder0Pos = 16;
unsigned long time;
unsigned long timeold;
unsigned long timenew;
unsigned long millisnew;
unsigned int tmp_Pos = 0;
int led = 13; // for checking nul ""o"" doorgang
int ledA = 24;
int ledB = 26;
boolean A_set;
boolean B_set;
boolean set_new;
int count;
int setmillsnew;
void setup()
{
pinMode(encoder0PinA, INPUT);
pinMode(encoder0PinB, INPUT);
pinMode(encoder0PinC, INPUT);
pinMode(encoder0PinD, INPUT);
pinMode(led, OUTPUT);
pinMode(ledA, OUTPUT);
pinMode(ledB, OUTPUT);
boolean A_set;
boolean B_set;
boolean set_new;
count = 0;
timeold = 0;
timenew = 0;
// encoder pin on interrupt 0 (pin 2)
attachInterrupt(0, doEncoderA, CHANGE);
// encoder pin on interrupt 1 (pin 3)
attachInterrupt(1, doEncoderB, CHANGE);
// encoder pin on interrupt 5 (pin 18)
attachInterrupt(5, doEncoderC, CHANGE);
// encoder pin on interrupt 4 (pin 19)
attachInterrupt(4, doEncoderD, RISING);
Serial.begin (9600);
set_new = false;
}
void loop()
{
time = timenew - timeold;
if (tmp_Pos != encoder0Pos)
{
Serial.print("<W:");
Serial.print(encoder0Pos, DEC);
Serial.print(":");
//Serial.print(time, DEC);
Serial.print(count, DEC);
Serial.print(">");
tmp_Pos = encoder0Pos;
}
if (encoder0Pos < 16){
encoder0Pos= encoder0Pos + 18;
}
}
void doEncoderA() { // Interrupt on A changing state
if (digitalRead(encoder0PinA) == HIGH) { // Low to High transition?
A_set = true;
digitalWrite(ledA, LOW);
if (!B_set) {
encoder0Pos = encoder0Pos + 1;
}
}
if (digitalRead(encoder0PinA) == LOW) { // High-to-low transition?
A_set = false;
digitalWrite(ledA, HIGH);
}
}
void doEncoderB() { // Interrupt on B changing state
if (digitalRead(encoder0PinB) == HIGH) { // Low-to-high transition?
B_set = true;
digitalWrite(ledB, LOW);
if (!A_set) {
encoder0Pos = encoder0Pos - 1;
}
}
if (digitalRead(encoder0PinB) == LOW) { // High-to-low transition?
B_set = false;
digitalWrite(ledB, HIGH); }
}
void doEncoderC() {
if (digitalRead(encoder0PinC) == LOW) {
digitalWrite( led , HIGH);
encoder0Pos = 16;
}
else{ digitalWrite( led , LOW);
}
}
void doEncoderD() {
if(digitalRead(encoder0PinD) == HIGH) {
if(set_new == true) {
while (count <= 100) {
count ++;
}
}
set_new == true;
millisnew = millis();
count = 0;
}
}