bluexpois
Feb 19th, 2024
76
0
Never
This is comment for paste Untitled
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "Joystick.h"
  2. #include "HX711.h"
  3.  
  4. // HX711 circuit wiring
  5. const int LOADCELL_DOUT_PIN = 2;
  6. const int LOADCELL_SCK_PIN = 3;
  7.  
  8. HX711 scale;
  9.  
  10. // Adjust these values based on your load cell's actual minimum and maximum readings
  11. float calibration_factor = 40; // Adjust based on your setup
  12. long zeroValue = 0; // The zero value after taring
  13. long maxValue = 5000; // Adjusted maximum value based on your input
  14.  
  15. // Initialize the Joystick library
  16. Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
  17. JOYSTICK_TYPE_JOYSTICK, 1, 0, // 1 button, no hat switch
  18. false, false, false, false, false, false,
  19. false, false, false, true, false); // Only the brake axis is enabled
  20.  
  21. void setup() {
  22. Serial.begin(57600);
  23. scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  24. scale.set_scale(calibration_factor); // Apply the calibration factor
  25. scale.tare(); // Reset the scale to zero
  26.  
  27. Joystick.begin(false); // Initialize joystick without automatic send
  28. Joystick.setBrakeRange(0, 1023);
  29. }
  30.  
  31. void loop() {
  32. if (scale.is_ready()) {
  33. long reading = scale.get_units();
  34. // Map the reading from the load cell range to the joystick brake range
  35. int brakeValue = map(reading, zeroValue, maxValue, 0, 1023);
  36. brakeValue = constrain(brakeValue, 0, 1023); // Ensure the value is within the expected range
  37.  
  38. Joystick.setBrake(brakeValue);
  39. Joystick.sendState(); // Explicitly send the state to the joystick
  40. Serial.print("Mapped Brake value: ");
  41. Serial.println(brakeValue);
  42. } else {
  43. Serial.println("HX711 not found.");
  44. }
  45. delay(10); // Short delay for stability
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment