souris-cercle.ino

Arduino Leonardo — Board Fiche · File : souris-cercle.ino · Language : Arduino · Use case : Dessiner un cercle avec le pointeur de la souris piloté par la carte

The Mouse library enumerates a relative HID mouse: Mouse.move shifts the pointer pixel by pixel. Open a drawing app and watch a circle trace itself — the classic 32U4 demo.

Hardware required

An Arduino Leonardo (ATmega32U4, 16 MHz, 5 V) with native USB: the board can pose as a keyboard or a mouse. None.

Wiring

USB cable to the computer.

How it works

Relative coordinates are computed on a 50 px circle and sent every 10 ms; the pointer follows the path. In the Arduino IDE, select « Arduino Leonardo » and the port, then Upload. Caution: a sketch that sleeps too fast can « lose » the USB port; hold RESET for 12 s to wipe the bootloader.

Going further

Add a potentiometer on A0 to steer the radius, or a button to click (Mouse.click).

souris-cercle.ino

// L'Arduino devient une souris HID : deplace le curseur en cercle.
#include <Mouse.h>

void setup() {
  Mouse.begin();
  delay(2000);
  Mouse.move(100, 0); // part du centre vers la droite
}

void loop() {
  const int RAYON = 50;
  for (int degre = 0; degre < 360; degre += 5) {
    float a = degre * 3.14159 / 180.0;
    float b = (degre + 5) * 3.14159 / 180.0;
    int dx = (int)(RAYON * (cos(b) - cos(a)));
    int dy = (int)(RAYON * (sin(b) - sin(a)));
    Mouse.move(dx, dy);
    delay(10);
  }
  delay(1000);
}

← Back to the board