gas_sensor

gas sensor project
git clone _git@git.tontus.eu/gas_sensor.git
Log | Files | Refs

commit 17041b973906f56465d5a990b5c1cf21252c55f1
parent db79b469fa9d8896c9d9e6edd314a2ac2a09eb62
Author: Tonton <tonterface@proton.me>
Date:   Sun, 28 Jun 2026 20:04:53 +0200

implement read function for sensor
add liveview for sensor monitoring

Diffstat:
Mgas_sensor.ino | 3+++
Minclude/sensor.h | 5+++--
Msrc/display.cpp | 1+
Msrc/menu.cpp | 30+++++++++++++++++++++++++++---
Msrc/sensor.cpp | 31+++++++++++++++++++++++++++++--
5 files changed, 63 insertions(+), 7 deletions(-)

diff --git a/gas_sensor.ino b/gas_sensor.ino @@ -4,6 +4,7 @@ #include "include/menu.h" #include "include/display.h" +#include "include/sensor.h" /* function declarations */ @@ -24,9 +25,11 @@ void setup(void) pinMode(BUILTIN_LED, OUTPUT); dsp_init(); + sensor_init(); } void loop(void) { menu_main(); + delay(100); } diff --git a/include/sensor.h b/include/sensor.h @@ -1,7 +1,8 @@ #ifndef SENSOR_H #define SENSOR_H -int sensor_read(void); -int sensor_analyze(); +void sensor_init(void); +int sensor_read(int); +int sensor_analyze(void); #endif // !MENU_H diff --git a/src/display.cpp b/src/display.cpp @@ -28,6 +28,7 @@ void dsp_print_result(int res) { display.clearDisplay(); dsp_write("RESULT:", 0, 0); + display.setCursor(0, SCREEN_ROW); display.print(res); display.display(); } diff --git a/src/menu.cpp b/src/menu.cpp @@ -1,4 +1,6 @@ -/* main menu library */ +/* menu system + * + */ #include <Arduino.h> @@ -14,6 +16,7 @@ static void select_menu_item(int input); static void menu_item1(void); static void menu_item2(void); static void menu_item3(void); +static void print_live(void); /* function implementations */ static int get_user_input(void) @@ -21,7 +24,7 @@ static int get_user_input(void) /* implement handling of physical buttons */ int input; - while (Serial.available() <= 0) { delay(10); } + while (Serial.available() <= 0) { delay(100); } /* always read to not clog buffer */ if (Serial.available() > 0) @@ -79,7 +82,7 @@ static void menu_item1(void) Serial.print("\e[1;1H\e[2J"); dsp_print_menu1(); Serial.print("Wait for analysis\n\r"); - res = sensor_analyze(); + res = sensor_read(0); dsp_print_result(res); goto_main(); } @@ -89,6 +92,7 @@ static void menu_item2(void) Serial.print("\e[1;1H\e[2J"); dsp_print_menu2(); Serial.println("You chose item 2."); + print_live(); goto_main(); } @@ -100,6 +104,26 @@ static void menu_item3(void) goto_main(); } +static void print_live(void) +{ + int rx, in; + + while (Serial.available() <= 0) { + rx = sensor_read(0); + dsp_print_result(rx); + Serial.print("\e[1;1H\e[2J"); + Serial.print("Sensor: "); + Serial.print(rx); + delay(500); + } + + /* don't clog buffer */ + if (Serial.available() > 0) + in = Serial.parseInt(); + + Serial.print("\n\r"); +} + void menu_main(void) { int input; diff --git a/src/sensor.cpp b/src/sensor.cpp @@ -4,10 +4,29 @@ #include "../include/sensor.h" +/* enums */ +enum { + SENSOR_1 = 26, + SENSOR_2 = 25, + SENSOR_3 = 34, + SENSOR_4 = 39 +}; + +/* variables */ +const int sens_arr[] = { + SENSOR_1, + SENSOR_2, + SENSOR_3, + SENSOR_4 +}; + /* function definitions */ -int sensor_read() +int sensor_read(int sensor) { - return 10; + int rx; + + rx = analogRead(sens_arr[sensor]); + return rx; } int sensor_analyze() @@ -15,3 +34,11 @@ int sensor_analyze() delay(1000); return 1; } + +void sensor_init(void) +{ + pinMode(SENSOR_1, INPUT); + pinMode(SENSOR_2, INPUT); + pinMode(SENSOR_3, INPUT); + pinMode(SENSOR_4, INPUT); +}