RAK14013 WisBlock Joystick Module Quick Start Guide
Prerequisite
- Assemble the RAK14013 with a WisBlock Base and WisBlock Core by following the RAK14013 Installation Guide.
- Download and install the Arduino IDE, then add the RAKwireless Core boards by installing the RAKwireless Arduino BSP.
- USB Cable
- RAK14007 WisBlock Interface Module
- Li-Ion/LiPo battery (optional)
- Solar charger (optional)
Software Configuration and Example
In this example, you will be getting the rotation position of the knob or if it is pressed to your serial monitor.
NOTE
- This guide uses the RAK4631 WisBlock Core to demonstrate the configuration and upload process. The example is also compatible with other WisBlock Cores, including the RAK11200 and RAK11310.
- When using the RAK11200 WisBlock WiFi Module, configure the BOOT0 pin properly before uploading the firmware. Incorrect BOOT0 configuration may cause the firmware upload to fail. For more information, refer to the RAK11200 Quick Start Guide.
- You need to select first the WisBlock Core you have.
Figure 1: Selecting RAK4631 as WisBlock Core- Copy the example code below:
Click to view the code
/**
@file RAK14007_ATTINY441_Joystick.ino
@author rakwireless.com
@brief Joystick example
@version 0.1
@date 2021-7-28
@copyright Copyright (c) 2020
**/
#include <Wire.h>
#define INT_LED 35
#define SLAVE_I2C_ADDRESS_DEFAULT 0x21
//JOYSTICK Sensor Register Addresses
#define JOYSTICK_GET_DATA 0x01 // (r) 2 bytes
#define JOYSTICK_GET_I2C_ADDRESS 0x02 //(r) 1 bytes
#define JOYSTICK_SET_I2C_ADDRESS 0x03 //(w) 1 bytes
#define JOYSTICK_GET_VERSION 0x04 // (r) 1 bytes
#define JOYSTICK_SET_INT_TYPE 0x05 // (W) 1 bytes, five keys, from bit4-bit0, 0-push, 1-release.
#define JOYSTICK_GET_X_POSITION 0x06 // (r) 2 bytes, return the analog quantity. Range:0-1024, middle position is 507(near 512)
#define JOYSTICK_GET_Y_POSITION 0x07 // (r) 2 bytes, return the analog quantity. Range:0-1024, middle position is 507(near 512)
#define JOYSTICK_SET_X_LEFT_THREHOLD 0x08 // (w) 2 bytes, set the analog quantity. Range:0-1024
#define JOYSTICK_SET_X_RIGHT_THREHOLD 0x09 // (w) 2 bytes, set the analog quantity. Range:0-1024
#define JOYSTICK_SET_Y_UP_THREHOLD 0x0A // (w) 2 bytes, set the analog quantity. Range:0-1024
#define JOYSTICK_SET_Y_DOWN_THREHOLD 0x0B // (w) 2 bytes, set the analog quantity. Range:0-1024
#define JOYSTICK_GET_X_LEFT_THREHOLD 0x0C // (r) 2 bytes, get the analog quantity. Range:0-1024
#define JOYSTICK_GET_X_RIGHT_THREHOLD 0x0D // (r) 2 bytes, get the analog quantity. Range:0-1024
#define JOYSTICK_GET_Y_UP_THREHOLD 0x0E // (r) 2 bytes, get the analog quantity. Range:0-1024
#define JOYSTICK_GET_Y_DOWN_THREHOLD 0x0F // (r) 2 bytes, get the analog quantity. Range:0-1024
uint8_t flag = 0;
void detect()
{
flag = 1;
digitalWrite(35, HIGH);
}
//motion1 NA NA NA keyE keyD keyC keyB keyA
//bit 7 6 5 4 3 2 1 0
//motion2 NA NA NA NA x-l x-r y-u y-d
//bit 7 6 5 4 3 2 1 0
void judge()
{
uint8_t data[2] = {0};
uint8_t motion[2] = {0}; //we use 8 bit to record the motion
uint16_t tmp = 0;
read_from_ttiny441(JOYSTICK_GET_DATA,motion,2);
if((motion[0] & 0x01) == 0x01){Serial.println("KeyA Pressed");}
if((motion[0] & 0x02) == 0x02){Serial.println("KeyB Pressed");}
if((motion[0] & 0x04) == 0x04){Serial.println("KeyC Pressed");}
if((motion[0] & 0x08) == 0x08){Serial.println("KeyD Pressed");}
if((motion[0] & 0x10) == 0x10){Serial.println("KeyE Pressed");}
if((motion[1] & 0x04) == 0x04)
{
tmp = 0;
read_from_ttiny441(JOYSTICK_GET_X_POSITION,data,2);
tmp = (((uint16_t)data[0]) << 8) | ((uint16_t)data[1]);
if(tmp != 0xFFFF)
{
Serial.print("RIGHT ");
Serial.print("X Position is ");
Serial.println(tmp);
}
}
if((motion[1] & 0x08) == 0x08)
{
tmp = 0;
read_from_ttiny441(JOYSTICK_GET_X_POSITION,data,2);
tmp = (((uint16_t)data[0]) << 8) | ((uint16_t)data[1]);
if(tmp != 0xFFFF)
{
Serial.print("LEFT ");
Serial.print("X Position is ");
Serial.println(tmp);
}
}
if((motion[1] & 0x02) == 0x02)
{
tmp = 0;
read_from_ttiny441(JOYSTICK_GET_Y_POSITION,data,2);
tmp = (((uint16_t)data[0]) << 8) | ((uint16_t)data[1]);
if(tmp != 0xFFFF)
{
Serial.print("UP ");
Serial.print("Y Position is ");
Serial.println(tmp);
}
}
if((motion[1] & 0x01) == 0x01)
{
tmp = 0;
read_from_ttiny441(JOYSTICK_GET_Y_POSITION,data,2);
tmp = (((uint16_t)data[0]) << 8) | ((uint16_t)data[1]);
if(tmp != 0xFFFF)
{
Serial.print("DOWN ");
Serial.print("Y Position is ");
Serial.println(tmp);
}
}
digitalWrite(INT_LED, LOW);
}
void setup() {
pinMode(WB_IO2, OUTPUT);
digitalWrite(WB_IO2, HIGH);
pinMode(INT_LED, OUTPUT);
digitalWrite(INT_LED, LOW);
Serial.begin(115200);
Serial.print("Joystick Test!");
Wire.begin();
delay(1000);
uint8_t data = 0;
read_from_ttiny441(JOYSTICK_GET_VERSION,&data,1);
Serial.print("Sensor Firmware version: ");
Serial.println(data,HEX);
Serial.println();
//here we use interrupt to notify
pinMode(WB_IO6, INPUT);
attachInterrupt(WB_IO6, detect, RISING);
//set interrupt type for each key. 0 for push/ 1 for release. This step is necessary.
uint8_t itp = 0x00;
write_to_ttiny441(JOYSTICK_SET_INT_TYPE,&itp,1);
}
void loop() {
if(flag == 1)
{
//slave interrupt will clear after host read
judge();
flag = 0;
}
delay(10);
}
void read_from_ttiny441(uint8_t reg, uint8_t *data, uint8_t length)
{
Wire.beginTransmission(SLAVE_I2C_ADDRESS_DEFAULT);
Wire.write(reg); // sends five bytes
Wire.endTransmission(); // stop transmitting
delay(20);
Wire.requestFrom(SLAVE_I2C_ADDRESS_DEFAULT,length);
int i = 0;
while ( Wire.available() ) // slave may send less than requested
{
data[i++]= Wire.read(); // receive a byte as a proper uint8_t
}
}
void write_to_ttiny441(uint8_t reg, uint8_t *data, uint8_t length)
{
Wire.beginTransmission(SLAVE_I2C_ADDRESS_DEFAULT);
Wire.write(reg); // sends five bytes
for(int i=0;i<length;i++)
{
Wire.write(data[i]);
}
Wire.endTransmission(); // stop transmitting
}
If you experience any error in compiling the example sketch, check the updated code for the RAK14013 WisBlock Joystick Module that can be found on the RAK14013 WisBlock Example Code Repository.
- Select the right serial port and upload the code.
Figure 1: Selecting the correct serial port
Figure 1: Uploading the sample code- When you have successfully uploaded the sample code, you may rotate or press the joystick and buttons of RAK14013. Then you can open up your serial monitor to get the joystick reading.
Figure 1: Position readings in serial monitor