From 97dd28a88a2278f77fc0a9ac0d32315d9ba1265b Mon Sep 17 00:00:00 2001 From: Linux_Hat Date: Tue, 23 Apr 2024 15:38:22 +0200 Subject: [PATCH] Token detection --- jacopad/arduino/README.md | 1 + jacopad/arduino/main.cpp | 22 ++++++++++++++++++++++ jacopad/arduino/token.cpp | 34 ++++++++++++++++++++++++++++++++++ jacopad/arduino/token.h | 21 +++++++++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 jacopad/arduino/README.md create mode 100644 jacopad/arduino/main.cpp create mode 100644 jacopad/arduino/token.cpp create mode 100644 jacopad/arduino/token.h diff --git a/jacopad/arduino/README.md b/jacopad/arduino/README.md new file mode 100644 index 0000000..a49c909 --- /dev/null +++ b/jacopad/arduino/README.md @@ -0,0 +1 @@ +# JacoPad Arduino diff --git a/jacopad/arduino/main.cpp b/jacopad/arduino/main.cpp new file mode 100644 index 0000000..8830cfe --- /dev/null +++ b/jacopad/arduino/main.cpp @@ -0,0 +1,22 @@ +#include + +#include "token.h" + +// Define array of limits for token j1 +float limits1[3] = {1000.0, 4000.0, 7500.0}; + +// Create an instance of Token class named j1 with specified parameters +Token j1(1, 15000, limits1, 400, 10000); + +void setup() { + Serial.begin(9600); // Initialize serial communication at 9600 baud rate +} + +void loop() { + // Read the sensor value and print it along with the token type + Serial.print(j1.read()); // Print the adjusted sensor value + Serial.print(" "); // Print separator + Serial.println(j1.type()); // Print the type of token based on the sensor value + + delay(1000); // Delay for 1 second before next reading (adjust as needed) +} \ No newline at end of file diff --git a/jacopad/arduino/token.cpp b/jacopad/arduino/token.cpp new file mode 100644 index 0000000..1bc009f --- /dev/null +++ b/jacopad/arduino/token.cpp @@ -0,0 +1,34 @@ +#include + +#include "token.h" + +// Constructor for Token class +Token::Token(int pin, float base, float limits[3], float min, float max) { + m_pin = pin; // Initialize pin number + m_base = base; // Set base value + m_min = min; // Set minimum value + m_max = max; // Set maximum value + for(int i=0; i<3; i++) { + m_limits[i] = limits[i]; // Copy limit values to class member array + } +} + +// Read the analog value from the sensor and calculate the adjusted value +float Token::read() { + float valeurLue = (float)analogRead(m_pin); // Read analog value from specified pin + return (valeurLue * m_base) / (1023.0F - valeurLue); // Perform scaling and return adjusted value +} + +// Determine the type of token based on the read value +int Token::type() { + float value = Token::read(); // Read the token value + if(value < m_min || m_max < value) { + return 0; // Out of range, return type 0 + } + for(int i=0; i<3; i++) { + if(value < m_limits[i]) { + return i + 1; // Return type corresponding to the limit that value is less than + } + } + return 4; // Default type (in range but not matching any specific limit) +} \ No newline at end of file diff --git a/jacopad/arduino/token.h b/jacopad/arduino/token.h new file mode 100644 index 0000000..3ca611d --- /dev/null +++ b/jacopad/arduino/token.h @@ -0,0 +1,21 @@ +#pragma once + +// Token class declaration +class Token { +private: + int m_pin; // Pin number connected to the sensor + float m_base; // Base value used for scaling the sensor reading + float m_limits[3]; // Array of limit values for token type determination + float m_min; // Minimum value threshold for token range + float m_max; // Maximum value threshold for token range + +public: + // Constructor declaration + Token(int pin, float base, float limits[3], float min, float max); + + // Method to read sensor value and return adjusted value + float read(); + + // Method to determine and return the type of token based on the sensor value + int type(); +}; \ No newline at end of file