2

I'm not sure how to proceed on a project when it comes to the code design.

Think of a device which has to monitor a voltage value with an analog/digital converter and sometimes has to regulate it's pwm output if the measured values leave certain limits. The device also has an interface, e.g. LEDs and buttons and a potentiometer.

If the user changes the potentiometer value, the device will still have to monitor the voltage while the device is waiting for user input. And another concern is that the device will have to act fast (shutting down the driver for a supply within 10ms). Would you recommend a state machine or rather procedural programming?

How hard would it be to build up a state machine in C? Would it be fast enough when the Controller (STM32F0) has to read 4 ADC inputs and generate 2 PWMs?

  • 1
    If you're considering a state machine design then consider the [QP Active Object Framework](http://state-machine.com/). It has a learning curve but it's well worth it, in my opinion. – kkrambo May 16 '15 at 16:20

1 Answers1

2

State machines are best for tiny applications that won't need to scale up. If this is going into a device that serves one purpose, state machines are ok. When it comes to feature creep, state machines will quickly become a mess.

The processor you are using has more than adequate horsepower to handle this task. What will likely work best is a timer and interrupt driven design. Read the ADCs from a timer interrupt. You should be able to easily read the ADC at 1000Hz or faster.

And use fixed point or integers instead of floating point.

James
  • 6,978
  • 2
  • 17
  • 18
  • 5
    I disagree that a state machine is necessarily difficult to scale. State machines are a way to break down complexity into more modular chunks. With the right state machine framework, scaling up should become easier - not harder. – kkrambo May 16 '15 at 16:17