Magnus Larsson / Mbed 2 deprecated SpotWelderController

Dependencies:   mbed TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TextLCD.h"
00003 
00004 #define ENABLED                     1
00005 #define DISABLED                    0
00006 
00007 #define ENABLE_SERIAL_OUTPUT        ENABLED
00008 #define ENABLE_DISPLAY              ENABLED
00009 
00010 #define DEBOUNCE_TIME_MS            50
00011 #define POTMETER_SAMPLES            10
00012 #define MIN_WELDING_TIME_MS         40
00013 #define WELDING_TIME_MS             3000
00014 #define POTMETER_UPDATE_TIME_MS     250
00015 
00016 #if ENABLE_SERIAL_OUTPUT
00017 Serial pc(USBTX, USBRX);
00018 #endif
00019 
00020 DigitalOut myled(LED1);
00021 DigitalIn pedal(PB_6);
00022 AnalogIn pot(PA_0);
00023 DigitalOut relay(PB_0);
00024 
00025 #if ENABLE_DISPLAY
00026 TextLCD lcd(PB_1, PF_1, PA_8, PA_11, PB_5, PB_4); // rs, e, d4-d7
00027 DigitalOut rw(PF_0);
00028 #endif
00029 
00030 bool bActive = false;
00031 unsigned int runTime = 0;
00032 Ticker potUpdateTicker;
00033 
00034 void update_time(void);
00035 unsigned int get_time(void);
00036 
00037 int main()
00038 {    
00039     // Initialize pedal
00040     pedal.mode(PullUp);
00041     
00042 #if ENABLE_SERIAL_OUTPUT
00043     // Initialize Serial
00044     pc.baud(115200);
00045     pc.printf("Spot Welder 3000\r\n");
00046 #endif
00047     
00048     // Initialize time
00049     get_time();
00050     
00051     // Initialize relay output
00052     relay = 1;
00053     
00054 #if ENABLE_DISPLAY
00055     // Initialize LCD
00056     rw = 0;
00057     lcd.cls();
00058     lcd.locate(0,0);
00059     lcd.printf("Spot Welder 3000");    
00060     lcd.locate(0,1);
00061     lcd.printf("Time: %4d ms", runTime);
00062 #endif
00063 
00064     // Start update time
00065     potUpdateTicker.attach_us(&update_time, POTMETER_UPDATE_TIME_MS * 1000);
00066     
00067     while (1)
00068     {
00069         // Check for pedal press
00070         if (pedal == 0)
00071         {
00072             bActive = true;
00073             
00074             // Debounce
00075             wait_ms(DEBOUNCE_TIME_MS);
00076             if (pedal == 0)
00077             {
00078 #if ENABLE_SERIAL_OUTPUT
00079                 pc.printf("Welding for %d ms\r\n", runTime);
00080 #endif
00081                 relay = 1;
00082                 myled = 1; // LED is ON   
00083                 wait_ms(runTime);
00084             }
00085         }
00086         
00087         myled = 0; // LED is OFF
00088         relay = 0;
00089         bActive = false;
00090         
00091         // One-shot
00092         while (pedal == 0)
00093         {
00094             wait_us(1);
00095         }
00096     }
00097 }
00098 
00099 void update_time(void)
00100 {
00101     if (not bActive)
00102     {
00103         get_time();
00104 #if ENABLE_SERIAL_OUTPUT
00105         lcd.locate(6,1);
00106         lcd.printf("%4d", runTime);
00107 #endif
00108     }
00109 }
00110 
00111 unsigned int get_time(void)
00112 {
00113     float potSum = 0;
00114     
00115     for (uint8_t i = 0; i < POTMETER_SAMPLES; i++)
00116     {
00117         potSum += pot.read();
00118     }
00119     
00120     runTime = MIN_WELDING_TIME_MS + (unsigned int)((potSum / POTMETER_SAMPLES) * WELDING_TIME_MS);
00121     return runTime;
00122 }