0

---> 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
0

1 Answer 1

0

Important note:

After reading (again) your other question and its answers: You are stressing the input of your Arduino with negative voltages! Please set the offset of the signal to 2.5V (the middle between GND and VCC) and the amplitude to 5Vpp. The signal must neither be lower than GND nor higher than VCC! The input might withstand this for a while but given enough time and power it will get damaged.

Analysis of your problem:

1. The measurement of the period

Let's take a look at a "condensed" version of your time code:

void setup()
{
  startMillis = millis();
}

void loop()
{
  currentMillis = millis();
  if (currentMillis - startMillis <= period)
  {
    startMillis = currentMillis;
  }
}

I'm sure you see it now, too! Because right at the first call of loop() the time passed since setup() is short, the condition is true and startMillis is updated to the current time. This is repeated ever and ever again. The condition will always evaluate as true.

2. The number of pulses

In the function CountPulses() the counter is reset at the beginning. Then after some undefined very short duration (because your if-condition is always true) it is read. Chances are low for a pulse to be counted in that time span.

Additional note:

Just one more tip. You can define and use your own data types to show your intentions. For example the states look so much better like shown below, and you'll get free error messages from code analyzers.

typedef enum {
    STATE_00, STATE_01, STATE_10, STATE_11,
} fsm_state_type;
fsm_state_type fsm_state = STATE_00;

EDIT after you added new informations:

Item #2

Why do you think you need to debounce the laser pulse? Is it generated by a mechanical switch? Only those need debouncing. If the pulses are generated from some electronic circuit they should be clean.

Item #4

You can reset the IRQcount and then wait for it to count up.

But as this involves disabling and enabling the interrupt and reading an int just for 1 event, I'd rather set a flag if the interrupt occurs. Then the FSM could wait for the flag to be set.

Another alternative is to poll the input pin directly and to wait for the leading edge. But for this the pulse has to be wide enough.

As @EdgarBonet pointed out it will be even simpler to poll the interrupt flag. But IIRC the interrupt has to be disabled for this, else the call of the interrupt service routine will clear the flag with no chance for the main program to see it.

A better alternative is to poll the interrupt flag (i.e. if (bit_is_set(EIFR, INTF0)) { ... }). Then you don't need a wide pulse: once the hardware detects the rising edge, the flag is set and remains set until you explicitly clear it (EIFR = _BV(INTF0);).

8
  • here is where i am having the difficulty from transition from delay() to millis() : when adding the delay() function, the program is able to count the number of pulses, now removing it and replacing with millis() function, the program is too fast to count the pulses (always shows 0 pules) . I have tried lowering the baud rate but and changing the settings for millis() function with different periods, but it doesnt help.
    – sk95
    Oct 27, 2019 at 18:11
  • Well, step away from the source code and make a design. Don't program by accident, think and plan. You know that you need some certain period as the source shows. Now use it. What would you do as a human to tackle the task? millis() can be compared to a look at the wall clock. Oct 27, 2019 at 19:25
  • Re “Another alternative is to poll the input pin [...]. But for this the pulse has to be wide enough.”: A better alternative is to poll the interrupt flag (i.e. if (bit_is_set(EIFR, INTF0)) { ... }). Then you don't need a wide pulse: once the hardware detects the rising edge, the flag is set and remains set until you explicitly clear it (EIFR = _BV(INTF0);). Oct 29, 2019 at 21:20
  • @EdgarBonet Thanks for reminding me to this. I took the liberty to quote you. Oct 30, 2019 at 6:58
  • Re “IIRC the interrupt has to be disabled for this [...]”: You do recall correctly. ;-) Oct 30, 2019 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.