I need some help.. im new in arduino

I desperately need a help… i have my arduino uno, sd shield, Serial LCD display, and a tipping bucket.

my goal is to make a digital tipping bucket.

procedure:

  1. Read data from the tipping bucket every time it ticked.
  2. display data from Serial LCD display.
  3. save the data from SD shield including time and date when the tipping bucket tipped.

my question is how to program a CLOCK without using any external hardware, and how sync it with the tipping bucket?

here is my code

#include <SoftwareSerial.h>
#include <SD.h>
#define  MODEPIN  2


File myFile;

const int switchPin = 7;     //     // Reed switch to digital pin 7
int counter;
int infinite; // (/)
int tipped;
int nt;
float perTipped; // (/)
//---------------------
void setup(){
Serial.begin(9600);
delay(1500);          // Allow time for Serial LCD to set up
digitalWrite(MODEPIN, HIGH);
pinMode(switchPin, INPUT);        // switchPin is an input
digitalWrite(switchPin, HIGH);    // Activate internal pullup resistor
Serial.write(0x02);
Serial.print("F");
Serial.write(0x03);

Serial.write(0x02); //STX code
Serial.print("c"); //c – Clear LCD function
Serial.write(0x03); //ETX code

while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  
  Serial.write(0x02);
  Serial.print("1");
  Serial.print("Starting SD card...");
  Serial.write(0x03);
  
   pinMode(10, OUTPUT);
   
  if (!SD.begin(4)) {
  Serial.write(0x02);
  Serial.print("2");
  Serial.print("Starting failed!");
  Serial.write(0x03);
  return;
  }
  Serial.write(0x02);
  Serial.print("2");
  Serial.println("Starting done.");
  Serial.write(0x03);

Serial.write(0x02); //STX code
Serial.print("c"); //c – Clear LCD function
Serial.write(0x03); //ETX code

};

void loop(){
  tipped = digitalRead(switchPin);  // 
  delay(70);
  perTipped += 0.50;
  nt += 1;
  myFile = SD.open("robin.txt", FILE_WRITE);

  myFile.print(perTipped);
  myFile.print("    ");
  myFile.println(nt);
  myFile.close();
  
  Serial.write(0x02);      //STX
  Serial.print(">");      // Send position info
  Serial.print(a);
  Serial.write(0x03);    //ETX
  Serial.write(0x02); //STX
  Serial.print("3");
  Serial.print(perTipped); //
  Serial.write(0x03); //ETX
//---------------------------------------
  Serial.write(0x02);      //STX
  Serial.print(">");      // Send position info
  Serial.print(a);
  Serial.write(0x03);    //ETX
  Serial.write(0x02); //STX
  Serial.print("4");
  Serial.print(nt); //
  Serial.write(0x03); //ETX
};

firstly the time issue

There is a library for managing time http://playground.arduino.cc/Code/Time

However you'd need to somehow set this, and there is no way to do this without some sort of external hardware, i.e either a realtime clock module, or other clever solutions like getting the time via a Wifi connection etc.

Re: triggering when the bucket is tipped

Firstly you'll need to debounce your switch see http://playground.arduino.cc/Code/Bounce

Secondly, in your loop you need to wait for the "switchPin" to change state (ie as a function of the debounce code)

i.e if the switch is active high, then without debounce you would need to do something like this

void loop(){
  while(!digitalRead(switchPin));  // Poll waiting for the switch to go HIGH (NOTE THIS NEEDS TO BE DEBOUNCED !!!!)


//  delay(70); // this is pointless if you use debounce


  perTipped += 0.50;
  nt += 1;
  myFile = SD.open("robin.txt", FILE_WRITE);

  myFile.print(perTipped);
  myFile.print("    ");
  myFile.println(nt);
  myFile.close();
  
  Serial.write(0x02);      //STX
  Serial.print(">");      // Send position info
  Serial.print(a);
  Serial.write(0x03);    //ETX
  Serial.write(0x02); //STX
  Serial.print("3");
  Serial.print(perTipped); //
  Serial.write(0x03); //ETX
//---------------------------------------
  Serial.write(0x02);      //STX
  Serial.print(">");      // Send position info
  Serial.print(a);
  Serial.write(0x03);    //ETX
  Serial.write(0x02); //STX
  Serial.print("4");
  Serial.print(nt); //
  Serial.write(0x03); //ETX
};

rogerClark: There is a library for managing time http://playground.arduino.cc/Code/Time

However you'd need to somehow set this, and there is no way to do this without some sort of external hardware, i.e either a realtime clock module, or other clever solutions like getting the time via a Wifi connection etc.

I agree with Roger, I think external hardware for keeping time is your best bet. Check out Adafruit's DS1307 module;

https://www.adafruit.com/products/264

You can stack it right on your Arduino, and it will last for ~5 years. It will also keep time while the Ardunio is off. Also, her tutorials accompanying this chip will also answer many questions about how to utilize a Real Time Clock (RTC). It helped me out with my plant watering system;

https://learn.adafruit.com/ds1307-real-time-clock-breakout-board-kit

If you can't wait for the RTC module, download the library it uses(example "softRTC"). This example "fakes" an RTC module using Millis(). It's incredibly inaccurate over the long run, but it will get you started with RTC code;

https://github.com/adafruit/RTClib

I think it would help to describe your project in more detail.

In particular ... How often does the bucket tip? (shortest and longest intervals) What sort of signal is produced when the bucket tips? How long is the "bucket tipped" signal available to be detected?

I suspect that, in an Arduino context, the bucket tips very infrequently even at its fastest.

If that is true all you need is code to check the state of the bucketTipped pin every iteration of loop and use millis() or the RTC to measure the time.

When the code detects bucketTipped it can calculate the elapsed time since the previous tip and write the number to the SD card. Even if that takes a few millisecs there is no (?) likelihood of another bucketTip in such a short time.

...R