查看: 1592|回复: 2

[未解决] arduino Leonardo

[复制链接]
  • TA的每日心情
    开心
    2018-1-27 17:23
  • 签到天数: 53 天

    [LV.5]常住居民I

    发表于 2017-8-5 21:37 | 显示全部楼层 |阅读模式
    怎样做鼠标?

    该用户从未签到

    发表于 2017-8-5 22:06 | 显示全部楼层
    例子里面就有

    /*
      ButtonMouseControl

    For Leonardo and Due boards only.

    Controls the mouse from five pushbuttons on an Arduino Leonardo, Micro or Due.

    Hardware:
    * 5 pushbuttons attached to D2, D3, D4, D5, D6


    The mouse movement is always relative. This sketch reads
    four pushbuttons, and uses them to set the movement of the mouse.

    WARNING:  When you use the Mouse.move() command, the Arduino takes
    over your mouse!  Make sure you have control before you use the mouse commands.

    created 15 Mar 2012
    modified 27 Mar 2012
    by Tom Igoe

    this code is in the public domain

    */

    #include "Mouse.h"

    // set pin numbers for the five buttons:
    const int upButton = 2;
    const int downButton = 3;
    const int leftButton = 4;
    const int rightButton = 5;
    const int mouseButton = 6;

    int range = 5;              // output range of X or Y movement; affects movement speed
    int responseDelay = 10;     // response delay of the mouse, in ms


    void setup() {
      // initialize the buttons' inputs:
      pinMode(upButton, INPUT);
      pinMode(downButton, INPUT);
      pinMode(leftButton, INPUT);
      pinMode(rightButton, INPUT);
      pinMode(mouseButton, INPUT);
      // initialize mouse control:
      Mouse.begin();
    }

    void loop() {
      // read the buttons:
      int upState = digitalRead(upButton);
      int downState = digitalRead(downButton);
      int rightState = digitalRead(rightButton);
      int leftState = digitalRead(leftButton);
      int clickState = digitalRead(mouseButton);

      // calculate the movement distance based on the button states:
      int  xDistance = (leftState - rightState) * range;
      int  yDistance = (upState - downState) * range;

      // if X or Y is non-zero, move:
      if ((xDistance != 0) || (yDistance != 0)) {
        Mouse.move(xDistance, yDistance, 0);
      }

      // if the mouse button is pressed:
      if (clickState == HIGH) {
        // if the mouse is not pressed, press it:
        if (!Mouse.isPressed(MOUSE_LEFT)) {
          Mouse.press(MOUSE_LEFT);
        }
      }
      // else the mouse button is not pressed:
      else {
        // if the mouse is pressed, release it:
        if (Mouse.isPressed(MOUSE_LEFT)) {
          Mouse.release(MOUSE_LEFT);
        }
      }

      // a delay so the mouse doesn't move too fast:
      delay(responseDelay);
    }
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    热门推荐

    5分钟带你快速了解新一代开发板:M5STACK
    5分钟带你快速了解新一代
    一、什么是M5Stack M5Stack是一种模块化、可堆叠扩展的开发板,每个模块
    【原创】全球最小口袋3D打印机mini one直播教程贴
    【原创】全球最小口袋3D打
    最近闲得蛋疼,没事搞个掌上3D打印机,先放效果图吧。 搞了半天,终于能正常打印,
    【小项目】基于XIAO的逃跑蜘蛛
    【小项目】基于XIAO的逃跑
    项目介绍:蜘蛛会在夜晚保持安静。但是一有光线,蜘蛛就会开始震动,逃离光线。硬件:
    心电传感器实验 ESP32 + AD8232
    心电传感器实验 ESP32 + A
    早前买了心电传感器,一直没机会用起来,疫情期间无聊翻将出来发现买错了,但是心电导
    Stduino IDE stm32 最小系统 小蓝板 驱动继电器(五)
    Stduino IDE stm32 最小系
    弱电控制强电 单片机输出输入的电压在3.3V到5V之间,而生活中常用电器很多是12V、220V
    Copyright   ©2015-2016  Arduino中文社区  Powered by©Discuz!   
    快速回复 返回顶部 返回列表