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.
This commit is contained in:
Binary file not shown.
170
src/main.cpp
170
src/main.cpp
@@ -67,33 +67,17 @@ Encoder period_encoder(24, 25);
|
|||||||
long new_period = 4;
|
long new_period = 4;
|
||||||
long old_period = 4;
|
long old_period = 4;
|
||||||
|
|
||||||
|
|
||||||
//Setup values for arming and firing system
|
|
||||||
volatile boolean is_enabled = false;
|
|
||||||
volatile boolean is_armed = false;
|
|
||||||
boolean has_single_pulsed = false;
|
|
||||||
unsigned long debounce_timer = 0;
|
|
||||||
volatile unsigned long armed_debounce = 0;
|
|
||||||
|
|
||||||
// Toggles the armed state of single fire mode. (Only toggles is signle fire mdoe is already enabled.)
|
|
||||||
void arm_single_fire(){
|
|
||||||
if(is_enabled && millis()-armed_debounce > 100){
|
|
||||||
is_armed = !is_armed;
|
|
||||||
// Toggle the armed indicator light
|
|
||||||
digitalWrite(armed_indicator_pin, !digitalRead(armed_indicator_pin));
|
|
||||||
digitalWrite(fired_indicator_pin, LOW);
|
|
||||||
armed_debounce = millis();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Toggles single fire mode
|
|
||||||
void enable_single_fire(){
|
|
||||||
is_enabled = !is_enabled;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Create LCD object
|
//Create LCD object
|
||||||
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
|
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
|
||||||
|
|
||||||
|
//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
|
/* Cycle through the waveform samples over a given period of time
|
||||||
* The time scalar argument changes how many samples it will skip.
|
* The time scalar argument changes how many samples it will skip.
|
||||||
* A value of 1 will not skip any samples, a value of 2 will use every 2nd sammple, and a value of 5 will use every 5th sample.
|
* A value of 1 will not skip any samples, a value of 2 will use every 2nd sammple, and a value of 5 will use every 5th sample.
|
||||||
@@ -129,6 +113,29 @@ void recalc_waveform(float percent_amplitude){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//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() {
|
void setup() {
|
||||||
//Immediately set the analog output to the mid point
|
//Immediately set the analog output to the mid point
|
||||||
@@ -145,9 +152,7 @@ void setup() {
|
|||||||
|
|
||||||
//Set up interrupts to simplify single fire mode:
|
//Set up interrupts to simplify single fire mode:
|
||||||
// This will call the arm_single_fire() function whenever it detects a falling signal on this pin
|
// This will call the arm_single_fire() function whenever it detects a falling signal on this pin
|
||||||
attachInterrupt(digitalPinToInterrupt(single_pulse_arm_pin), arm_single_fire, FALLING);
|
//attachInterrupt(digitalPinToInterrupt(single_pulse_arm_pin), arm_single_fire, FALLING);
|
||||||
// Will call the enable_single_fire function whenever it detects a falling signal on this pin
|
|
||||||
attachInterrupt(digitalPinToInterrupt(single_pulse_enable_pin), enable_single_fire, FALLING);
|
|
||||||
|
|
||||||
//Change the resolution of the analog ourput to its maximum (12 bit res)
|
//Change the resolution of the analog ourput to its maximum (12 bit res)
|
||||||
analogWriteResolution(12);
|
analogWriteResolution(12);
|
||||||
@@ -156,6 +161,9 @@ void setup() {
|
|||||||
amp_encoder.write(new_amp*4);
|
amp_encoder.write(new_amp*4);
|
||||||
period_encoder.write(new_period*4);
|
period_encoder.write(new_period*4);
|
||||||
|
|
||||||
|
// Check single pulse status
|
||||||
|
is_armed = !digitalRead(single_pulse_enable_pin);
|
||||||
|
|
||||||
// Only executes this block of code if serial debug is enabled
|
// Only executes this block of code if serial debug is enabled
|
||||||
#ifdef enable_serial_debug
|
#ifdef enable_serial_debug
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
@@ -172,12 +180,13 @@ void setup() {
|
|||||||
#ifdef enable_lcd
|
#ifdef enable_lcd
|
||||||
lcd.init();
|
lcd.init();
|
||||||
lcd.backlight();
|
lcd.backlight();
|
||||||
lcd.setCursor(0, 0);
|
lcd.setCursor(0,0);
|
||||||
lcd.print("Period:");
|
if(single_fire_is_enabled){
|
||||||
lcd.print((float)(new_period)/sample_resolution);
|
single_fire_lcd();
|
||||||
lcd.setCursor(0, 1);
|
}
|
||||||
lcd.print("Amplitude:");
|
else{
|
||||||
lcd.print(waveform_vals[new_amp-1]);
|
continuous_output_lcd();
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,8 +219,10 @@ void loop() {
|
|||||||
|
|
||||||
// If LCD output is enabled output to LCD
|
// If LCD output is enabled output to LCD
|
||||||
#ifdef enable_lcd
|
#ifdef enable_lcd
|
||||||
lcd.setCursor(10,1);
|
if(single_fire_is_enabled == false){
|
||||||
lcd.print(waveform_vals[new_amp-1]);
|
lcd.setCursor(10,1);
|
||||||
|
lcd.print(waveform_vals[new_amp-1]);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
recalc_waveform(new_amp);
|
recalc_waveform(new_amp);
|
||||||
@@ -238,8 +249,10 @@ void loop() {
|
|||||||
|
|
||||||
// If LCD output is enabled output to LCD
|
// If LCD output is enabled output to LCD
|
||||||
#ifdef enable_lcd
|
#ifdef enable_lcd
|
||||||
lcd.setCursor(7,0);
|
if(single_fire_is_enabled == false){
|
||||||
lcd.print((float)(new_period)/sample_resolution);
|
lcd.setCursor(7,0);
|
||||||
|
lcd.print((float)(new_period)/sample_resolution);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,27 +260,80 @@ void loop() {
|
|||||||
// Creates a synch signal so an oscilliscope can more easily read irregular pulses
|
// Creates a synch signal so an oscilliscope can more easily read irregular pulses
|
||||||
digitalWrite(sync_pin, !digitalRead(sync_pin));
|
digitalWrite(sync_pin, !digitalRead(sync_pin));
|
||||||
|
|
||||||
|
//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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
single_fire_is_disabled = !single_fire_is_enabled;
|
||||||
|
|
||||||
// Check to see if the single pulse pin has been pulled LOW
|
// Check to see if the single pulse pin has been pulled LOW
|
||||||
if(is_enabled){
|
if(single_fire_is_enabled){
|
||||||
if(is_armed && millis() - debounce_timer > 100){
|
|
||||||
if(!digitalRead(single_pulse_trig_pin) && has_single_pulsed == false){
|
//Heavy-duty debounce routine for arming pin
|
||||||
generate_waveform(sample_resolution);
|
if(!digitalRead(single_pulse_arm_pin)){
|
||||||
has_single_pulsed = true;
|
// Reset the debounce timer
|
||||||
debounce_timer = millis();
|
if(millis()-armed_debounce > 3000) armed_debounce = millis();
|
||||||
digitalWrite(fired_indicator_pin, HIGH);
|
|
||||||
digitalWrite(armed_indicator_pin, LOW);
|
//Once the debounce timer reaches 250ms toggle the armed state.
|
||||||
is_armed = false;
|
if(millis()-armed_debounce > 200 && millis()-armed_debounce < 300){
|
||||||
}
|
// Toggle the armed state
|
||||||
else if(digitalRead(single_pulse_trig_pin)) {
|
is_armed = !is_armed;
|
||||||
has_single_pulsed = false;
|
// Output the armed state to the lcd
|
||||||
debounce_timer = millis();
|
lcd.setCursor(7,1);
|
||||||
|
if(is_armed){
|
||||||
|
lcd.print("Yes");
|
||||||
|
}
|
||||||
|
else lcd.print("No ");
|
||||||
|
//Output the armed state to the arm pin
|
||||||
|
digitalWrite(armed_indicator_pin, is_armed);
|
||||||
|
//Set the timer so this if statement doesn't run again
|
||||||
|
armed_debounce = millis()-301;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(millis()-debounce_timer>250){
|
|
||||||
digitalWrite(fired_indicator_pin, LOW);
|
// 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, HIGH);
|
||||||
|
//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
|
||||||
|
digitalWrite(armed_indicator_pin, LOW);
|
||||||
|
//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, LOW);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
else{
|
else{
|
||||||
generate_waveform(sample_resolution);
|
generate_waveform(sample_resolution);
|
||||||
// Slows down the output pulse if the slow pulse pin is pulled LOW
|
// Slows down the output pulse if the slow pulse pin is pulled LOW
|
||||||
|
|||||||
Reference in New Issue
Block a user