Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Master script combine functions
- */
- //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
- //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);
- }
- 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.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;
- }
- }
- //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