Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@4:3b51cf89299b, 2017-07-31 (annotated)
- Committer:
- Magnus_tl
- Date:
- Mon Jul 31 21:29:53 2017 +0000
- Revision:
- 4:3b51cf89299b
- Parent:
- 1:5d1abf5d24a5
Reinserted correct TextLCD library.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Magnus_tl | 0:9b09ea979c72 | 1 | #include "mbed.h" |
| UBAL | 1:5d1abf5d24a5 | 2 | #include "TextLCD.h" |
| UBAL | 1:5d1abf5d24a5 | 3 | |
| UBAL | 1:5d1abf5d24a5 | 4 | #define ENABLED 1 |
| UBAL | 1:5d1abf5d24a5 | 5 | #define DISABLED 0 |
| UBAL | 1:5d1abf5d24a5 | 6 | |
| UBAL | 1:5d1abf5d24a5 | 7 | #define ENABLE_SERIAL_OUTPUT ENABLED |
| UBAL | 1:5d1abf5d24a5 | 8 | #define ENABLE_DISPLAY ENABLED |
| UBAL | 1:5d1abf5d24a5 | 9 | |
| UBAL | 1:5d1abf5d24a5 | 10 | #define DEBOUNCE_TIME_MS 50 |
| UBAL | 1:5d1abf5d24a5 | 11 | #define POTMETER_SAMPLES 10 |
| UBAL | 1:5d1abf5d24a5 | 12 | #define MIN_WELDING_TIME_MS 40 |
| UBAL | 1:5d1abf5d24a5 | 13 | #define WELDING_TIME_MS 3000 |
| UBAL | 1:5d1abf5d24a5 | 14 | #define POTMETER_UPDATE_TIME_MS 250 |
| UBAL | 1:5d1abf5d24a5 | 15 | |
| UBAL | 1:5d1abf5d24a5 | 16 | #if ENABLE_SERIAL_OUTPUT |
| UBAL | 1:5d1abf5d24a5 | 17 | Serial pc(USBTX, USBRX); |
| UBAL | 1:5d1abf5d24a5 | 18 | #endif |
| Magnus_tl | 0:9b09ea979c72 | 19 | |
| Magnus_tl | 0:9b09ea979c72 | 20 | DigitalOut myled(LED1); |
| UBAL | 1:5d1abf5d24a5 | 21 | DigitalIn pedal(PB_6); |
| UBAL | 1:5d1abf5d24a5 | 22 | AnalogIn pot(PA_0); |
| UBAL | 1:5d1abf5d24a5 | 23 | DigitalOut relay(PB_0); |
| UBAL | 1:5d1abf5d24a5 | 24 | |
| UBAL | 1:5d1abf5d24a5 | 25 | #if ENABLE_DISPLAY |
| UBAL | 1:5d1abf5d24a5 | 26 | TextLCD lcd(PB_1, PF_1, PA_8, PA_11, PB_5, PB_4); // rs, e, d4-d7 |
| UBAL | 1:5d1abf5d24a5 | 27 | DigitalOut rw(PF_0); |
| UBAL | 1:5d1abf5d24a5 | 28 | #endif |
| UBAL | 1:5d1abf5d24a5 | 29 | |
| UBAL | 1:5d1abf5d24a5 | 30 | bool bActive = false; |
| UBAL | 1:5d1abf5d24a5 | 31 | unsigned int runTime = 0; |
| UBAL | 1:5d1abf5d24a5 | 32 | Ticker potUpdateTicker; |
| UBAL | 1:5d1abf5d24a5 | 33 | |
| UBAL | 1:5d1abf5d24a5 | 34 | void update_time(void); |
| UBAL | 1:5d1abf5d24a5 | 35 | unsigned int get_time(void); |
| Magnus_tl | 0:9b09ea979c72 | 36 | |
| UBAL | 1:5d1abf5d24a5 | 37 | int main() |
| UBAL | 1:5d1abf5d24a5 | 38 | { |
| UBAL | 1:5d1abf5d24a5 | 39 | // Initialize pedal |
| UBAL | 1:5d1abf5d24a5 | 40 | pedal.mode(PullUp); |
| UBAL | 1:5d1abf5d24a5 | 41 | |
| UBAL | 1:5d1abf5d24a5 | 42 | #if ENABLE_SERIAL_OUTPUT |
| UBAL | 1:5d1abf5d24a5 | 43 | // Initialize Serial |
| UBAL | 1:5d1abf5d24a5 | 44 | pc.baud(115200); |
| UBAL | 1:5d1abf5d24a5 | 45 | pc.printf("Spot Welder 3000\r\n"); |
| UBAL | 1:5d1abf5d24a5 | 46 | #endif |
| UBAL | 1:5d1abf5d24a5 | 47 | |
| UBAL | 1:5d1abf5d24a5 | 48 | // Initialize time |
| UBAL | 1:5d1abf5d24a5 | 49 | get_time(); |
| UBAL | 1:5d1abf5d24a5 | 50 | |
| UBAL | 1:5d1abf5d24a5 | 51 | // Initialize relay output |
| UBAL | 1:5d1abf5d24a5 | 52 | relay = 1; |
| Magnus_tl | 0:9b09ea979c72 | 53 | |
| UBAL | 1:5d1abf5d24a5 | 54 | #if ENABLE_DISPLAY |
| UBAL | 1:5d1abf5d24a5 | 55 | // Initialize LCD |
| UBAL | 1:5d1abf5d24a5 | 56 | rw = 0; |
| UBAL | 1:5d1abf5d24a5 | 57 | lcd.cls(); |
| UBAL | 1:5d1abf5d24a5 | 58 | lcd.locate(0,0); |
| UBAL | 1:5d1abf5d24a5 | 59 | lcd.printf("Spot Welder 3000"); |
| UBAL | 1:5d1abf5d24a5 | 60 | lcd.locate(0,1); |
| UBAL | 1:5d1abf5d24a5 | 61 | lcd.printf("Time: %4d ms", runTime); |
| UBAL | 1:5d1abf5d24a5 | 62 | #endif |
| UBAL | 1:5d1abf5d24a5 | 63 | |
| UBAL | 1:5d1abf5d24a5 | 64 | // Start update time |
| UBAL | 1:5d1abf5d24a5 | 65 | potUpdateTicker.attach_us(&update_time, POTMETER_UPDATE_TIME_MS * 1000); |
| UBAL | 1:5d1abf5d24a5 | 66 | |
| UBAL | 1:5d1abf5d24a5 | 67 | while (1) |
| UBAL | 1:5d1abf5d24a5 | 68 | { |
| UBAL | 1:5d1abf5d24a5 | 69 | // Check for pedal press |
| UBAL | 1:5d1abf5d24a5 | 70 | if (pedal == 0) |
| Magnus_tl | 0:9b09ea979c72 | 71 | { |
| UBAL | 1:5d1abf5d24a5 | 72 | bActive = true; |
| UBAL | 1:5d1abf5d24a5 | 73 | |
| UBAL | 1:5d1abf5d24a5 | 74 | // Debounce |
| UBAL | 1:5d1abf5d24a5 | 75 | wait_ms(DEBOUNCE_TIME_MS); |
| UBAL | 1:5d1abf5d24a5 | 76 | if (pedal == 0) |
| UBAL | 1:5d1abf5d24a5 | 77 | { |
| UBAL | 1:5d1abf5d24a5 | 78 | #if ENABLE_SERIAL_OUTPUT |
| UBAL | 1:5d1abf5d24a5 | 79 | pc.printf("Welding for %d ms\r\n", runTime); |
| UBAL | 1:5d1abf5d24a5 | 80 | #endif |
| UBAL | 1:5d1abf5d24a5 | 81 | relay = 1; |
| UBAL | 1:5d1abf5d24a5 | 82 | myled = 1; // LED is ON |
| UBAL | 1:5d1abf5d24a5 | 83 | wait_ms(runTime); |
| UBAL | 1:5d1abf5d24a5 | 84 | } |
| Magnus_tl | 0:9b09ea979c72 | 85 | } |
| UBAL | 1:5d1abf5d24a5 | 86 | |
| Magnus_tl | 0:9b09ea979c72 | 87 | myled = 0; // LED is OFF |
| UBAL | 1:5d1abf5d24a5 | 88 | relay = 0; |
| UBAL | 1:5d1abf5d24a5 | 89 | bActive = false; |
| Magnus_tl | 0:9b09ea979c72 | 90 | |
| UBAL | 1:5d1abf5d24a5 | 91 | // One-shot |
| UBAL | 1:5d1abf5d24a5 | 92 | while (pedal == 0) |
| UBAL | 1:5d1abf5d24a5 | 93 | { |
| UBAL | 1:5d1abf5d24a5 | 94 | wait_us(1); |
| UBAL | 1:5d1abf5d24a5 | 95 | } |
| Magnus_tl | 0:9b09ea979c72 | 96 | } |
| Magnus_tl | 0:9b09ea979c72 | 97 | } |
| UBAL | 1:5d1abf5d24a5 | 98 | |
| UBAL | 1:5d1abf5d24a5 | 99 | void update_time(void) |
| UBAL | 1:5d1abf5d24a5 | 100 | { |
| UBAL | 1:5d1abf5d24a5 | 101 | if (not bActive) |
| UBAL | 1:5d1abf5d24a5 | 102 | { |
| UBAL | 1:5d1abf5d24a5 | 103 | get_time(); |
| UBAL | 1:5d1abf5d24a5 | 104 | #if ENABLE_SERIAL_OUTPUT |
| UBAL | 1:5d1abf5d24a5 | 105 | lcd.locate(6,1); |
| UBAL | 1:5d1abf5d24a5 | 106 | lcd.printf("%4d", runTime); |
| UBAL | 1:5d1abf5d24a5 | 107 | #endif |
| UBAL | 1:5d1abf5d24a5 | 108 | } |
| UBAL | 1:5d1abf5d24a5 | 109 | } |
| UBAL | 1:5d1abf5d24a5 | 110 | |
| UBAL | 1:5d1abf5d24a5 | 111 | unsigned int get_time(void) |
| UBAL | 1:5d1abf5d24a5 | 112 | { |
| UBAL | 1:5d1abf5d24a5 | 113 | float potSum = 0; |
| UBAL | 1:5d1abf5d24a5 | 114 | |
| UBAL | 1:5d1abf5d24a5 | 115 | for (uint8_t i = 0; i < POTMETER_SAMPLES; i++) |
| UBAL | 1:5d1abf5d24a5 | 116 | { |
| UBAL | 1:5d1abf5d24a5 | 117 | potSum += pot.read(); |
| UBAL | 1:5d1abf5d24a5 | 118 | } |
| UBAL | 1:5d1abf5d24a5 | 119 | |
| UBAL | 1:5d1abf5d24a5 | 120 | runTime = MIN_WELDING_TIME_MS + (unsigned int)((potSum / POTMETER_SAMPLES) * WELDING_TIME_MS); |
| UBAL | 1:5d1abf5d24a5 | 121 | return runTime; |
| UBAL | 1:5d1abf5d24a5 | 122 | } |
