13 Commits

Author SHA1 Message Date
aab02c8066 Added demagnetization pulses
Added some demag pulses to demag the HV transformer whenever the single pulse is armed.
2021-11-08 21:07:10 -06:00
dd293435e6 Disabled amplitude and period changes when in single fire mode 2021-11-08 20:53:18 -06:00
ee57b7e14c Removed continuous fire and slow fire modes
Removed these modes because they're no longer needed for the project
2021-11-08 20:20:23 -06:00
b32f768220 Added Isolate pin and fixed is_armed not disabling
Added an extra pin that's basically just a copy of the armed state indicator pin. This pin will control a relay to enable/disable some high voltage electronics. The is_armed flag and its corresponding indicator pin now get disabled when the single_pulse_enable_pin is off
2021-11-08 20:18:51 -06:00
4e7eba68ce Fixed period bounds and period resolution math
Fixed the bug where period bounds weren't being updated according to the sample_resolution parameter. Also artificially limited the period resolution to 1ms
2021-11-03 22:21:01 -05:00
4341f022b6 Changed some default period settings
Limited the maximum period to 20ms and set the default start up period to 10ms.
2021-11-03 21:46:37 -05:00
c9480fb278 Inverted Indicator Pin Logic
Indicator pins are now off when output is HIGH and on when output is LOW
2021-11-03 21:40:53 -05:00
0235bd3859 Fixed bug where is_armed could be true at startup 2021-11-03 21:22:01 -05:00
2dd56f6315 Updated the single pulse mode
Enabling single pulse mode now changes the status screen on the LCD display. Single pulse mode also no longer uses interrupts, and instead uses HEAVILY debounced inputs for enable, arm, and fire.
2021-11-03 21:15:15 -05:00
92f4db12ce Fixed debounce timer mistake
I realized I has been subtracting the wrong way which cause the debounce to not work. I've fixed this now. I also added a debounce to the arming pin.
2021-10-30 12:46:15 -05:00
1d274a6a2c Updated Single Pulse Mode
Updated single pulse mode so that it has an ENABLED, ARMED, and FIRED state. The enabled pin toggles the single fire mode. The armed state toggles whether it's armed or not. The fire pin will only fire if it has been armed.
2021-10-30 12:39:27 -05:00
0f3a19f653 Enabled LCD Output
Added support for an I2C LCD to be used with the project. The LCD can be driven directly by the data lines
2021-10-28 20:49:49 -05:00
f6453e4ab4 Got rid of waveform select feature
Waveform select feature is no longer needed since we've settled on a given waveform.
2021-10-28 18:37:00 -05:00
3 changed files with 222 additions and 94 deletions

Binary file not shown.

View File

@@ -15,4 +15,4 @@ framework = arduino
monitor_speed = 115200
lib_deps =
paulstoffregen/Encoder@^1.4.1
C:\Users\Quinn\.platformio\lib\LiquidCrystal_I2C
marcoschwartz/LiquidCrystal_I2C@^1.1.4

View File

@@ -10,66 +10,73 @@
* D23 - Amplitude Encoder Input
* D24 - Period Encoder Input
* D25 - Period Encoder Input
* D26 - Waveform Select Encoder
* D27 - Waveform Select Encoder
* D28 - Single pulse flag (LOW enables single pulse mode)
* D26 - Nothing
* D27 - Single pulse armed pin. toggles the armed state when it recieves a falling signal
* D28 - Single pulse enable pin (A FALLING signal toggles single pulse mode)
* D29 - Single pulse trigger. (LOW will cause the arduino to send 1 waveform pulse)
* D30 - Slow pulse flag (LOW enables a pulse rate of 3 pulses/second)
*
* D31 - Armed state indicator light output
* D32 - Fired state indicator light output
* D33 - Operates a relay which isolates the high voltage side of electronics from the electrodes
*/
// Define pinouts
#define waveform_pin DAC0
#define amplitude_pin DAC1
#define sync_pin 2
#define single_pulse_arm_pin 27
#define single_pulse_enable_pin 28
#define single_pulse_trig_pin 29
#define armed_indicator_pin 31
#define fired_indicator_pin 32
#define isolate_pin 33
//Include necessary libraries
#include <Arduino.h>
#include <Encoder.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//uncomment to enable serial output for debugging
#define enable_serial_debug
//#define enable_serial_debug
//uncomment to enable lcd output
#define enable_lcd
/* Changes the number of array points to sample. If 1, it will sample every point.
* If 2, it will sample every other point,. If four it will sample every 4 points, etc.
*/
#define sample_resolution 4
#define waveform_pin DAC0
#define amplitude_pin DAC1
#define sync_pin 2
#define single_pulse_flag_pin 28
#define single_pulse_trig_pin 29
#define slow_pulse_flag_pin 30
// Number of waveforms available. Change this if you add or remove waveforms
#define waveform_num 4
// Array of waveform arrays. Currently holds four waveforms with 600 samples each.
int waveforms[waveform_num][600] = {
{2083,2075,2067,2059,2071,2086,2100,2115,2129,2135,2141,2147,2153,2176,2176,2176,2176,2188,2201,2215,2228,2242,2255,2269,2282,2294,2305,2340,2352,2363,2376,2389,2401,2414,2427,2440,2453,2466,2478,2491,2504,2522,2539,2557,2574,2586,2597,2609,2621,2633,2644,2656,2668,2679,2691,2703,2714,2726,2738,2749,2761,2777,2792,2808,2820,2831,2843,2855,2867,2878,2890,2902,2960,2972,2984,2996,3007,3019,3030,3042,3053,3065,3078,3092,3105,3118,3131,3145,3158,3171,3181,3192,3202,3212,3223,3233,3243,3254,3264,3288,3294,3299,3314,3329,3343,3358,3364,3370,3393,3405,3417,3428,3440,3440,3452,3463,3463,3463,3463,3472,3481,3489,3498,3498,3498,3498,3498,3498,3510,3522,3522,3522,3522,3522,3522,3522,3522,3522,3522,3522,3522,3522,3522,3522,3522,3522,3522,3522,3522,3516,3510,3510,3498,3493,3487,3475,3471,3467,3463,3451,3440,3428,3416,3408,3401,3358,3346,3323,3288,3276,3253,3229,3218,3195,3171,3148,3124,3083,3042,3025,3007,2978,2948,2931,2913,2872,2831,2750,2668,2645,2621,2586,2551,2480,2445,2328,2270,2247,2223,2184,2145,1989,1954,1931,1872,1866,1860,1849,1837,1802,1785,1767,1747,1728,1708,1673,1650,1626,1609,1591,1568,1533,1521,1474,1462,1451,1439,1381,1346,1310,1264,1229,1205,1176,1147,1135,1112,1088,1077,1065,1048,1030,983,966,948,928,909,889,866,849,831,819,808,796,784,761,749,714,702,691,679,667,655,650,644,626,608,597,591,585,574,562,550,539,503,492,480,468,456,445,433,433,433,433,433,421,421,418,416,413,410,410,410,410,407,404,401,398,398,398,398,398,398,398,398,398,398,398,404,410,415,421,425,429,433,439,445,451,456,458,461,463,466,468,476,483,491,496,501,505,510,515,523,530,538,544,550,556,562,574,585,587,590,592,595,597,609,620,623,626,629,632,638,644,644,655,661,667,673,679,687,694,702,720,737,755,772,796,819,843,866,892,919,945,971,1030,1053,1123,1147,1178,1209,1240,1310,1345,1381,1416,1445,1474,1509,1544,1580,1603,1619,1634,1650,1685,1720,1767,1872,1884,1895,1907,1942,1954,1966,2012,2030,2048,2129,2188,2258,2293,2352,2434,2463,2492,2609,2656,2726,2734,2742,2750,2820,2837,2854,2871,2888,2904,2921,2938,2955,2972,2996,3019,3048,3077,3083,3089,3101,3147,3159,3171,3183,3218,3230,3241,3288,3311,3319,3327,3335,3381,3399,3416,3440,3452,3463,3463,3463,3475,3475,3475,3487,3491,3494,3498,3498,3498,3498,3498,3498,3498,3498,3502,3506,3510,3510,3510,3510,3510,3510,3510,3510,3510,3510,3510,3510,3510,3510,3487,3487,3485,3482,3480,3477,3475,3463,3459,3456,3452,3446,3440,3432,3424,3416,3411,3405,3399,3393,3370,3346,3342,3339,3335,3323,3311,3299,3299,3299,3264,3259,3253,3247,3241,3235,3229,3218,3206,3200,3194,3188,3182,3177,3171,3163,3155,3147,3136,3112,3101,3089,3074,3060,3045,3030,2995,2990,2984,2972,2960,2948,2925,2919,2913,2890,2885,2881,2876,2872,2867,2861,2855,2831,2820,2808,2802,2796,2787,2779,2770,2761,2745,2730,2691,2679,2656,2650,2597,2574,2551,2528,2399,2393,2387,2375,2363,2352,2340,2328,2305,2282,2278,2274,2270,2258,2247,2235,2223,2212,2200,
},
{1637,1637,1637,4095,4086,4078,4069,4061,4052,4044,4035,4027,4018,4010,4001,3993,3985,3977,3968,3960,3952,3944,3936,3928,3920,3912,3903,3896,3888,3880,3872,3864,3856,3848,3840,3833,3825,3817,3810,3802,3794,3787,3779,3772,3764,3757,3749,3742,3734,3727,3720,3712,3705,3698,3690,3683,3676,3669,3662,3655,3647,3640,3633,3626,3619,3612,3605,3598,3591,3585,3578,3571,3564,3557,3551,3544,3537,3530,3524,3517,3510,3504,3497,3491,3484,3478,3471,3465,3458,3452,3446,3439,3433,3427,3420,3414,3408,3401,3395,3389,3383,3377,3371,3365,3358,3352,3346,3340,3334,3328,3322,3316,3311,3305,3299,3293,3287,3281,3276,3270,3264,3258,3253,3247,3241,3236,3230,3224,3219,3213,3208,3202,3197,3191,3186,3180,3175,3169,3164,3159,3153,3148,3143,3137,3132,3127,3121,3116,3111,3106,3101,3096,3090,3085,3080,3075,3070,3065,3060,3055,3050,3045,3040,3035,3030,3025,3020,3016,3011,3006,3001,2996,2991,2987,2982,2977,2972,2968,2963,2958,2954,2949,2945,2940,2935,2931,2926,2922,2917,2913,2908,2904,2899,2895,2890,2886,2882,2877,2873,2868,2864,2860,2855,2851,2847,2843,2838,2834,2830,2826,2822,2817,2813,2809,2805,2801,2797,2793,2789,2785,2781,2777,2773,2769,2765,2761,2757,2753,2749,2745,2741,2737,2733,2729,2725,2722,2718,2714,2710,2706,2703,2699,2695,2691,2688,2684,2680,2677,2673,2669,2666,2662,2659,2655,2651,2648,2644,2641,2637,2634,2630,2627,2623,2620,2616,2613,2609,2606,2603,2599,2596,2592,2589,2586,2582,2579,2576,2572,2569,2566,2563,2559,2556,2553,2550,2546,2543,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,1637,0,6,12,17,23,29,34,40,46,51,57,62,68,73,79,84,90,95,101,106,112,117,122,128,133,138,143,149,154,159,164,170,175,180,185,190,195,200,205,210,215,220,225,230,235,240,245,250,255,260,265,270,274,279,284,289,294,298,303,308,312,317,322,326,331,336,340,345,349,354,358,363,367,372,376,381,385,389,394,398,403,407,411,416,420,424,428,433,437,441,445,450,454,458,462,466,470,474,479,483,487,491,495,499,503,507,511,515,519,523,527,531,534,538,542,546,550,554,558,561,565,569,573,576,580,584,588,591,595,599,602,606,610,613,617,620,624,628,631,635,638,642,645,649,652,656,659,663,666,669,673,676,680,683,686,690,693,696,700,703,706,709,713,716,719,722,726,729,732,735,738,742,745,748,751,754,757,760,764,767,770,773,776,779,782,785,788,791,794,797,800,803,806,809,811,814,817,820,823,826,829,832,834,837,840,843,846,848,851,854,857,860,862,865,868,870,873,876,879,881,884,887,889,892,894,897,900,902,905,907,910,913,915,918,920,923,925,928,930,933,935,938,940,943,945,947,950,952,955,957,960,962,964,967,969,971,974,976,978,981,983,985,988,990,992,994,997,999,1001,1003,1006,1008,1010,1012,1015,1017,1019,1021,1023,1025,1028,1030,1032,1034,1637,
},
{2048,4093,4091,4088,4085,4083,4080,4077,4075,4072,4069,4066,4064,4061,4058,4055,4052,4050,4047,4044,4041,4038,4036,4033,4030,4027,4024,4021,4018,4015,4012,4009,4007,4004,4001,3998,3995,3992,3989,3986,3983,3980,3977,3973,3970,3967,3964,3961,3958,3955,3952,3949,3946,3942,3939,3936,3933,3930,3926,3923,3920,3917,3913,3910,3907,3904,3900,3897,3894,3890,3887,3884,3880,3877,3874,3870,3867,3863,3860,3856,3853,3850,3846,3843,3839,3836,3832,3829,3825,3821,3818,3814,3811,3807,3803,3800,3796,3792,3789,3785,3781,3778,3774,3770,3767,3763,3759,3755,3751,3748,3744,3740,3736,3732,3728,3725,3721,3717,3713,3709,3705,3701,3697,3693,3689,3685,3681,3677,3673,3669,3665,3661,3657,3652,3648,3644,3640,3636,3632,3627,3623,3619,3615,3610,3606,3602,3598,3593,3589,3585,3580,3576,3571,3567,3563,3558,3554,3549,3545,3540,3536,3531,3527,3522,3518,3513,3508,3504,3499,3494,3490,3485,3480,3476,3471,3466,3462,3457,3452,3447,3442,3437,3433,3428,3423,3418,3413,3408,3403,3398,3393,3388,3383,3378,3373,3368,3363,3358,3353,3348,3342,3337,3332,3327,3322,3316,3311,3306,3301,3295,3290,3285,3279,3274,3268,3263,3258,3252,3247,3241,3236,3230,3225,3219,3213,3208,3202,3197,3191,3185,3180,3174,3168,3162,3157,3151,3145,3139,3133,3127,3122,3116,3110,3104,3098,3092,3086,3080,3074,3068,3062,3055,3049,3043,3037,3031,3025,3018,3012,3006,3000,2993,2987,2981,2974,2968,2961,2955,2948,2942,2935,2929,2922,2916,2909,2903,2896,2889,2883,2876,2869,2862,2856,2849,2842,2835,2828,2821,2815,2808,2801,2794,2787,2780,2773,2766,2758,2751,2744,2737,2048,0,3,5,8,11,13,16,19,22,24,27,30,33,35,38,41,44,47,49,52,55,58,61,64,66,69,72,75,78,81,84,87,90,93,96,99,102,105,108,111,114,117,120,123,126,129,132,135,138,142,145,148,151,154,157,161,164,167,170,173,177,180,183,186,190,193,196,200,203,206,210,213,216,220,223,227,230,233,237,240,244,247,251,254,258,261,265,268,272,276,279,283,286,290,294,297,301,305,308,312,316,319,323,327,331,334,338,342,346,350,353,357,361,365,369,373,377,381,384,388,392,396,400,404,408,412,416,420,425,429,433,437,441,445,449,453,458,462,466,470,474,479,483,487,491,496,500,504,509,513,518,522,526,531,535,540,544,549,553,558,562,567,571,576,580,585,590,594,599,604,608,613,618,622,627,632,637,641,646,651,656,661,666,670,675,680,685,690,695,700,705,710,715,720,725,730,735,741,746,751,756,761,766,772,777,782,787,793,798,803,809,814,819,825,830,836,841,847,852,858,863,869,874,880,885,891,897,902,908,914,919,925,931,937,943,948,954,960,966,972,978,984,990,996,1002,1008,1014,1020,1026,1032,1038,1044,1050,1056,1063,1069,1075,1081,1087,1094,1100,1106,1113,1119,1126,1132,1138,1145,1151,1158,1164,1171,1178,1184,1191,1197,1204,1211,1217,1224,1231,1238,1244,1251,1258,1265,1272,1279,1286,1293,1300,1307,1314,1321,1328,1335,1342,1349,2048,
},
{1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,2875,4095,4079,4062,4046,4030,4014,3999,3983,3967,3952,3937,3921,3906,3891,3876,3861,3846,3832,3817,3803,3788,3774,3760,3746,3732,3718,3704,3690,3677,3663,3650,3636,3623,3610,3597,3584,3571,3558,3545,3532,3520,3507,3495,3483,3470,3458,3446,3434,3422,3410,3399,3387,3375,3364,3352,3341,3329,3318,3307,3296,3285,3274,3263,3252,3242,3231,3220,3210,3199,3189,3179,3169,3158,3148,3138,3128,3118,3109,3099,3089,3079,3070,3060,3051,3041,3032,3023,3014,3005,2995,2986,2977,2969,2960,2951,2942,2934,2925,2916,2908,2899,2891,2883,2874,2866,2858,2850,2842,2834,2826,2818,2810,2802,2795,2787,2779,2772,2764,2757,2749,2742,2735,2727,2720,2713,2706,2699,2692,2685,2678,2671,2664,2657,2650,2644,2637,2630,2624,2617,2611,2604,2598,2591,2585,2579,2573,2566,2560,2554,2093,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,814,0,11,22,32,43,54,64,75,85,95,106,116,126,136,146,156,166,176,185,195,204,214,223,233,242,251,261,270,279,288,297,306,315,323,332,341,350,358,367,375,383,392,400,408,416,425,433,441,449,456,464,472,480,488,495,503,510,518,525,533,540,547,555,562,569,576,583,590,597,604,611,618,624,631,638,645,651,658,664,671,677,683,690,696,702,709,715,721,727,733,739,745,751,757,763,769,774,780,786,791,797,803,808,814,819,825,830,835,841,846,851,856,862,867,872,877,882,887,892,897,902,907,912,917,921,926,931,936,940,945,949,954,959,963,968,972,976,981,985,990,994,998,1002,1007,1011,1015,1019,1023,1027,1335,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,1638,
}
int default_waveform[600] = {2048,4093,4091,4088,4085,4083,4080,4077,4075,4072,4069,4066,4064,4061,4058,4055,4052,4050,4047,4044,4041,4038,4036,4033,4030,4027,4024,4021,4018,4015,4012,4009,4007,4004,4001,3998,3995,3992,3989,3986,3983,3980,3977,3973,3970,3967,3964,3961,3958,3955,3952,3949,3946,3942,3939,3936,3933,3930,3926,3923,3920,3917,3913,3910,3907,3904,3900,3897,3894,3890,3887,3884,3880,3877,3874,3870,3867,3863,3860,3856,3853,3850,3846,3843,3839,3836,3832,3829,3825,3821,3818,3814,3811,3807,3803,3800,3796,3792,3789,3785,3781,3778,3774,3770,3767,3763,3759,3755,3751,3748,3744,3740,3736,3732,3728,3725,3721,3717,3713,3709,3705,3701,3697,3693,3689,3685,3681,3677,3673,3669,3665,3661,3657,3652,3648,3644,3640,3636,3632,3627,3623,3619,3615,3610,3606,3602,3598,3593,3589,3585,3580,3576,3571,3567,3563,3558,3554,3549,3545,3540,3536,3531,3527,3522,3518,3513,3508,3504,3499,3494,3490,3485,3480,3476,3471,3466,3462,3457,3452,3447,3442,3437,3433,3428,3423,3418,3413,3408,3403,3398,3393,3388,3383,3378,3373,3368,3363,3358,3353,3348,3342,3337,3332,3327,3322,3316,3311,3306,3301,3295,3290,3285,3279,3274,3268,3263,3258,3252,3247,3241,3236,3230,3225,3219,3213,3208,3202,3197,3191,3185,3180,3174,3168,3162,3157,3151,3145,3139,3133,3127,3122,3116,3110,3104,3098,3092,3086,3080,3074,3068,3062,3055,3049,3043,3037,3031,3025,3018,3012,3006,3000,2993,2987,2981,2974,2968,2961,2955,2948,2942,2935,2929,2922,2916,2909,2903,2896,2889,2883,2876,2869,2862,2856,2849,2842,2835,2828,2821,2815,2808,2801,2794,2787,2780,2773,2766,2758,2751,2744,2737,2048,0,3,5,8,11,13,16,19,22,24,27,30,33,35,38,41,44,47,49,52,55,58,61,64,66,69,72,75,78,81,84,87,90,93,96,99,102,105,108,111,114,117,120,123,126,129,132,135,138,142,145,148,151,154,157,161,164,167,170,173,177,180,183,186,190,193,196,200,203,206,210,213,216,220,223,227,230,233,237,240,244,247,251,254,258,261,265,268,272,276,279,283,286,290,294,297,301,305,308,312,316,319,323,327,331,334,338,342,346,350,353,357,361,365,369,373,377,381,384,388,392,396,400,404,408,412,416,420,425,429,433,437,441,445,449,453,458,462,466,470,474,479,483,487,491,496,500,504,509,513,518,522,526,531,535,540,544,549,553,558,562,567,571,576,580,585,590,594,599,604,608,613,618,622,627,632,637,641,646,651,656,661,666,670,675,680,685,690,695,700,705,710,715,720,725,730,735,741,746,751,756,761,766,772,777,782,787,793,798,803,809,814,819,825,830,836,841,847,852,858,863,869,874,880,885,891,897,902,908,914,919,925,931,937,943,948,954,960,966,972,978,984,990,996,1002,1008,1014,1020,1026,1032,1038,1044,1050,1056,1063,1069,1075,1081,1087,1094,1100,1106,1113,1119,1126,1132,1138,1145,1151,1158,1164,1171,1178,1184,1191,1197,1204,1211,1217,1224,1231,1238,1244,1251,1258,1265,1272,1279,1286,1293,1300,1307,1314,1321,1328,1335,1342,1349,2048
};
// A dynamic waveform that can change its values based on some scalar. This gets initialized at the start of the program
int dynamic_waveform[600];
//Waveform values
float waveform_vals[] = {1,2,3,4,5,6,7,8,9,10};
//Setup encoders and their corresponding variables
Encoder amp_encoder(22, 23);
long new_amp = 10;
long old_amp = 10;
Encoder period_encoder(24, 25);
long new_period = 4;
long old_period = 4;
Encoder waveform_encoder(26, 27);
long new_waveform = 2;
long old_waveform = 2;
long new_period = 10*sample_resolution;
long old_period = 10*sample_resolution;
//Create LCD object
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
//Used to debounce the trigger input
boolean has_single_pulsed = false;
unsigned long deb_timer = 0;
//Setup values for arming and firing system
boolean single_fire_is_enabled = true; // is true when single fire mode is enabled
boolean single_fire_is_disabled = false; // is false when single fire mode is disabled. Used to keep track of state changes
boolean is_armed = false; // is true when single fire mode is armed
unsigned long fire_debounce = 0; // A timer to debounce the firing pin
unsigned long armed_debounce = 0; //A time to deboucne the arming pin
/* Cycle through the waveform samples over a given period of time
@@ -103,40 +110,87 @@ void recalc_waveform(float percent_amplitude){
// Scale the array and give it its new offset
for(int i = 0; i < 600; i++){
dynamic_waveform[i] = (int)(waveforms[new_waveform][i]*amp_scalar + offset);
dynamic_waveform[i] = (int)(default_waveform[i]*amp_scalar + offset);
}
}
//Changes the LCD screen to the continuous firing status screen
void continuous_output_lcd(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Period:");
lcd.print((float)(new_period)/sample_resolution);
lcd.setCursor(0, 1);
lcd.print("Amplitude:");
lcd.print(waveform_vals[new_amp-1]);
}
//Change the LCD screen to the single fire status screen
void single_fire_lcd(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Single Fire Mode");
lcd.setCursor(0,1);
lcd.print("Armed: ");
if(is_armed){
lcd.print("Yes");
}
else lcd.print("No");
}
void setup() {
//The sync pin can be run into an oscilliscope's trig channel to easiy find the waveform
pinMode(sync_pin, OUTPUT);
pinMode(single_pulse_flag_pin, INPUT_PULLUP);
pinMode(single_pulse_trig_pin, INPUT_PULLUP);
pinMode(slow_pulse_flag_pin, INPUT_PULLUP);
//Change the resolution of the analog ourput to its maximum (12 bit res)
analogWriteResolution(12);
//Immediately set the analog output to the mid point
analogWrite(waveform_pin, 2048);
//The sync pin can be run into an oscilliscope's trig channel to easiy find the waveform
pinMode(sync_pin, OUTPUT);
// Setup the rest of the IO pins
pinMode(single_pulse_enable_pin, INPUT_PULLUP);
pinMode(single_pulse_arm_pin, INPUT_PULLUP);
pinMode(single_pulse_trig_pin, INPUT_PULLUP);
pinMode(armed_indicator_pin, OUTPUT);
pinMode(fired_indicator_pin, OUTPUT);
pinMode(isolate_pin, OUTPUT);
// Set the indicator pins off (Inverted logic)
digitalWrite(armed_indicator_pin, HIGH);
digitalWrite(fired_indicator_pin, HIGH);
// Disconnect the electrodes
digitalWrite(isolate_pin, HIGH);
// Initialize timers and encoders
amp_encoder.write(new_amp*4);
period_encoder.write(new_period*4);
waveform_encoder.write(new_waveform*4);
period_encoder.write(new_period*4/sample_resolution);
// Check single pulse status
single_fire_is_enabled = !digitalRead(single_pulse_enable_pin);
single_fire_is_disabled = !single_fire_is_enabled;
// Only executes this block of code if serial debug is enabled
#ifdef enable_serial_debug
Serial.begin(115200);
Serial.print("Ampltidue: ");
Serial.println(new_amp*10);
Serial.println(waveform_vals[new_amp-1]);
Serial.print("Period: ");
Serial.println(new_period);
Serial.print("Waveform #: ");
Serial.println(new_waveform);
Serial.println(new_period/sample_resolution);
#endif
// Loads currently selected waveform into the dynamic waveform
for(int i = 0; i < 600; i++) dynamic_waveform[i] = waveforms[new_waveform][i];
for(int i = 0; i < 600; i++) dynamic_waveform[i] = default_waveform[i];
//Init LCD
#ifdef enable_lcd
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
if(single_fire_is_enabled){
single_fire_lcd();
}
else{
continuous_output_lcd();
}
#endif
}
@@ -145,14 +199,20 @@ void loop() {
// Read in encoder values
new_amp = amp_encoder.read()/4;
new_period = period_encoder.read()/4;
new_waveform = waveform_encoder.read()/4;
new_period = new_period*sample_resolution;
// Check to see if any of the encoder values have changed. If they have, update their values.
//Handles ampltidue encoder
if(new_amp != old_amp){
// This if statement stops the period from being changes if single fire mdoe is enabled
if(single_fire_is_enabled == true){
new_amp = old_amp;
amp_encoder.write(new_amp*4);
}
// Make sure the value is within a valid range
if(new_amp > 10){
//128 ampltidue steps should be enough granularity
//10 ampltidue steps should be enough granularity
new_amp = 10;
amp_encoder.write(new_amp*4);
}
@@ -163,74 +223,142 @@ void loop() {
old_amp = new_amp;
//If serial is enabled, output some serial data
#ifdef enable_serial_debug
Serial.print("Ampltidue (%): ");
Serial.println(new_amp*10);
Serial.print("Ampltidue: ");
Serial.println(waveform_vals[new_amp-1]);
#endif
// If LCD output is enabled output to LCD
#ifdef enable_lcd
if(single_fire_is_enabled == false){
lcd.setCursor(10,1);
lcd.print(waveform_vals[new_amp-1]);
}
#endif
recalc_waveform(new_amp);
}
//Handles period encoder
if(new_period != old_period){
// This if statement stops the period from being changes if single fire mdoe is enabled
if(single_fire_is_enabled == true){
new_period = old_period;
period_encoder.write(new_period);
}
else{
// Make sure the value is within a valid range
if(new_period > 100){
new_period = 100;
period_encoder.write(new_period*4);
if(new_period > 20*sample_resolution){
new_period = 20*sample_resolution;
period_encoder.write(20*4);
}
//Anything less than three will cause errors
if(new_period < 4){
new_period = 4;
period_encoder.write(new_period*4);
if(new_period < sample_resolution){
new_period = sample_resolution;
period_encoder.write(sample_resolution);
}
old_period = new_period;
}
//If serial is enabled, output some serial data
#ifdef enable_serial_debug
Serial.print("Period (ms): ");
Serial.println((float)(new_period)/sample_resolution);
#endif
// If LCD output is enabled output to LCD
#ifdef enable_lcd
if(single_fire_is_enabled == false){
lcd.setCursor(7,0);
lcd.print((float)(new_period)/sample_resolution);
}
#endif
}
//Handles waveform encoder
if(new_waveform != old_waveform){
// Make sure the value is within a valid range
if(new_waveform > waveform_num-1){
new_waveform = waveform_num-1;
waveform_encoder.write(new_waveform*4);
}
if(new_waveform < 0){
new_waveform = 0;
waveform_encoder.write(new_waveform*4);
}
old_waveform = new_waveform;
// If serial is enabled, output some serial data
#ifdef enable_serial_debug
Serial.print("Waveform #: ");
Serial.println(new_waveform);
#endif
recalc_waveform(1);
}
// Creates a synch signal so an oscilliscope can more easily read irregular pulses
digitalWrite(sync_pin, !digitalRead(sync_pin));
// Check to see if the single pulse pin has been pulled LOW
if(!digitalRead(single_pulse_flag_pin)){
if(deb_timer-millis() > 100){
if(!digitalRead(single_pulse_trig_pin) && has_single_pulsed == false){
generate_waveform(1);
has_single_pulsed = true;
deb_timer = millis();
}
else if(digitalRead(single_pulse_trig_pin)) {
has_single_pulsed = false;
deb_timer = millis();
//Reads the state of the single pulse enable pin
single_fire_is_enabled = !digitalRead(single_pulse_enable_pin);
//Check to see if the single fire state has changed
#ifdef enable_lcd
if(single_fire_is_enabled == single_fire_is_disabled){
if(single_fire_is_enabled){
single_fire_lcd();
}
else{
continuous_output_lcd();
is_armed = false;
digitalWrite(armed_indicator_pin, HIGH);
digitalWrite(isolate_pin, HIGH);
}
}
else{
generate_waveform(4);
// Slows down the output pulse if the slow pulse pin is pulled LOW
if(!digitalRead(slow_pulse_flag_pin)) delay(500);
#endif
single_fire_is_disabled = !single_fire_is_enabled;
// Check to see if the single pulse pin has been pulled LOW
if(single_fire_is_enabled){
//Heavy-duty debounce routine for arming pin
if(!digitalRead(single_pulse_arm_pin)){
// Reset the debounce timer
if(millis()-armed_debounce > 3000) armed_debounce = millis();
//Once the debounce timer reaches 250ms toggle the armed state.
if(millis()-armed_debounce > 200 && millis()-armed_debounce < 300){
// Toggle the armed state
is_armed = !is_armed;
// Output the armed state to the lcd
lcd.setCursor(7,1);
if(is_armed){
lcd.print("Yes");
// send a bunch of pulses to demagnetize the HV transformer before arming
for(int i = 0; i < 10; i++){
generate_waveform(sample_resolution);
}
}
else lcd.print("No ");
//Output the armed state to the arm pin and isolate pin
digitalWrite(armed_indicator_pin, !is_armed);
digitalWrite(isolate_pin, !is_armed);
//Set the timer so this if statement doesn't run again
armed_debounce = millis()-301;
}
}
// Heavy duty debounce routine for firing pin
if(!digitalRead(single_pulse_trig_pin) && is_armed){
// Reset the debounce timer
if(millis()-fire_debounce > 2500) fire_debounce = millis();
//Once the debounce timer reaches 250ms toggle the armed state.
if(millis()-fire_debounce > 200 && millis()-fire_debounce < 300){
digitalWrite(fired_indicator_pin, LOW);
//Send out pulse
generate_waveform(sample_resolution);
// Toggle the armed state
is_armed = false;
// Output the armed state to the lcd
lcd.setCursor(7,1);
lcd.print("No ");
//Output the state to the indicator lights and turn off the isolate relay
digitalWrite(armed_indicator_pin, HIGH);
digitalWrite(isolate_pin, HIGH);
//Set the timer so this if statement doesn't run again
fire_debounce = millis()-301;
armed_debounce = millis();
}
}
// Turn off the fire indicator pin after a half of a second
if(millis()-fire_debounce>300+500){
digitalWrite(fired_indicator_pin, HIGH);
}
}
}