1
1
forked from MDL29/JacoBot

Detection of the token

This commit is contained in:
Linux_Hat 2024-04-23 19:25:07 +02:00
parent 2020fed424
commit 51545da322
3 changed files with 77 additions and 0 deletions

22
jacopad/arduino/main.cpp Normal file
View File

@ -0,0 +1,22 @@
#include <Arduino.h>
#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)
}

34
jacopad/arduino/token.cpp Normal file
View File

@ -0,0 +1,34 @@
#include <Arduino.h>
#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)
}

21
jacopad/arduino/token.h Normal file
View File

@ -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();
};