EloiStree / HelloInput

Collect of information about old project on Input in aim to do a workshop on the topic
0 stars 0 forks source link

Keyword: Arduino Leonardo #38

Open EloiStree opened 1 month ago

EloiStree commented 1 month ago

Planning to simulate a keyboard or Xbox controller? You'll need an Arduino Micro or an Arduino Leonardo—both are designed specifically for this purpose.

If you want something compact and stable, the Arduino Micro is a good choice. However, I often need to salvage parts from my projects, and for that, the Leonardo is generally better.

If you’re not a fan of C++, you could consider a Raspberry Pi Pico W with CircuitPython, though I haven’t found any code that replicates Xbox XInput functionality on the Pico like it does on Arduino.

image
https://amzn.to/4hzaxyl

Simulate Keyboard and mouse with this one is as simple 🤖:


#include <Keyboard.h>
#include <Mouse.h>

// Define analog pins for the potentiometers
const int potXPin = A0;  // X-axis movement (horizontal)
const int potYPin = A1;  // Y-axis movement (vertical)

// Define the button pin
const int buttonPin = 2;

// Variables to store the potentiometer values
int potXValue = 0;
int potYValue = 0;

// Variable for the button state
bool buttonState = false;
bool lastButtonState = false;

void setup() {
  // Initialize the serial communication (optional, for debugging)
  Serial.begin(9600);

  // Set up the button pin
  pinMode(buttonPin, INPUT_PULLUP);  // Button uses internal pull-up resistor

  // Initialize the Keyboard library
  Keyboard.begin();
}

void loop() {
  // Read potentiometer values (0-1023)
  potXValue = analogRead(potXPin);
  potYValue = analogRead(potYPin);

  // Map the potentiometer values to mouse movement range (-20 to 20)
  int mouseX = map(potXValue, 0, 1023, -20, 20);
  int mouseY = map(potYValue, 0, 1023, -20, 20);

  // Move the mouse (this is an example, you can remove or modify as needed)
  Mouse.move(mouseX, mouseY);

  // Read the button state
  buttonState = digitalRead(buttonPin) == LOW;  // Button pressed (active low)

  // Check if the button is pressed (for jump action)
  if (buttonState && !lastButtonState) {
    // Simulate pressing the spacebar (jump)
    Keyboard.press(KEY_SPACE);
    delay(100); // Short delay to simulate holding the spacebar
    Keyboard.release(KEY_SPACE);  // Release spacebar after press
  }

  // Save the current button state for next iteration
  lastButtonState = buttonState;

  // Small delay for smoother operation
  delay(10);
}

Note: did not verified but apparently you could use 12 analog and not just 6 on them. Need to be check.

EloiStree commented 1 week ago

🐿️ Can we use all 12 analog pin on Leonardo ?


🤖 Yes, you can use all 12 analog-capable pins on the Arduino Leonardo, but with some distinctions:

  1. Dedicated Analog Pins (A0–A5):

    • The Arduino Leonardo has six dedicated analog input pins, labeled A0 to A5. These pins are exclusively designed for analog input and can be used directly with the analogRead() function.
  2. Additional Analog-Capable Pins (D4, D6, D8, D9, D10, D12):

    • The Leonardo also allows some digital pins to be used as analog inputs, adding another six pins with analog capabilities. These pins include D4, D6, D8, D9, D10, and D12.
    • To use these pins for analog input, you can refer to them as A6 to A11.

So, in total, you get 12 analog-capable inputs (A0–A11).

Summary

Usage Example

To read from these pins, simply use:

int valueA0 = analogRead(A0); // Dedicated analog input
int valueA6 = analogRead(A6); // Digital pin D4 as an analog input

This flexibility allows you to maximize the number of analog inputs on your Leonardo if needed.