Skip to content

Commit 65125df

Browse files
committed
Update Pro Micro Examples
- rename files to match the examples in the tutorial - adjust the output for the hardware UART in **Example_1_Blinkies_Hello.ino** file to distinguish betweeh the serial monitor - add `Keyboard.begin()` in `setup()` for **Example_2a_SimpleKeyboard_HID.ino** (this appears to work without initializing it but it was added to be consistent with Arduino's reference example) - add `Mouse.begin()` in `setup()` to **Example_2b_SimpleJoystickMouse_HID.ino** (this appears to work without initializing it but it was added to be consistent with Arduino's reference example) - include the ability to invert the mouse using the joystick depending on the orientation of the joystick - include examples to that utilize Qwiic connect system using the Qwiic Joystick and Qwiic Keyboard - autoformat
1 parent 4c69b2c commit 65125df

File tree

7 files changed

+428
-50
lines changed

7 files changed

+428
-50
lines changed

Firmware/Blinkies/Blinkies.ino

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* Pro Micro Test Code
2+
by: Nathan Seidle
3+
modified by: Jim Lindblom
4+
SparkFun Electronics
5+
date: September 16, 2013
6+
license: Public Domain - please use this code however you'd like.
7+
It's provided as a learning tool.
8+
9+
This code is provided to show how to control the SparkFun
10+
ProMicro's TX and RX LEDs within a sketch. It also serves
11+
to explain the difference between Serial.print() and
12+
Serial1.print().
13+
*/
14+
15+
int RXLED = 17; // The RX LED has a defined Arduino pin
16+
// Note: The TX LED was not so lucky, we'll need to use pre-defined
17+
// macros (TXLED1, TXLED0) to control that.
18+
// (We could use the same macros for the RX LED too -- RXLED1,
19+
// and RXLED0.)
20+
21+
void setup()
22+
{
23+
pinMode(RXLED, OUTPUT); // Set RX LED as an output
24+
// TX LED is set as an output behind the scenes
25+
26+
Serial.begin(9600); //This pipes to the serial monitor
27+
Serial.println("Initialize Serial Monitor");
28+
29+
Serial1.begin(9600); //This is the UART, pipes to sensors attached to board
30+
Serial1.println("Initialize Serial Hardware UART Pins");
31+
}
32+
33+
void loop()
34+
{
35+
Serial.println("Hello world!"); // Print "Hello World" to the Serial Monitor
36+
Serial1.println("Hello! Can anybody hear me?"); // Print "Hello!" over hardware UART
37+
38+
digitalWrite(RXLED, LOW); // set the RX LED ON
39+
TXLED0; //TX LED is not tied to a normally controlled pin so a macro is needed, turn LED OFF
40+
delay(1000); // wait for a second
41+
42+
digitalWrite(RXLED, HIGH); // set the RX LED OFF
43+
TXLED1; //TX LED macro to turn LED ON
44+
delay(1000); // wait for a second
45+
}

Firmware/SimpleKeyboard_HID/SimpleKeyboard_HID.ino renamed to Firmware/Example_2a_SimpleKeyboard_HID/Example_2a_SimpleKeyboard_HID.ino

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
code in your future endeavors! Reuse and share.
77
88
This is very simplistic code that allows you to send a 'z' with
9-
a momentary pushbutton.
10-
*/
9+
a momentary pushbutton.
10+
*/
1111

1212
#include <Keyboard.h>
1313
int buttonPin = 9; // Set a button to any pin
@@ -16,6 +16,8 @@ void setup()
1616
{
1717
pinMode(buttonPin, INPUT); // Set the button as an input
1818
digitalWrite(buttonPin, HIGH); // Pull the button high
19+
20+
Keyboard.begin(); //Init keyboard emulation
1921
}
2022

2123
void loop()

Firmware/SimpleJoystickMouse_HID/SimpleJoystickMouse_HID.ino renamed to Firmware/Example_2b_SimpleJoystickMouse_HID/Example_2b_SimpleJoystickMouse_HID.ino

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
No restrictions. Just keep this license if you go on to use this
66
code in your future endeavors! Reuse and share.
77
8-
This is very simplistic code that allows you to turn the
8+
This is very simplistic code that allows you to turn the
99
SparkFun Thumb Joystick (http://www.sparkfun.com/products/9032)
1010
into an HID Mouse. The select button on the joystick is set up
11-
as the mouse left click.
12-
*/
11+
as the mouse left click.
12+
*/
1313
#include <Mouse.h>
1414
int horzPin = A0; // Analog output of horizontal joystick pin
1515
int vertPin = A1; // Analog output of vertical joystick pin
@@ -20,6 +20,9 @@ int vertValue, horzValue; // Stores current analog output of each axis
2020
const int sensitivity = 200; // Higher sensitivity value = slower mouse, should be <= about 500
2121
int mouseClickFlag = 0;
2222

23+
//int invertMouse = 1; //Invert joystick based on orientation
24+
int invertMouse = -1; //Noninverted joystick based on orientation
25+
2326
void setup()
2427
{
2528
pinMode(horzPin, INPUT); // Set both analog pins as inputs
@@ -30,6 +33,7 @@ void setup()
3033
vertZero = analogRead(vertPin); // get the initial values
3134
horzZero = analogRead(horzPin); // Joystick should be in neutral position when reading these
3235

36+
Mouse.begin(); //Init mouse emulation
3337
}
3438

3539
void loop()
@@ -38,16 +42,16 @@ void loop()
3842
horzValue = analogRead(horzPin) - horzZero; // read horizontal offset
3943

4044
if (vertValue != 0)
41-
Mouse.move(0, vertValue/sensitivity, 0); // move mouse on y axis
45+
Mouse.move(0, (invertMouse * (vertValue / sensitivity)), 0); // move mouse on y axis
4246
if (horzValue != 0)
43-
Mouse.move(horzValue/sensitivity, 0, 0); // move mouse on x axis
47+
Mouse.move((invertMouse * (horzValue / sensitivity)), 0, 0); // move mouse on x axis
4448

4549
if ((digitalRead(selPin) == 0) && (!mouseClickFlag)) // if the joystick button is pressed
4650
{
4751
mouseClickFlag = 1;
4852
Mouse.press(MOUSE_LEFT); // click the left button down
4953
}
50-
else if ((digitalRead(selPin))&&(mouseClickFlag)) // if the joystick button is not pressed
54+
else if ((digitalRead(selPin)) && (mouseClickFlag)) // if the joystick button is not pressed
5155
{
5256
mouseClickFlag = 0;
5357
Mouse.release(MOUSE_LEFT); // release the left button
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/******************************************************************************
2+
Example_3a_Qwiic_Joystick_HID_Mouse.ino
3+
Written by: Ho Yun "Bobby" Chan
4+
Date: January 13, 2020
5+
Development Environment Specifics:
6+
Arduino IDE 1.8.9
7+
8+
Description:
9+
Based on the Jim's Pro Micro "HID Mouse" and Wes' Qwiic Joystick "basic reading"
10+
examples, this example moves your computer's mouse based on the joystick's
11+
position. Pressing down on the joystick's will enable a mouse's left click.
12+
The left click will relese as soon as you stop pressing down on the joystick.
13+
14+
Libraries:
15+
Mouse.h (included with Arduino IDE)
16+
Wire.h (included with Arduino IDE)
17+
SparkFun_Qwiic_Joystick_Arduino_Library.h (included in the src folder) http://librarymanager/All#SparkFun_joystick
18+
19+
License:
20+
This code is released under the MIT License (http://opensource.org/licenses/MIT)
21+
22+
******************************************************************************/
23+
24+
#include <Mouse.h>
25+
#include <Wire.h>
26+
27+
#include "SparkFun_Qwiic_Joystick_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_joystick
28+
JOYSTICK joystick; //Create instance of this object
29+
30+
int vertZero, horzZero; // Stores the initial value of each axis, usually around 512
31+
int vertValue, horzValue; // Stores current analog output of each axis
32+
const int sensitivity = 200; // Higher sensitivity value = slower mouse, should be <= about 500
33+
int mouseClickFlag = 0;
34+
35+
//int invertMouse = 1; //Invert joystick based on orientation
36+
int invertMouse = -1; //Noninverted joystick based on orientation
37+
38+
//Debug mode, comment one of these lines out using a syntax
39+
//for a single line comment ("//"):
40+
#define DEBUG 0 //0 = HID only
41+
//#define DEBUG 1 //1 = HID with serial output
42+
43+
void setup() {
44+
#if DEBUG
45+
Serial.begin(9600);
46+
Serial.println("Example 3: HID Mouse w/ Qwiic Joystick");
47+
#endif
48+
49+
if (joystick.begin() == false)
50+
{
51+
#if DEBUG
52+
Serial.println("Joystick does not appear to be connected. Please check wiring. Freezing...");
53+
#endif
54+
while (1);
55+
}
56+
57+
delay(1000); // short delay to let outputs settle
58+
vertZero = joystick.getVertical(); // get the initial values
59+
horzZero = joystick.getHorizontal(); // Joystick should be in neutral position when reading these
60+
61+
Mouse.begin(); //Init mouse emulation
62+
}
63+
64+
void loop() {
65+
#if DEBUG
66+
Serial.print("X: ");
67+
Serial.print(joystick.getHorizontal());
68+
69+
Serial.print(" Y: ");
70+
Serial.print(joystick.getVertical());
71+
72+
Serial.print(" Button: ");
73+
Serial.println(joystick.getButton());
74+
#endif
75+
76+
vertValue = joystick.getVertical() - vertZero; // read vertical offset
77+
horzValue = joystick.getHorizontal() - horzZero; // read horizontal offset
78+
79+
if (vertValue != 0)
80+
Mouse.move(0, (invertMouse * (vertValue / sensitivity)), 0); // move mouse on y axis
81+
if (horzValue != 0)
82+
Mouse.move((invertMouse * (horzValue / sensitivity)), 0, 0); // move mouse on x axis
83+
84+
if ((joystick.getButton() == 0) && (!mouseClickFlag)) // if the joystick button is pressed
85+
{
86+
mouseClickFlag = 1;
87+
Mouse.press(MOUSE_LEFT); // click the left button down
88+
}
89+
else if ((joystick.getButton()) && (mouseClickFlag)) // if the joystick button is not pressed
90+
{
91+
mouseClickFlag = 0;
92+
Mouse.release(MOUSE_LEFT); // release the left button
93+
}
94+
95+
//delay(200); //remove "//" on this line if you need a small delay
96+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/******************************************************************************
2+
Example_3b_Qwiic_Keypad_HID_Keyboard.ino
3+
Written by: Ho Yun "Bobby" Chan
4+
Date: February 6, 2020
5+
Development Environment Specifics:
6+
Arduino IDE 1.8.9
7+
8+
Description:
9+
Based on the Jim's Pro Micro "HID Mouse" and Pete' Qwiic Keypad "read button"
10+
examples, this example outputs keyboard presses associated with the keypad.
11+
12+
Libraries:
13+
Keyboard.h (included with Arduino IDE)
14+
Wire.h (included with Arduino IDE)
15+
SparkFun_Qwiic_Keypad_Arduino_Library.h (included in the src folder) http://librarymanager/All#SparkFun_keypad
16+
17+
License:
18+
This code is released under the MIT License (http://opensource.org/licenses/MIT)
19+
20+
******************************************************************************/
21+
22+
#include <Keyboard.h>
23+
#include <Wire.h>
24+
#include "SparkFun_Qwiic_Keypad_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_keypad
25+
KEYPAD keypad1; //Create instance of this object
26+
27+
void setup() {
28+
Serial.begin(9600);
29+
Serial.println("Qwiic KeyPad Example");
30+
31+
if (keypad1.begin() == false) // Note, using begin() like this will use default I2C address, 0x4B.
32+
// You can pass begin() a different address like so: keypad1.begin(Wire, 0x4A).
33+
{
34+
Serial.println("Keypad does not appear to be connected. Please check wiring. Freezing...");
35+
while (1);
36+
}
37+
Serial.print("Initialized. Firmware Version: ");
38+
Serial.println(keypad1.getVersion());
39+
Serial.println("Press a button: * to do a space. # to go to next line.");
40+
41+
Keyboard.begin(); //Init keyboard emulation
42+
}
43+
44+
void loop() {
45+
keypad1.updateFIFO(); // necessary for keypad to pull button from stack to readable register
46+
char button = keypad1.getButton();
47+
48+
if (button == -1)
49+
{
50+
Serial.println("No keypad detected");
51+
delay(1000);
52+
}
53+
else if (button != 0)
54+
{
55+
if (button == '0') {//note that this is a keypad '0' not the button press itself
56+
Keyboard.write('0');
57+
}
58+
else if (button == '1') {
59+
Keyboard.write('1');
60+
}
61+
else if (button == '2') {
62+
Keyboard.write('2');
63+
}
64+
else if (button == '3') {
65+
Keyboard.write('3');
66+
}
67+
else if (button == '4') {
68+
Keyboard.write('4');
69+
}
70+
else if (button == '5') {
71+
Keyboard.write('5');
72+
}
73+
else if (button == '6') {
74+
Keyboard.write('6');
75+
}
76+
else if (button == '7') {
77+
Keyboard.write('7');
78+
}
79+
else if (button == '8') {
80+
Keyboard.write('8');
81+
}
82+
else if (button == '9') {
83+
Keyboard.write('9');
84+
}
85+
else if (button == '#') {
86+
Keyboard.write('#');
87+
}
88+
else if (button == '*') {
89+
Keyboard.write('*');
90+
}
91+
}
92+
93+
//Do something else. Don't call your Keypad a ton otherwise you'll tie up the I2C bus
94+
delay(25); //25 is good, more is better
95+
}

0 commit comments

Comments
 (0)