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>
- //library inits
- RTC_DS3231 rtc;
- //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 need to be adjusted
- #define RADIUS 10 // definiert den Rotorradius in cm
- //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;
- 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){
- // automatically sets the RTC to the date & time on PC this sketch was compiled
- rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
- }
- }
- 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();
- //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.println("####################");
- Serial.println("rpm:"+(float)rpm));
- //Serial.print("Drehzahl: ");
- //Serial.println((float)rpm);
- Serial.print("Umdrehungsgeschwindigkeit: ");
- Serial.print((float)rs);
- Serial.println("m/s");
- Serial.print("Kontakte: ");
- Serial.println(contacts);
- Serial.print("Windgeschwindigkeit: ");
- Serial.print((float)ws);
- Serial.println("m/s");
- Serial.println(time_inbetween_interrupts);
- //reset contact counter to 0
- contacts = 0;
- test();
- }
- }
- void printTime(){
- DateTime now = rtc.now();
- Serial.print("Date & Time: ");
- Serial.print(now.year(), DEC);
- Serial.print('/');
- Serial.print(now.month(), DEC);
- Serial.print('/');
- Serial.print(now.day(), DEC);
- Serial.print(" (");
- Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
- Serial.print(") ");
- Serial.print(now.hour(), DEC);
- Serial.print(':');
- Serial.print(now.minute(), DEC);
- Serial.print(':');
- Serial.println(now.second(), DEC);
- }
- //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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement