Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- Windmesser, Arduino Programm
- Name: windmesseer.ino
- Purpose: Counts the impulses from a reed contact and logs that data to the sd card
- Arduino IDE Version: 2.1.0
- @author Johannes Röring
- @version 1.0.0 28/06/23
- */
- /*
- --User Settings and Constants--
- this part may be edited by the user if needed
- */
- #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 SERIAL_BAUD 9600
- #define REEDPIN 2 //io pin for reed contact (with exterenal 10k pullup resistor to vcc)
- #define LOGFILE_NAME "log.txt"
- #define RADIUS 10 // definiert den Rotorradius in cm
- #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
- /*
- --Including and Setting up Libraries--
- !!!Dont edit below this point unless you know what you are doing!!!
- */
- //includes
- #include <RTClib.h>
- #include <ArduinoJson.h>
- #include <SPI.h>
- #include <SD.h>
- //library declarations
- RTC_DS3231 rtc;
- File myFile;
- /*
- --Variables for the code--
- */
- //variables
- volatile unsigned int contacts; // counter for reed contact pulses
- volatile unsigned long time_inbetween_interrupts; // time since the last interrupt trigger
- unsigned long lastmillis; // last timestamp that data was proceessed at
- unsigned int lastcontacts; // rolling counter of contacts
- StaticJsonDocument<200> doc; // space for the json array to fit into
- /*
- --Setup Function--
- this Function runs only once at powerup and reset
- it initializes all relevant pieces before the cyclic operation starts
- */
- void setup() {
- //attatch interrupt for handling the reed contact
- attachInterrupt(digitalPinToInterrupt(REEDPIN), count, FALLING);
- //start the serial interface (BAUD rate is defined at the top)
- Serial.begin(SERIAL_BAUD);
- init_rtc(); //calls init function for rtc
- init_sd(); //calls init function for sd
- }
- /*
- --Loop Function--
- this Function runs in a constant loop and does everything that is not done through interrupts
- */
- void loop() {
- //function runs if the time since the last run is greater than the configured interval
- if (lastmillis + INTERVAL <= millis()) {
- process_data();
- }
- }
- /*
- --Interrupt routine for Reed contact--
- this Function runs every time the reed contact gets triggered
- it procecsses the impulse
- */
- 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;
- }
- /*
- --Processing Function for the Loop--
- this Function gets called from loop roughly every second
- it procecsses the counted data and writes it to serial and the sd card
- */
- void process_data() {
- //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;
- //open the logfile
- myFile = SD.open(LOGFILE_NAME, FILE_WRITE);
- // if the file opened okay, write to it:
- if (myFile) {
- serializeJson(doc, myFile);
- myFile.println(","); //add a newline after the json data
- // close the file:
- myFile.close();
- } else {
- // if the file didn't open, print an error:
- Serial.println("error opening logfile");
- }
- //reset contact counter to 0
- contacts = 0;
- }
- /*
- --INIT Function for the RTC Module--
- this Function runs only once in the void setup
- it initializes the RTC Module
- */
- void init_rtc() {
- // 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) {
- adjust_rtc_date();
- }
- }
- /*
- --Setting Function for the RTC Module--
- this Function runs only once in the void setup if the flag is set to true
- it initializes the RTC Module with the timestamp that was provided during compilation
- */
- void adjust_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);
- }
- /*
- --INIT Function for the SD Card--
- this Function runs only once in the void setup
- it initializes all the sd card module
- */
- void init_sd() {
- // 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.");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement