Difference between revisions of "Circuit Playground Express: Arduino-hid"

From Interaction Station Wiki
Jump to navigation Jump to search
(Created page with "in this case, we only need to change some code in Arduino IDE. now. we will use the X and Y values from the motion sensor on CPX to control the position of the nouse. and le...")
 
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
in this case, we only need to change some code in Arduino IDE.
+
In this case, we only need to change some code in Arduino IDE.
  
now. we will use the X and Y values from the motion sensor on CPX to control the position of the nouse.
+
Now. we will use the X and Y values from the motion sensor on CPX to control the position of the nouse.
  
 
and left button on CPX to work as mouse left click.
 
and left button on CPX to work as mouse left click.
Line 58: Line 58:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
 +
[[Category:Circuit Playground Express]]

Latest revision as of 12:02, 13 January 2023

In this case, we only need to change some code in Arduino IDE.

Now. we will use the X and Y values from the motion sensor on CPX to control the position of the nouse.

and left button on CPX to work as mouse left click.

Arduino HID:

 1#include "Mouse.h"
 2#include <Adafruit_CircuitPlayground.h>
 3float X, Y, Z;
 4//int leftButton;
 5bool mouseIsActive = false; 
 6int slideSwitch;
 7
 8void setup() {
 9  // put your setup code here, to run once:
10  CircuitPlayground.begin();
11  Mouse.begin();
12}
13
14void loop() {
15  //read slide switch from cpx.
16  slideSwitch = CircuitPlayground.slideSwitch();
17  // if the switch is on the CPX mouse is active, 
18  if(slideSwitch ==HIGH){
19    mouseIsActive=true;
20    }else{
21      //if the slide switch is off, the cpx mouse will be off
22      mouseIsActive=false;
23      }
24
25  //read the leftButton from cpx
26  int leftButton = CircuitPlayground.leftButton();
27  //x and y position from motion sensor
28  X = CircuitPlayground.motionX();
29  Y = CircuitPlayground.motionY();
30
31  //int xReading=map(x,-1,1,0,100);
32   // if the mouse control state is active, move the mouse with motion sensor X and Y value.
33  if (mouseIsActive) {
34    Mouse.move(X*2, Y*2, 0);
35  }
36
37 // if the mouse is not pressed, press it:
38  if (leftButton==HIGH){
39    if (!Mouse.isPressed(MOUSE_LEFT)) {
40      Mouse.press(MOUSE_LEFT);
41     }
42    }else{  // else the mouse button is not pressed:
43      if (Mouse.isPressed(MOUSE_LEFT)) {
44      Mouse.release(MOUSE_LEFT);
45    }
46      }
47  delay(5);
48}