Compare commits

...

5 Commits

Author SHA1 Message Date
zvonimir 8798a2d012 docs: add ky-023 to bill of materials 2026-04-24 15:30:17 +02:00
zvonimir 79f8a70399 feat: add joystick demo 2026-04-24 15:30:10 +02:00
zvonimir 0c8d5de1c3 chore: add libprintf 2026-04-24 15:29:54 +02:00
zvonimir d5e5f031da feat(joystick): add joystick class 2026-04-24 15:29:45 +02:00
zvonimir 51cbbd35e8 chore: remove unused READMEs 2026-04-24 15:29:26 +02:00
7 changed files with 67 additions and 90 deletions
+1
View File
@@ -4,6 +4,7 @@ An open-source implementation of the Tamagotchi virtual pet game, designed to ru
## Bill of Materials
- Arduino Nano (ATmega328P)
- KY-023 Joystick Module
## Development
After cloning the repository, navigate to the project directory and run `make build` to compile the code. To upload the compiled firmware to your Arduino Nano, use `make upload`.
-37
View File
@@ -1,37 +0,0 @@
This directory is intended for project header files.
A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.
```src/main.c
#include "header.h"
int main (void)
{
...
}
```
Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.
In C, the convention is to give header files names that end with `.h'.
Read more about using header files in official GCC documentation:
* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
+28
View File
@@ -0,0 +1,28 @@
#pragma once
#include <Arduino.h>
/*
* A helper class to read values from the KY-023 joystick module.
* It provides methods to get the X and Y positions of the joystick, as well as whether the button is pressed.
* The joystick is connected to the following pins:
* - VRx (X-axis) connected to A0
* - VRy (Y-axis) connected to A1
* - SW (button) connected to D4
*
* Note: The button is active LOW and requires a pull-up resistor, which can either be external (10kΩ)
* or the internal pull-up resistor of the microcontroller (default).
*
* TODO: Add "deadzone" handling to prevent small joystick movements from being registered as input.
*/
class Joystick {
public:
Joystick();
double getX() const;
double getY() const;
bool isPressed() const;
private:
uint8_t vrx;
uint8_t vry;
uint8_t sw;
};
-46
View File
@@ -1,46 +0,0 @@
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into the executable file.
The source code of each library should be placed in a separate directory
("lib/your_library_name/[Code]").
For example, see the structure of the following example libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
Example contents of `src/main.c` using Foo and Bar:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
The PlatformIO Library Dependency Finder will find automatically dependent
libraries by scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html
+4
View File
@@ -15,3 +15,7 @@ framework = arduino
; Set monitor baud rate
monitor_speed = 9600
; External libraries
lib_deps =
embeddedartistry/LibPrintf@1.2.13
+22
View File
@@ -0,0 +1,22 @@
#include "joystick.hpp"
// Initialize the joystick pins in the constructor
Joystick::Joystick() : vrx(A0), vry(A1), sw(4) {
pinMode(vrx, INPUT);
pinMode(vry, INPUT);
pinMode(sw, INPUT_PULLUP);
}
// Map the analog readings from the joystick to a range of -100 to 100 for both X and Y axes
double Joystick::getX() const {
return map(analogRead(vrx), 0, 1023, -100, 100);
}
double Joystick::getY() const {
return map(analogRead(vry), 0, 1023, -100, 100);
}
// Check if the joystick button is pressed (active LOW)
bool Joystick::isPressed() const {
return digitalRead(sw) == LOW;
}
+12 -7
View File
@@ -1,4 +1,8 @@
#include <Arduino.h>
#include <LibPrintf.h>
#include "joystick.hpp"
Joystick joystick;
void setup() {
Serial.begin(9600);
@@ -11,11 +15,12 @@ void setup() {
}
void loop() {
Serial.println("ping");
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
double x = joystick.getX();
double y = joystick.getY();
bool pressed = joystick.isPressed();
Serial.println("pong");
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
printf("X: %.2f | Y: %.2f | Pressed: %s\n", x, y, pressed ? "Yes" : "No");
digitalWrite(LED_BUILTIN, pressed ? HIGH : LOW);
delay(500);}