---> I am trying to create a controller using Arduino Uno that simply detects pulses from a waveform generator mimicking a Laser Machine and then switches between 4 state machines states at the output of a Pre-amp, it is a built on of this previous post so the connections are the same except for the waveform generator settings. The new waveform generator settings are :
Type: Pulse, Freq:20Hz (50ms), Amp:5.0 Vpp, Offset:0V, Width = 100ns
---> I am trying to add 5 things in the project so any input is greatly appreciated:
1) Initialize all the 4 state machine states into an Array so user can define as many states as they want from the declarations.
2) ADD a digital de-bounce to the input pin, LASER_PIN.
3)(requires item #5 to be done first) ADD a reset to the system which moves the program back to STATE_00 from whatever it is doing.
4) Better code to wait until a pulse from Laser arrives before going to the state machine in void loop().
5) (item #2 depends on this) Completely Remove delay from the CountPulses() function and replace with millis() function to allow the use of reset button
---> Here is what i have so far, i started off by adding 4 state machine states and using the delay() function in the CountPulses() function just like in the previous post and it works fine and i am able to switch states while counting pulses, however, when i added the millis() to replace delay() function as shown below in the code, i only get 0s in pulse count and the state machine is stuck in STATE_00 so i used a couple of print statements to debug and it turns out currentmillis() - startmillis() is actually less than period = 1000ms so i am stuck on how to set this up and without this i cannot move forward, any help would be appreciated. My code and serial monitor results are shown below:
--> UPDATE: items #3 and #5 work perfectly fine , please see updated code. for item #4 i have not figured this one out yet. for item #2 ,there are a lot of libraries for debounce but not sure which libraries (Button, Bounce and etc) are most trusted by arduino community, any suggestions would help.
-->UPDATE 2: The code has been update with the following changes, external interrupts are used to reset the system if switch is LOW, Polling with interrupts is used to wait for pulses.
THE UPDATE TO QUESTION: items#3, 4 and 5 were solved and now only 2 items remain to be added to the project with modifications to item #1 and item #2 based on comments:
1) I require the state machine to do the following:
a) Declared in an array where user can easily change the order of execution of states. (eg: state_2--> state_1 -->state_3--> state_0).
b) User can easily decrease the number of states from MAX_VALUE = 4 to only 1 state. (eg with MAX_VALUE = 3: state_0--> state_1--> state_2) .
2) ADD a digital de-bounce to the input pin, RESET_PIN. Note: The debounce to LASER_PIN was a mistake in understanding based on the comments, so i have updated this.
Updated code with current serial monitor results is shown below:
//Objectives: Use input from laser to control pre-amp on adc. Multiplex the inputs on Pre-Amp
//Date:10/21/2019
//Type: Pulse, Freq:20Hz (50ms), Amp:5.0 Vpp, Offset:500mV, Width = 100ns
//-----------------------PROJECT LIBRARIES----------------------------------
#include <Bounce2.h>
#include <Arduino.h>
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
//-----------------------DEFINES------------------------------------------
//Declare Laser Input Pin
#define LASER_PIN 2
//Declare Reset Pin
#define RESET_PIN 3
//Define the states of the machine
typedef enum {STATE_00, STATE_01, STATE_10, STATE_11} fsm_state_type;
fsm_state_type fsm_state = STATE_00;
// -------------------------CONSTANTS (won't change)-------------------------------------
const unsigned long period = 1000; //the value is a number of milliseconds
//-------------------------VARIABLES (will change)-------------------------------------
bool only_for_print = false;//used only for print state ments
int reset_switch = 1;//Start HIGH to avoid reset
int PulseCount = 0; //Pulse count from X-RAY
int Output = 0;//Switch state on the Pre-Amp
int wait = 0;//wait for pulses count
int N = 20;//no. of pulses to count before switching states
volatile int IRQcount = 0;
volatile boolean reset_flag = false;
unsigned long start_time = 0;
unsigned long current_time = 0;
//----------------------------USER DEFINED FUNCTIONS---------------------------------
void fsm();
void loop();
void setup();
void WDT_RESET();
void IRQcounter();
void CountPulses();
//-----------------------------DEBOUNCE FUNCTIONS---------------------------------------
//--------------------------------MAIN SETUP--------------------------------------
void setup()
{
Serial.begin(115200);
//Pin Setup
pinMode(LASER_PIN, INPUT_PULLUP);
pinMode(RESET_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(LASER_PIN), IRQcounter, RISING);//attach interrupt handler to laser input
attachInterrupt (digitalPinToInterrupt (RESET_PIN), RESET_ISR, FALLING); // attach interrupt handler to reset, wait for user press button or switch
start_time = millis(); //initial start time
sei();//Turn on Interrupts
WaitForPulses();//Waits to detect 20 pulses
}
//--------------------------------MAIN LOOP----------------------------------
void loop()
{
current_time = millis();
fsm();//State machine
}
//--------------------------------PULSE COUNT FUNCTION--------------------------------------------
void CountPulses()
{
// current_time = millis();
if ((current_time - start_time) >= period)
{
start_time = current_time;
cli();//disable interrupts
PulseCount = IRQcount;
IRQcount = 0;
Serial.print(F("Pulse Count is = "));
Serial.println(PulseCount);
sei();//enable interrupts
}
}
//--------------------------------STATE MACHINE FUNCTION--------------------------------------------
void fsm()
{
switch (fsm_state)
{
case STATE_00:
/////////Print Statement only for debugging//////////
while (only_for_print == false)
{
Serial.println("The state is 00");
only_for_print = true;
}
///////// Count Pulses Setup /////////////////
current_time = millis();
CountPulses();
Output = 0;
if (PulseCount == N)
{
PulseCount = 0;//Reset Pulse Count
only_for_print = false; //used only for print statments
fsm_state = STATE_01;//switch to next state
}
break;
case STATE_01:
/////////Print Statement only for debugging//////////
while (only_for_print == false)
{
Serial.println("The state is 01");
only_for_print = true;
}
///////// Count Pulses Setup /////////////////
current_time = millis();
CountPulses();
Output = 2;
if (PulseCount == N)
{
PulseCount = 0;//Reset Pulse Count
only_for_print = false; //used only for print statments
fsm_state = STATE_10;//switch to next state
}
break;
case STATE_10:
/////////Print Statement only for debugging//////////
while (only_for_print == false)
{
Serial.println("The state is 10");
only_for_print = true;
}
///////// Count Pulses Setup /////////////////
current_time = millis();
CountPulses();
Output = 3;
if (PulseCount == N)
{
PulseCount = 0;//Reset Pulse Count
only_for_print = false; //used only for print statments
fsm_state = STATE_11;//switch to next state
}
break;
case STATE_11:
/////////Print Statement only for debugging//////////
while (only_for_print == false)
{
Serial.println("The state is 11");
only_for_print = true;
}
///////// Count Pulses Setup /////////////////
current_time = millis();
CountPulses();
Output = 4;
if (PulseCount == N)
{
PulseCount = 0;//Reset Pulse Count
only_for_print = false; //used only for print statments
fsm_state = STATE_00;//switch to next state
}
break;
default:
fsm_state = STATE_00;
break;
}
}
//----------------------------------RESET SWITCH ISR-------------------------------------
void RESET_ISR()
{
reset_flag = true;
if(reset_flag == true)
{
// Serial.println("System will now Reset");// Only for debugging
reset_flag = false;//Reset reset switch flag
wdt_enable(WDTO_500MS);//Reset after 0.5 seconds
while (1)
{
// wdt_reset(); // uncomment to avoid reboot
}
}
}
//-----------------------PULSE COUNT ISR---------------------------------------
void IRQcounter()
{
IRQcount++;
}
//-----------------------WAIT FOR PULSES---------------------------------------
void WaitForPulses()
{
while(wait<20)
{
if (bit_is_set(EIFR, INTF0))
{
Serial.println("Pulse is detected ");
wait++;
}
}
wait = 0;//reset
}
*/
[![edited image][3]][3]
The serial monitor is shown below with added reset switch on pin 3 using watch dog timer library .
Pulse is detected
Pulse is detected
Pulse is detected
Pulse is detected
Pulse is detected
Pulse is detected
Pulse is detected
Pulse is detected
Pulse is detected
Pulse is detected
Pulse is detected
Pulse is detected
Pulse is detected
Pulse is detected
Pulse is detected
Pulse is detected
Pulse is detected
Pulse is detected
Pulse is detected
Pulse is detected
The state is 00
Pulse Count is = 112
Pulse Count is = 20
The state is 01
Pulse Count is = 20
The state is 10
Pulse Count is = 20
The state is 11
Pulse Count is = 20
The state is 00
Pulse Count is = 20
The state is 01
Pulse Count is = 20
The state is 10
Pulse Count is = 20
The state is 11
Pulse Count is = 20
The state is 00
Pulse Count is = 20
The state is 01
Pulse Count is = 20
The state is 10
Pulse Count is = 20
The state is 11
Pulse Count is = 20
The state is 00
Pulse Count is = 20
The state is 01
Pulse Count is = 20
The state is 10
Pulse Count is = 20
The state is 11
Pulse Count is = 20