Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define STANDBY_PHASE -1
- #define ERROR_PHASE 0
- #define CALCULATION_PHASE 1
- #define NEGATIVE -1
- #define POSITIVE 1
- #define DEFAULT_VALUE 500
- #define DIFF_SENSITIVITY 0.1
- int phase = STANDBY_PHASE;
- int value = 0;
- int value_max = 0;
- int value_min = 1000;
- int value_diff = 0;
- int phase_ticks = 0;
- int previous_value_diff = 0;
- boolean sensitivity_check() {
- int upper_check = previous_value_diff * (1 + DIFF_SENSITIVITY);
- int lower_check = previous_value_diff * (1 - DIFF_SENSITIVITY);
- if (value_diff >= lower_check && value_diff <= upper_check) return 1;
- else return 0;
- }
- boolean presenceCheck(){
- }
- void readCommand() {
- if (Serial.available() > 0) {
- char tempChar = Serial.read();
- switch (tempChar) {
- case 'A': {
- phase = STANDBY_PHASE;
- resetAll();
- }
- case 'B' : {
- phase = ERROR_PHASE;
- }
- }
- }
- }
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(4800);
- }
- void loop() {
- readCommand();
- value = analogRead(A2);
- if (value > value_max) {
- value_max = value;
- } else if (value < value_min) {
- value_min = value;
- }
- previous_value_diff = value_diff;
- value_diff = value_max - value_min;
- switch (phase) {
- case ERROR_PHASE: {
- debug_Print();
- if (value_diff > 60 || value_max == 1000 || value_min == 0) {
- reset_min();
- reset_max();
- } else {
- if (sensitivity_check()) {
- Serial.println("CHECK");
- phase_ticks++;
- }
- if (phase_ticks == 20) {
- Serial.println("DONE");
- phase = CALCULATION_PHASE;
- }
- }
- }
- case CALCULATION_PHASE: {
- int temp_diff = value -
- }
- }
- }
- void reset_min() {
- value_min = DEFAULT_VALUE;
- }
- void reset_max() {
- value_max = DEFAULT_VALUE;
- }
- void reset_value() {
- value = 0;
- }
- void reset_value_diff() {
- value_diff = 0;
- }
- void reset_previous_value_diff() {
- previous_value_diff = 0;
- }
- void reset_phase_ticks() {
- phase_ticks = 0;
- }
- void resetAll() {
- reset_value();
- reset_min();
- reset_max();
- reset_value_diff();
- reset_previous_value_diff();
- reset_phase_ticks();
- }
- void debug_Print() {
- Serial.print("value: ");
- Serial.print(value);
- Serial.print(" value_max: ");
- Serial.print(value_max);
- Serial.print(" value_min: ");
- Serial.print(value_min);
- Serial.print(" value_diff: ");
- Serial.println(value_diff);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement