]> fbox.kageds.com Git - richie-water-pump.git/blob - main.cpp
1st commit
[richie-water-pump.git] / main.cpp
1 /**
2 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7
8 #include <stdio.h>
9 #include "pico/stdlib.h"
10 #include "hardware/gpio.h"
11 #include "hardware/adc.h"
12
13 #include "sevseg.h"
14 SevSeg sevseg; //Instantiate a seven segment controller object
15
16 #define PUMP_PIN 16
17 #define BTN_PIN 11
18
19 volatile bool button_pressed = false;
20 volatile int time_left;
21 // Debounce control
22 volatile unsigned long time = to_ms_since_boot(get_absolute_time());
23 const int delayTime = 300; // Delay for every push button may vary
24
25 bool timer_cb(struct repeating_timer *t) {
26 time_left--;
27 return true;
28 }
29
30 void button_cb(uint gpio, uint32_t events) {
31 if ((to_ms_since_boot(get_absolute_time())-time)>delayTime) {
32 time = to_ms_since_boot(get_absolute_time());
33 printf("GPIO Interupt %d\n", gpio);
34 button_pressed = true;
35 }
36 }
37
38 int main() {
39 const uint LED_PIN = PICO_DEFAULT_LED_PIN;
40 stdio_init_all();
41 gpio_init(LED_PIN);
42 gpio_set_dir(LED_PIN, GPIO_OUT);
43
44 gpio_init(PUMP_PIN);
45 gpio_set_dir(PUMP_PIN, GPIO_OUT);
46 gpio_put(PUMP_PIN, false);
47
48 adc_init();
49 // Make sure GPIO is high-impedance, no pullups etc
50 adc_gpio_init(26);
51 // Select ADC input 0 (GPIO26)
52 adc_select_input(0);
53
54 // 12-bit conversion, assume max value == ADC_VREF == 3.3 V
55 const float conversion_factor = 60.0f / (1 << 12);
56 uint16_t result;
57
58 byte numDigits = 2;
59 byte digitPins[] = {9, 21};
60 byte segmentPins[] = {2, 3, 22, 28, 27, 7, 8, 9};
61 bool resistorsOnSegments = true; // 'false' means resistors are on digit pins
62 byte hardwareConfig = COMMON_CATHODE; // See README.md for options
63 bool updateWithDelays = false; // Default. Recommended
64 bool leadingZeros = true; // Use 'true' if you'd like to keep the leading zeros
65 sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments, updateWithDelays, leadingZeros);
66
67 gpio_init(BTN_PIN);
68 gpio_pull_down(BTN_PIN);
69 gpio_set_irq_enabled_with_callback(BTN_PIN, GPIO_IRQ_EDGE_RISE, true, &button_cb);
70
71 struct repeating_timer timer;
72 bool timer_running = false;
73
74 while (true) {
75 gpio_put(LED_PIN, true);
76 if (button_pressed && !timer_running) {
77 add_repeating_timer_ms(-1000 * 60, timer_cb, NULL, &timer);
78 gpio_put(PUMP_PIN, true);
79 timer_running = true;
80 button_pressed= false;
81 }
82 else if (button_pressed && timer_running) {
83 cancel_repeating_timer(&timer);
84 gpio_put(PUMP_PIN, false);
85 timer_running = false;
86 button_pressed = false;
87 }
88 if (timer_running && time_left <= 0) {
89 cancel_repeating_timer(&timer);
90 timer_running = false;
91 gpio_put(PUMP_PIN, false);
92 }
93 if (!timer_running) {
94 result = adc_read();
95 time_left = (int)(result * conversion_factor) + 1;
96 }
97 sevseg.setNumber(time_left, 0);
98 sevseg.refreshDisplay(); // Must run repeatedly
99 gpio_put(LED_PIN, false);
100 }
101 }