Advertisement
Johanneszockt1

Untitled

Jun 27th, 2023
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. /*
  2. Master script combine functions
  3.  
  4. */
  5.  
  6. //settings that need to be adjusted
  7. #define RADIUS 10 // definiert den Rotorradius in cm
  8.  
  9. //static settings
  10. #define BAUD 9600
  11. #define REEDPIN 2 //io pin for reed contact (with exterenal 10k pullup resistor to vcc)
  12. #define DEBOUNCETIME 200 //debouncing interval
  13. #define INTERVAL 1000 //calculation interval in which the values get sent to serial
  14.  
  15.  
  16. //constant numbers
  17. #define PI 3.1415926535897932384626433832795 // definiere PI
  18.  
  19. //variables
  20. volatile unsigned int contacts;
  21. volatile unsigned long time_inbetween_interrupts; // Periodendauer in ms
  22. unsigned long lastmillis;
  23. unsigned int lastcontacts;
  24.  
  25.  
  26. void setup() {
  27. attachInterrupt(digitalPinToInterrupt(REEDPIN), count, FALLING); // setzt ISR fürs hochzählen
  28. Serial.begin(BAUD);
  29. }
  30.  
  31. void loop() {
  32. //function runs if the time since the last run is greater than the configured interval
  33. if (lastmillis + INTERVAL <= millis()) {
  34. //store time of running the function
  35. lastmillis = millis();
  36.  
  37. //calculations
  38. float rpm = 1000.0 / time_inbetween_interrupts;
  39. float rs = (2 * PI * RADIUS) / time_inbetween_interrupts * 10; // Bahngeschwindigkeitsformel (https://www.leifiphysik.de/mechanik/kreisbewegung/grundwissen/bahngeschwindigkeit-und-winkelgeschwindigkeit)
  40. float ws = 0.5921 * rpm + 2.3654; // Windgeschwindigkeit wird berechnet
  41.  
  42. //serial outputs
  43. Serial.print("Drehzahl: ");
  44. Serial.println((float)rpm);
  45.  
  46. Serial.print("Umdrehungsgeschwindigkeit: ");
  47. Serial.print((float)rs);
  48. Serial.println("m/s");
  49.  
  50. Serial.print("Kontakte: ");
  51. Serial.println(contacts);
  52.  
  53. Serial.print("Windgeschwindigkeit: ");
  54. Serial.print((float)ws);
  55. Serial.println("m/s");
  56.  
  57. Serial.println(time_inbetween_interrupts);
  58.  
  59. //reset contact counter to 0
  60. contacts = 0;
  61. }
  62. }
  63. //interrupt routine defined in setup
  64. void count() {
  65. static unsigned long last_interrupt_time = 0;
  66. unsigned long interrupt_time = millis();
  67. //skipping impulses if they happen to fast (debouncing)
  68. if(interrupt_time - last_interrupt_time > DEBOUNCETIME){
  69. contacts++; // erhöht die gezählten Kontakte
  70. time_inbetween_interrupts = interrupt_time - last_interrupt_time;
  71. }
  72.  
  73. last_interrupt_time = interrupt_time;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement