Hi everyone, I bought a hall sensor, the ss49 and then I bought some arduinos for the micro, I put them together and used them elsewhere, just the hall sensor connected to the arduino and it worked exactly like an axis, the way it was supposed to, but these days I tried to do it again, with another arduino and another hall sensor but both came with the purchase I had mentioned and i am using the same code, but for some reason it can read much further than the first hall sensor and it doesn't work like an axis, but like a switch, the code is not mine and I'm just using the accelerator at the moment, thank you in advance for your help, here is the code
#include <EEPROM.h>
#include <Joystick.h>
#include <HX711.h>
#include <SmartButton.h>
#include "data.h"
// Accelerator on X, Brake on Y, Clutch on Z
// X, Y, Z
// rX,rY,rZ
// rudder, throttle
// accelerator, brake, steering
Joystick_ joyInput(
JOYSTICK_DEFAULT_REPORT_ID,
JOYSTICK_TYPE_JOYSTICK,
ENABLE_GHOST_BUTTONS, ENABLE_GHOST_HATS,
(ENABLE_ACCELERATOR ? true : ENABLE_GHOST_ACCELERATOR),
(ENABLE_BRAKE ? true : ENABLE_GHOST_BRAKE),
(ENABLE_CLUTCH ? true : ENABLE_GHOST_CLUTCH),
false, false, false,
false, false,
false, false, false);
struct PedalData pedals;
HX711 brakeSensor;
using namespace smartbutton;
SmartButton buttonCal(CALIBRATE_PIN, SmartButton::InputType::NORMAL_HIGH);
const bool debugMode = false;
const bool debugSpam = false;
long debugTimer = 0;
const uint8_t debugInterval = 250; //ms
long usbTimer = 0;
const uint8_t usbInterval = 2; //ms
long calibrateTimer = 0;
const uint16_t calibrateInterval = 10000; // ms
#define LED_BLINK_COUNT 5
bool ledFlag = false;
uint8_t ledBlinkCounter = 0;
long ledTimer = 0;
uint16_t ledInterval = 2000; //ms
void setup()
{
// Serial
Serial.begin(115200);
// Joystick
joyInput.begin(false);
// Button
pinMode(CALIBRATE_PIN, INPUT_PULLUP);
buttonCal.begin(eventCallback);
// LED
pinMode(CAL_LED_PIN, OUTPUT);
// Check/get eeprom
getEEPROM();
// Brake HX711 setup
if (ENABLE_BRAKE){
pedals.brake.hx711 = &brakeSensor;
pedals.brake.hx711->begin(HX711DT_PIN, HX711SCK_PIN);
}
pinMode(ACCELERATOR_PIN, INPUT);
pinMode(CLUTCH_PIN, INPUT);
// Inverts from defines
pedals.accelerator.invert = INVERT_ACCELERATOR;
pedals.brake.invert = INVERT_BRAKE;
pedals.clutch.invert = INVERT_CLUTCH;
// Set up the joystick ranges after eeprom load
joyRangeSettings();
}
void loop()
{
ledAction();
SmartButton::service();
getAxes();
if (debugSpam){
if ((debugTimer + debugInterval) < millis()) {
debugReport();
}
}
if (pedals.calibrateFlag) {
processCalibrate();
}
if (pedals.eepromChangeFlag) {
putEEPROM();
}
if ((usbTimer + usbInterval) < millis()) {
joyInput.sendState();
}
}
void joyRangeSettings()
{
joyInput.setXAxisRange(AXIS_MIN, AXIS_MAX);
joyInput.setYAxisRange(AXIS_MIN, AXIS_MAX);
joyInput.setZAxisRange(AXIS_MIN, AXIS_MAX);
}
void ledAction()
{
if (ledFlag) {
if ((ledTimer + ledInterval) < millis()) {
if (ledBlinkCounter < LED_BLINK_COUNT){
bool onoff = (ledBlinkCounter % 2);
digitalWrite(CAL_LED_PIN, !onoff);
ledBlinkCounter++;
ledTimer = millis();
} else {
ledFlag = false;
ledBlinkCounter = 0;
digitalWrite(CAL_LED_PIN, false);
}
}
}
}
void getAxes()
{
if (!pedals.calibrateFlag) {
int16_t outval = 0;
if (ENABLE_ACCELERATOR) {
pedals.accelerator.value = analogRead(ACCELERATOR_PIN);
pedals.accelerator.value = clampu(pedals.accelerator.value, pedals.accelerator.min, pedals.accelerator.max);
if (pedals.accelerator.invert) {
outval = map(pedals.accelerator.value, pedals.accelerator.max, pedals.accelerator.min, AXIS_MIN, AXIS_MAX);
} else {
outval = map(pedals.accelerator.value, pedals.accelerator.min, pedals.accelerator.max, AXIS_MIN, AXIS_MAX);
}
joyInput.setXAxis(outval);
}
if (ENABLE_BRAKE) {
pedals.brake.value = readHX711pedal(pedals.brake.hx711);
pedals.brake.value = clampu(pedals.brake.value, pedals.brake.min, pedals.brake.max);
if (pedals.brake.invert) {
outval = map(pedals.brake.value, pedals.brake.max, pedals.brake.min, AXIS_MIN, AXIS_MAX);
} else {
outval = map(pedals.brake.value, pedals.brake.min, pedals.brake.max, AXIS_MIN, AXIS_MAX);
}
joyInput.setYAxis(outval);
}
if (ENABLE_CLUTCH) {
pedals.clutch.value = analogRead(CLUTCH_PIN);
pedals.clutch.value = clampu(pedals.clutch.value, pedals.clutch.min, pedals.clutch.max);
if (pedals.clutch.invert) {
outval = map(pedals.clutch.value, pedals.clutch.max, pedals.clutch.min, AXIS_MIN, AXIS_MAX);
} else {
outval = map(pedals.clutch.value, pedals.clutch.min, pedals.clutch.max, AXIS_MIN, AXIS_MAX);
}
joyInput.setZAxis(outval);
}
} else {
pedals.accelerator.value = analogRead(ACCELERATOR_PIN);
pedals.brake.value = readHX711pedal(pedals.brake.hx711);
pedals.clutch.value = analogRead(CLUTCH_PIN);
}
}
uint16_t clampu(uint16_t val, uint16_t min, uint16_t max)
{
uint16_t out = 0;
if (val > max) {out = max;}
else if (val < min) {out = min;}
else {out = val;}
return out;
}
int16_t readHX711pedal(HX711* sensor_in)
{
float loadcellValue = sensor_in->read();
return (int)(loadcellValue / HX711SCALAR);
}
void eventCallback(SmartButton *button, SmartButton::Event event, int clickCounter)
{
if (event == SmartButton::Event::CLICK) {
// Calibration Mode
startCalibrate();
} else if (event == SmartButton::Event::LONG_HOLD) {
// Reset EEPROM
resetEEPROM();
}
}
void startCalibrate()
{
ledInterval = 1000;//ms
ledFlag = true;
pedals.calibrateFlag = true;
pedals.accelerator.min = AXIS_MAX;
pedals.accelerator.max = AXIS_MIN;
pedals.brake.min = AXIS_MAX;
pedals.brake.max = AXIS_MIN;
pedals.clutch.min = AXIS_MAX;
pedals.clutch.max = AXIS_MIN;
if (debugMode) {
Serial.println(F("Start calibration"));
}
// Timestamp for start of calibration period
calibrateTimer = millis();
}
void processCalibrate()
{
if (pedals.accelerator.value < pedals.accelerator.min) {pedals.accelerator.min = pedals.accelerator.value;}
if (pedals.accelerator.value > pedals.accelerator.max) {pedals.accelerator.max = pedals.accelerator.value;}
if (pedals.brake.value < pedals.brake.min) {pedals.brake.min = pedals.brake.value;}
if (pedals.brake.value > pedals.brake.max) {pedals.brake.max = pedals.brake.value;}
if (pedals.clutch.value < pedals.clutch.min) {pedals.clutch.min = pedals.clutch.value;}
if (pedals.clutch.value > pedals.clutch.max) {pedals.clutch.max = pedals.clutch.value;}
// Watch for end of calibration period
if (calibrateTimer + calibrateInterval < millis()) {
finishCalibration();
if (debugMode) {
Serial.println(F("End calibration"));
}
}
}
void finishCalibration()
{
pedals.calibrateFlag = false;
pedals.eepromChangeFlag = true;
Serial.println("Raw Calibration Values:");
debugReport();
if (ENABLE_BRAKE) {
calcBrakeMax();
Serial.println("Brake Calibration Values:");
debugReport();
}
addDeadzones();
Serial.println("DZ Calibration Values:");
debugReport();
}
void calcBrakeMax()
{
pedals.brake.max = (int)(pedals.brake.max / BRAKE_CALIBRATION_POINT);
}
void invertAxes()
{
int16_t endA;
int16_t endB;
if (INVERT_ACCELERATOR) {
endA = pedals.accelerator.min;
endB = pedals.accelerator.max;
pedals.accelerator.min = endB;
pedals.accelerator.max = endA;
}
if (INVERT_BRAKE) {
endA = pedals.brake.min;
endB = pedals.brake.max;
pedals.brake.min = endB;
pedals.brake.max = endA;
}
if (INVERT_CLUTCH) {
endA = pedals.clutch.min;
endB = pedals.clutch.max;
pedals.clutch.min = endB;
pedals.clutch.max = endA;
}
}
void addDeadzones()
{
int16_t dz;
int16_t range;
if (ENABLE_ACCELERATOR) {
range = pedals.accelerator.max - pedals.accelerator.min;
dz = range * DEADZONE_PERCENTAGE;
pedals.accelerator.min += dz;
pedals.accelerator.max -= dz;
}
if (ENABLE_BRAKE) {
range = pedals.brake.max - pedals.brake.min;
dz = range * BRAKE_DZ_PERCENTAGE;
pedals.brake.min += dz;
//pedals.brake.max -= dz;
}
if (ENABLE_CLUTCH) {
range = pedals.clutch.max - pedals.clutch.min;
dz = range * DEADZONE_PERCENTAGE;
pedals.clutch.min += dz;
pedals.clutch.max -= dz;
}
}
void debugReport()
{
Serial.print("accelerator: ");
Serial.print(pedals.accelerator.value);
Serial.print(" (min/max ");
Serial.print(pedals.accelerator.min);
Serial.print("/");
Serial.print(pedals.accelerator.max);
Serial.print(") ");
Serial.print("brake: ");
Serial.print(pedals.brake.value);
Serial.print(" (min/max ");
Serial.print(pedals.brake.min);
Serial.print("/");
Serial.print(pedals.brake.max);
Serial.println(") ");
//Reset timer
debugTimer = millis();
}
void getEEPROM()
{
PedalData eepromData;
EEPROM.get(0, eepromData);
if (eepromData.eepromDataFlag) {
if (debugMode) {
Serial.println(F("Getting EEPROM"));
}
pedals = eepromData;
} else {
if (debugMode) {
Serial.println(F("EEPROM flagged bad"));
}
}
pedals.eepromChangeFlag = false;
}
void resetEEPROM()
{
if (debugMode) {
Serial.println(F("Resetting EEPROM"));
}
ledInterval = 100;//ms
ledFlag = true;
pedals.eepromDataFlag = false;
EEPROM.put(0, 0);
}
void putEEPROM()
{
if (debugMode) {
Serial.println(F("Saving EEPROM"));
}
pedals.eepromDataFlag = true;
pedals.eepromChangeFlag = false;
EEPROM.put(0, pedals);
}
the data.h is here too
#ifndef __DATA_H__
#define __DATA_H__
#include <HX711.h>
#define ACCELERATOR_PIN A2
#define HX711DT_PIN A3
#define HX711SCK_PIN A1
#define CLUTCH_PIN A2
#define CALIBRATE_PIN 9
#define CAL_LED_PIN 8
#define AXIS_MIN -32768
#define AXIS_MAX 32767
#define ENABLE_ACCELERATOR true
#define ENABLE_GHOST_ACCELERATOR false
#define ENABLE_BRAKE false
#define ENABLE_GHOST_BRAKE false
#define ENABLE_CLUTCH false
#define ENABLE_GHOST_CLUTCH false
#define ENABLE_GHOST_BUTTONS 0
#define ENABLE_GHOST_HATS 0
#define INVERT_ACCELERATOR true
#define INVERT_BRAKE false
#define INVERT_CLUTCH false
#define DEADZONE_PERCENTAGE 0.00 // Adds deadzones to top and bottom of range, based on percentage of total range, for Throttle and Clutch
#define BRAKE_DZ_PERCENTAGE 0.0 // Adds a lower deadzone to the brake, based on percentage of total range
#define BRAKE_CALIBRATION_POINT 1.0 // Mapping point for maximum experienced point during calibration
// 0.8 means that 100% output brake will be 20% more force than your calibrated 'max'
// e.g. push your brake to your 80% 'standard' target force during calibration cycle
#define HX711SCALAR 254.0
enum AxisByName : uint8_t
{
AXIS_NULL,
AXIS_THROTTLE,
AXIS_BRAKE,
AXIS_CLUTCH
};
typedef struct AxisData
{
bool invert;
HX711* hx711 = 0;
int16_t value = 0;
int16_t min = AXIS_MIN;
int16_t max = AXIS_MAX;
};
typedef struct PedalData
{
bool eepromDataFlag = false;
bool eepromChangeFlag = false;
bool calibrateFlag = false;
uint16_t flashLEDInterval;
AxisData accelerator;
AxisData brake;
AxisData clutch;
};
#endif // __DATA_H__