State Machines

Created by Miroslav Biňas / mirek

![Night Light Illustration](../images/night.security.light.gif) [Night Security Light](https://randomnerdtutorials.com/build-a-night-security-light-with-arduino/)
![Night Light Schematic](../images/night.light-schematic.png)
## The Problem of Arduino Sketch
## State Machine
> **State Machine** is a **behavior model**, which consists of a **finite number of states**. Based on the current state and a given input the machine performs **state transitions** and produces outputs. ([itemis.com](https://www.itemis.com/en/yakindu/state-machine/documentation/user-guide/overview_what_are_state_machines))
## State Diagram
![State Diagram: Turnstile](../images/state.diagram-turnstile.jpg) notes: * https://www.smashingmagazine.com/2018/01/rise-state-machines/
## Programming State Machines * Object Oriented Programming - State Design Pattern * Procedural Programming - as functions with `switch` statement
## State Day ![Night Security Light: State Day](../images/night.light-state.day.png)
```cpp void state_day(){ // on enter // main loop // on leave } ```
![Houston, we have a problem](../images/houston.we.have.a.problem.jpg)
## Serial Communication
![Serial Communication: Wiring](../images/serial.communication-wiring.png) wiring with two cables only notes: * https://learn.sparkfun.com/tutorials/serial-communication/all
![Serial Communication: data](../images/serial.communication.png) transfering of 8 bits
## Serial Setup ```cpp Serial.begin(9600); ```
## State Night ![Night Security Light: State Night](../images/night.light-state.night.png)
![LDR: Sensor](../images/ldr.sensor.jpg) LDR (Light Dependent Resistor) notes: * https://www.amazon.com/eBoot-Photoresistor-Sensitive-Resistor-Dependent/dp/B01N7V536K
![LDR: Characteristics](../images/ldr-characteristic.jpg) notes: * https://www.instructables.com/Car-Auto-Light-System/
![LDR: Function](../images/ldr-function.png) notes: * https://forum.arduino.cc/t/how-to-reverse-the-function-of-an-ldr-sensor/52491/7
![Analog Signal](../images/analog.signal.png) Notes: * https://www.circuitbread.com/ee-faq/analog-versus-digital-signal-processing
![Digital Signal](../images/digital.signal.png) Notes: * https://www.circuitbread.com/ee-faq/analog-versus-digital-signal-processing
## Analog to Digital Converter (A/D Converter)
## Steps of ADC: 1. sampling 2. quantizing 3. encoding
![ADC: Sampling](../images/adc-sampling.png) notes: * https://commons.wikimedia.org/wiki/File:Digital.signal.discret.svg
![ADC: Quantizing](../images/adc-quantizing.jpg) notes: * https://de.wikipedia.org/wiki/Datei:Digital.signal.svg
![ADC: Resolution](../images/adc-resolution.png) Notes: * https://www.arrow.com/en/research-and-events/articles/engineering-resource-basics-of-analog-to-digital-converters
![Arduino Uno](../images/arduino-board.png)
$$567 \times \frac{5}{1024} = 567 \times 4.88^{-3} = 2.77V$$
```cpp int analogRead(int pin); ```
```cpp int map( value, fromLow, fromHigh, toLow, toHigh ); ```
## Code Refactoring
```cpp enum state { DAY, NIGHT, LIGHT }; ```
## Questions?