Hey guys
I am new to arduino and I have to do the raincollector as a project in the university. Its a tipping bucket thing, counting the ticks with a reedswitch. So the things I want my arduino to do is power on if rain falls and turn off if no rain falls over an amount of time, just to save energy. The second thing is to store the date, time and the rainfall every new row in my .txt file.
#include <avr/sleep.h>
#include <Time.h>
#include <Wire.h>
#include <DS3232RTC.h>
#include <SD.h>
#include <SPI.h>
unsigned long previousMillis = 0;
long interval = 10000;
int LED =13;
int REED = 2;
int wakePin = 2; // pin used for waking up
int sleepStatus = 0; // variable to store a request for sleep
int count = 0; // counter fpr sleep
int counter =0; // counter for rain
const int chipSelect = 10;
/*void wakeUpNow()
{
}
*/
void setup()
{
pinMode(LED, OUTPUT);
pinMode(wakePin,INPUT);
pinMode(REED, INPUT);
Serial.begin(9600);
if (!SD.begin(chipSelect))
{
Serial.println("Card failed, or not present");
return;
}
Serial.println("SD-Card initialized.");
/* if(timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
*/
Serial.println("Time set.");
Serial.print('\n');
Serial.print("Rainfall per minute:");
Serial.print('\n');
setSyncProvider(RTC.get); // the function to get the time from the RTC
attachInterrupt(0, wakeUpNow, HIGH);
}
/* void sleepNow()
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
attachInterrupt(0,wakeUpNow, HIGH);
sleep_mode();
sleep_disable();
detachInterrupt(0);
} */
void printDigits(int digits)
{
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(':');
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
void digitalClockDisplay(void)
{
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(' ');
Serial.print(' ');
Serial.print(' ');
Serial.print(day());
Serial.print('.');
Serial.print(month());
Serial.print('.');
Serial.print(year());
Serial.println();
}
/*
void wake ()
{
sleep_disable (); // first thing after waking from sleep:
} // end of wake
*/
void loop()
{
/*Serial.print("Awake for ");
Serial.print(count);
Serial.println("sec");
count++;
delay(1000);
// compute the serial input
if (Serial.available()) {
int val = Serial.read();
if (val == 'S') {
Serial.println("Serial: Entering Sleep mode");
delay(100); // this delay is needed, the sleep
//function will provoke a Serial error otherwise!!
count = 0;
sleepNow(); // sleep function called here
}
}
// check if it should go to sleep because of time
if (count >= 10) {
Serial.println("Timer: Entering Sleep mode");
delay(100); // this delay is needed, the sleep
//function will provoke a Serial error otherwise!!
count = 0;
sleepNow(); // sleep function called here
}
*/
unsigned long currentMillis = millis();
double rain =0;
if(digitalRead(REED))
{
counter++;
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(500);
}
/* else
{
count++;
if(count >= 10)
{
Serial.println("Timer: Entering Sleep mode");
count=0;
sleepNow();
}
}*/
if(currentMillis - previousMillis > interval)
{
previousMillis = currentMillis;
rain = counter * 0.235;
digitalClockDisplay();
Serial.print(rain);
Serial.print(" ml/m^2");
Serial.print('\n');
String string1 ="";
File dataFile = SD.open("datalog.txt", FILE_WRITE);
string1 = String(rain);
dataFile.print(string1);
dataFile.print(" ");
dataFile.print(";");
dataFile.print(" ");
dataFile.close();
counter=0;
}
}