I got any problem of my Anemometer

Hello guys, I got some problem when I combine my code on my button function with my windvelocity function, I make some project for weather station, when I combine both of them, the button function doesn’t work, but when I’ve turn off on my windvelocity function and it’s work

This is my code

#include <DHT.h>
#include <DS3231.h>
#include<Wire.h>

#define RainPin 6
#define DHTPIN 7
#define DHTTYPE DHT22
#define windPin 4 

DS3231 rtc(SDA, SCL);
DHT dht(DHTPIN, DHTTYPE);

Time t;

bool bucketPositionA = false;
const float pi = 3.14159265;
int val = 0;
int val2 = 0;
int pbuttonPin = 3;
int pbuttonPin2 = 4;
int Hor; 
int Min;
int Sec;
int relay = 2;
int relayPin = 10;
int relayPin2 = 11;
int period = 10000;
int delaytime = 10000; 
int radio = 80;
int jml_celah = 18; 
int chk;
int rainstatus;
float hum;
float temp;
float speedwind = 0;
unsigned int Sample = 0; 
unsigned int counter = 0; 
unsigned int RPM = 0;
bool first;

void setup(){
  Wire.begin();
  rtc.begin();
  dht.begin();
  Serial.begin(9600);
  pinMode(relay, OUTPUT);
  pinMode(relayPin, OUTPUT);
  pinMode(relayPin2, OUTPUT);
  pinMode(RainPin, INPUT);
  pinMode(4,INPUT);
  pinMode(pbuttonPin, INPUT_PULLUP); 
  pinMode(pbuttonPin2, INPUT_PULLUP); 
  digitalWrite(4, HIGH);
  digitalWrite(relay, HIGH);
  digitalWrite(relayPin, HIGH);
  digitalWrite(relayPin2, HIGH);
}

void loop(){
  /*t = rtc.getTime();
  Hor = t.hour;
  Min = t.min;
  Sec = t.sec;
  hum = dht.readHumidity();
  temp = dht.readTemperature();*/
  val = digitalRead(pbuttonPin);
  val2 = digitalRead(pbuttonPin2);
  
  
  /*if(Hor == 11 && Min == 19 && Sec == 20){
    digitalWrite(relay, LOW);
    delay(300000);
    digitalWrite(relay, HIGH);*/
  }
    button();
    windvelocity();
    RPMcalc();
    WindSpeed();
    raingauge();
    
    if(hum > 80 && temp < 17 && speedwind > 1.9444){
        digitalWrite(relayPin, LOW);
        delay(180000); //bisa diganti sesuai kebutuhan
        digitalWrite(relayPin, HIGH);
    }
    if(rainstatus == 1){
      if(rainstatus == 0){
        digitalWrite(relayPin2, LOW);
        delay(180000);
        digitalWrite(relayPin2, HIGH);
      }
    }
    
}
void windvelocity(){
  speedwind = 0;
  counter = 0;
  attachInterrupt(0, addcount, CHANGE);
  unsigned long millis();
  long startTime = millis();
  while(millis() < startTime + period){}
  detachInterrupt(1);
}
void RPMcalc(){
  RPM=((counter/jml_celah)*60)/(period/1000);
}
void WindSpeed(){
  speedwind = ((2 * pi * radio * RPM)/60) / 1000;
}
void addcount(){
  counter++;
}
void raingauge(){
  if((bucketPositionA==false)&&(digitalRead(RainPin)==LOW)){
    bucketPositionA=true;
    rainstatus = 1;
  }
  if((bucketPositionA==true)&&(digitalRead(RainPin)==HIGH)){
    bucketPositionA=false;
    rainstatus = 0;
  }
}
void button(){
  
  if(val == LOW){
    digitalWrite(relayPin, LOW);
    delay(10000);
    digitalWrite(relayPin, HIGH);
   }else{
    }   
    if(val2 == LOW){
    digitalWrite(relayPin2, LOW);
    delay(10000);
    digitalWrite(relayPin2, HIGH);
   }else{
    }   
}

*sorry I have a bad english, pardon my english :frowning:

        delay(180000); //bisa diganti sesuai kebutuhan

I don't know what the comment says but it should say "do nothing for 180 seconds and that includes reading input pins"

Also

void WindSpeed(){
  speedwind = ((2 * pi * radio * RPM)/60) / 1000;
}

The value of radio never changes in the program so neither will speedWind

it’s mean the relay will turn on in 3 minutes, and the comment means you can easily change what do you want…

fyi i got stuck on “while(millis() < startTime + period){}” in line 101, when I unactivated this code, my code for button work, but when I activated this code, it doesn’t work

fyi i got stuck on “while(millis() < startTime + period){}” in line 101

This is the same as “delay(period)”, but that is an equally bad idea.

millis() tutorial

Get rid of ALL the interrupt code. Interrupts will not work the way you are trying to use them. Instead, just poll the port pin.

it's mean the relay will turn on in 3 minutes, and the comment means you can easily change what do you want..

It means that nothing will happen for 3 minutes after the relay turns on , including reading your button inputs for the period of the delay()

Is that what you want to happen ?

I got problem in this function

void button(){
  
  if(val == LOW){
    digitalWrite(relayPin, LOW);
    delay(10000);
    digitalWrite(relayPin, HIGH);
   }else{
    }   
    if(val2 == LOW){
    digitalWrite(relayPin2, LOW);
    delay(10000);
    digitalWrite(relayPin2, HIGH);
   }else{
    }   
}

then when I activated this code

void windvelocity(){
  speedwind = 0;
  counter = 0;
  attachInterrupt(0, addcount, CHANGE);
  unsigned long millis();
  long startTime = millis();
  while(millis() < startTime + period){}
  detachInterrupt(1);
}

it doesn’t work well when I push the button, when I unactivated this code, the button is work.

so in my project, I’ll make weather station, when the weather want to rain, sensor will detect and relay for motor reverse is on, and the button for the manual for turn on the motor reverse, before I combine button function in this code, it’s still work. I have more plan I need 2 arduino to run this project, first for automatic with sensor and once again for manual, it’s good idea sir? btw this is my first project with anemometer

*btw pardon my english, I hope u know what I mean[/code]

I got problem in this function

There is a problem viewing your code because the forum software has inserted tags, which is a known problem and has been reported. Please turn of the WYSIWYG option in your profile and post the code again

Many thanks

UKHeliBob:
There is a problem viewing your code because the forum software has inserted tags, which is a known problem and has been reported. Please turn of the WYSIWYG option in your profile and post the code again

Many thanks
[/quote]
Sorry before, thanks for reminding me, I've been fixed it sir