Advertisement
Guest User

Untitled

a guest
Mar 31st, 2025
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. const int PUMP_PIN = 9;     // output to mosfet driving the pump
  2. const int BUTTON_PIN = 2;   // input from start button
  3. const int LED_PIN = 13;     // status led
  4.  
  5. const int PULSE_COUNT = 1000;   // number of pulses to generate
  6. const int PULSE_WIDTH = 22;     // pulse width in milliseconds
  7. const int PERIOD = 200;         // total period in milliseconds (5hz = 200ms)
  8.  
  9. void setup() {
  10.   // set up pin modes
  11.   pinMode(PUMP_PIN, OUTPUT);
  12.   pinMode(LED_PIN, OUTPUT);
  13.   pinMode(BUTTON_PIN, INPUT_PULLUP);
  14.  
  15.   // ensure pump is off at startup
  16.   digitalWrite(PUMP_PIN, LOW);
  17.  
  18.   // initialise serial for debugging
  19.   Serial.begin(9600);
  20.   Serial.println("diesel pump calibration system");
  21.   Serial.println("press button to start 1000 pulses at 5hz");
  22. }
  23.  
  24. void loop() {
  25.   // wait for button press (active low due to pull-up)
  26.   while (digitalRead(BUTTON_PIN) == HIGH) {
  27.     // blink led while waiting
  28.     digitalWrite(LED_PIN, !digitalRead(LED_PIN));
  29.     delay(100);
  30.   }
  31.  
  32.   // debounce button
  33.   delay(50);
  34.  
  35.   // turn on led to indicate running
  36.   digitalWrite(LED_PIN, HIGH);
  37.  
  38.   Serial.println("starting 1000 pulses sequence...");
  39.  
  40.   // generate 1000 pulses
  41.   for (int i = 0; i < PULSE_COUNT; i++) {
  42.     // turn on pump
  43.     digitalWrite(PUMP_PIN, HIGH);
  44.    
  45.     // keep on for pulse width duration
  46.     delay(PULSE_WIDTH);
  47.    
  48.     // turn off pump
  49.     digitalWrite(PUMP_PIN, LOW);
  50.    
  51.     // wait for remainder of period
  52.     delay(PERIOD - PULSE_WIDTH);
  53.    
  54.     // print progress every 100 pulses
  55.     if (i % 100 == 0) {
  56.       Serial.print("pulses completed: ");
  57.       Serial.println(i);
  58.     }
  59.   }
  60.  
  61.   // turn off led when complete
  62.   digitalWrite(LED_PIN, LOW);
  63.  
  64.   Serial.println("sequence complete! 1000 pulses delivered.");
  65.   Serial.println("check if you have 65ml in the measuring flask.");
  66.  
  67.   // wait for button release before allowing another cycle
  68.   while (digitalRead(BUTTON_PIN) == LOW) {
  69.     delay(10);
  70.   }
  71.  
  72.   // debounce button release
  73.   delay(50);
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement