Guest User

Untitled

a guest
Oct 3rd, 2021
2,066
1
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "Joystick.h" //https://github.com/MHeironimus/ArduinoJoystickLibrary
  2. #include "HX711.h" //https://github.com/bogde/HX711
  3.  
  4. #define DOUT  3
  5. #define CLK  2
  6. HX711 scale;
  7. float calibration_factor = -300; //this controls how sensitive the pedal is
  8.  
  9. Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
  10.   JOYSTICK_TYPE_JOYSTICK, 0, 0,
  11.   false, false, false, false, false, false,
  12.   false, false, false, true, false);
  13.  
  14. int brake = 0;
  15. int lastBrakeValue = 0;
  16.  
  17. void setup() {
  18.   //Serial.begin(9600);
  19.  
  20.   scale.begin(DOUT, CLK);
  21.   scale.set_scale();
  22.   scale.tare(); //Reset the scale to 0
  23.   long zero_factor = scale.read_average(); //Get a baseline reading
  24.   scale.set_scale(calibration_factor);
  25.   Joystick.setBrakeRange(0, 1023);
  26.   Joystick.begin();
  27. }
  28.  
  29. void loop() {
  30.   int brake = scale.get_units();
  31.   //Serial.print(String(brake) + " >> ");
  32.   if(brake > 1023){
  33.     brake = 1023;
  34.   }else if(brake < 10){
  35.     brake = 0;
  36.   }
  37.   //Serial.println(brake);
  38.   if (lastBrakeValue != brake) {
  39.      Joystick.setBrake(brake);
  40.      lastBrakeValue = brake;
  41.   }
  42.   delay(1);
  43. }
Advertisement
Comments
  • bluexpois
    2 years
    # text 0.18 KB | 0 0
    1. how do i make it work :( my load cells read with the test but when using this code it does not show any responses. i am new to arduino stuff. if you have any tips that would be great.
    2.  
  • bluexpois
    2 years
    # text 1.59 KB | 0 0
    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.  
Add Comment
Please, Sign In to add comment