This is comment for paste
Untitled
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Joystick.h"
- #include "HX711.h"
- // HX711 circuit wiring
- const int LOADCELL_DOUT_PIN = 2;
- const int LOADCELL_SCK_PIN = 3;
- HX711 scale;
- // Adjust these values based on your load cell's actual minimum and maximum readings
- float calibration_factor = 40; // Adjust based on your setup
- long zeroValue = 0; // The zero value after taring
- long maxValue = 5000; // Adjusted maximum value based on your input
- // Initialize the Joystick library
- Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
- JOYSTICK_TYPE_JOYSTICK, 1, 0, // 1 button, no hat switch
- false, false, false, false, false, false,
- false, false, false, true, false); // Only the brake axis is enabled
- void setup() {
- Serial.begin(57600);
- scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
- scale.set_scale(calibration_factor); // Apply the calibration factor
- scale.tare(); // Reset the scale to zero
- Joystick.begin(false); // Initialize joystick without automatic send
- Joystick.setBrakeRange(0, 1023);
- }
- void loop() {
- if (scale.is_ready()) {
- long reading = scale.get_units();
- // Map the reading from the load cell range to the joystick brake range
- int brakeValue = map(reading, zeroValue, maxValue, 0, 1023);
- brakeValue = constrain(brakeValue, 0, 1023); // Ensure the value is within the expected range
- Joystick.setBrake(brakeValue);
- Joystick.sendState(); // Explicitly send the state to the joystick
- Serial.print("Mapped Brake value: ");
- Serial.println(brakeValue);
- } else {
- Serial.println("HX711 not found.");
- }
- delay(10); // Short delay for stability
- }
Advertisement
Add Comment
Please, Sign In to add comment