Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Master script combine functions
- -> Rotation measurement
- -> Time reading from RTC
- */
- //library includes
- #include <RTClib.h>
- #include <ArduinoJson.h>
- #include <SPI.h>
- #include <SD.h>
- //library inits
- RTC_DS3231 rtc;
- File myFile;
- //test options
- #define SET_RTC_DATE true //set this to true to initialize the RTC clock with the time of the code upload
- //settings that can to be adjusted
- #define RADIUS 10 // definiert den Rotorradius in cm
- #define LOGFILE_NAME "log.json"
- //static settings
- #define BAUD 9600
- #define REEDPIN 2 //io pin for reed contact (with exterenal 10k pullup resistor to vcc)
- #define DEBOUNCETIME 200 //debouncing interval
- #define INTERVAL 1000 //calculation interval in which the values get sent to serial
- //constant numbers
- #define PI 3.1415926535897932384626433832795 // definiere PI
- //mapping arrays
- char daysOfTheWeek[7][12] = {
- "Sunday",
- "Monday",
- "Tuesday",
- "Wednesday",
- "Thursday",
- "Friday",
- "Saturday"
- };
- //variables
- volatile unsigned int contacts;
- volatile unsigned long time_inbetween_interrupts; // Periodendauer in ms
- unsigned long lastmillis;
- unsigned int lastcontacts;
- // create a StaticJsonDocument for the json data
- StaticJsonDocument<200> doc;
- void setup() {
- attachInterrupt(digitalPinToInterrupt(REEDPIN), count, FALLING); // setzt ISR fürs hochzählen
- Serial.begin(BAUD);
- // set up rtc module
- if (! rtc.begin()) {
- Serial.println("Couldn't find RTC");
- Serial.println("halting program, reset to retry");
- Serial.flush();
- while (1);
- }
- //if this is the first time you use the RTC module you can set the time of the module with this function,
- //disable afterwards in the #define of SET_RTC_DATE
- if (SET_RTC_DATE) {
- // get the compile time
- DateTime compileTime = DateTime(F(__DATE__), F(__TIME__));
- // define your time offset (for example, Berlin is UTC+2 or UTC+1 depending on daylight saving)
- int offsetHours = 2; // change this to your offset
- // adjust the compile time to UTC
- DateTime UTCTime = compileTime.unixtime() - offsetHours * 3600;
- // set the RTC time
- rtc.adjust(UTCTime);
- }
- // set up sd card
- Serial.print("Initializing SD card...");
- if (!SD.begin(4)) {
- Serial.println("initialization failed!");
- Serial.println("halting program, reset to retry");
- while (1);
- }
- Serial.println("SD Card initialization done.");
- }
- void loop() {
- //function runs if the time since the last run is greater than the configured interval
- if (lastmillis + INTERVAL <= millis()) {
- //store time of running the function
- lastmillis = millis();
- DateTime now = rtc.now();
- //date calculations
- uint32_t timestamp = now.unixtime();
- //calculations
- float rpm = 1000.0 / time_inbetween_interrupts;
- float rs = (2 * PI * RADIUS) / time_inbetween_interrupts * 10; // Bahngeschwindigkeitsformel (https://www.leifiphysik.de/mechanik/kreisbewegung/grundwissen/bahngeschwindigkeit-und-winkelgeschwindigkeit)
- float ws = 0.5921 * rpm + 2.3654; // Windgeschwindigkeit wird berechnet
- //serial outputs
- Serial.print("timestamp:"); Serial.print(String(timestamp)); Serial.print(",");
- Serial.print("rpm:"); Serial.print(String(rpm)); Serial.print(",");
- Serial.print("rs:"); Serial.print(String(rs)); Serial.print(",");
- Serial.print("ws:"); Serial.print(String(ws)); Serial.print(",");
- Serial.print("contacts:"); Serial.print(String(contacts)); Serial.print(",");
- Serial.print("delay:"); Serial.print(String(time_inbetween_interrupts)); Serial.println();
- //json output
- doc["timestamp"] = timestamp;
- doc["rpm"] = rpm;
- doc["rs"] = rs;
- doc["ws"] = ws;
- doc["contacts"] = contacts;
- doc["delay"] = time_inbetween_interrupts;
- // serialize json and send it via the Serial interface
- serializeJson(doc, Serial);
- Serial.println();
- //open the logfile
- myFile = SD.open(LOGFILE_NAME, FILE_WRITE);
- // if the file opened okay, write to it:
- if (myFile) {
- Serial.print("Writing to logfile");
- myFile.println("testing 1, 2, 3.");
- // close the file:
- myFile.close();
- Serial.println("done.");
- } else {
- // if the file didn't open, print an error:
- Serial.println("error opening logfile");
- }
- //reset contact counter to 0
- contacts = 0;
- }
- }
- //interrupt routine defined in setup
- void count() {
- static unsigned long last_interrupt_time = 0;
- unsigned long interrupt_time = millis();
- //skipping impulses if they happen to fast (debouncing)
- if (interrupt_time - last_interrupt_time > DEBOUNCETIME) {
- contacts++; // erhöht die gezählten Kontakte
- time_inbetween_interrupts = interrupt_time - last_interrupt_time;
- }
- last_interrupt_time = interrupt_time;
- }
- /* graveyard
- int year = now.year();
- int month = now.month();
- int day = now.month();
- int hour = now.month();
- int minute = now.month();
- int second = now.month()
- Serial.print("year:"); Serial.print(String(year)); Serial.print(",");
- Serial.print("month:"); Serial.print(String(month)); Serial.print(",");
- Serial.print("day:"); Serial.print(String(day)); Serial.print(",");
- Serial.print("hour:"); Serial.print(String(hour)); Serial.print(",");
- Serial.print("minute:");Serial.print(String(minute)); Serial.print(",");
- Serial.print("second:");Serial.print(String(second)); Serial.println();
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement