diff --git a/Finishing-Project.ino b/Finishing-Project.ino index 08b02c8..d86015d 100644 --- a/Finishing-Project.ino +++ b/Finishing-Project.ino @@ -1,7 +1,8 @@ #include #include -#include #include +#include "NeoPixel.h" +#include "Vector2.h" #ifdef __AVR__ #include #endif @@ -11,27 +12,49 @@ #define STCK_Y_PIN A1 ezButton butt(BTN_PIN); -Adafruit_NeoPixel neoPixel(16,4,NEO_GRB + NEO_KHZ800); +NeoPixel neoPixel(16,4,NEO_GRB + NEO_KHZ800); LiquidCrystal_I2C lcd(0x28,16,2); -int stickX; -int stickY; - +Vector2 stickPos; void setup() { - /*neoPixel.begin(); + neoPixel.begin(); neoPixel.clear(); for(int pixel = 0; pixel < 16; pixel++){ neoPixel.setPixelColor(pixel, neoPixel.ColorHSV(pixel*5461,255,255-pixel)); neoPixel.show(); - delay(250); - }*/ - + delay(50); + } + for (int pixel = 0; pixel < 16; pixel++) + { + neoPixel.setPixelColor(pixel,0,0,0); + neoPixel.show(); + delay(50); + } + Serial.begin(9600); } void loop() { - + stickPos.x = analogRead(STCK_X_PIN)-512; + stickPos.y = analogRead(STCK_Y_PIN)-511; + + int pixels = (stickPos.GetUnsignedAngle()*180/M_PI)/45; + + neoPixel.clear(); + if(stickPos.GetMagnitude() >= 5){ + neoPixel.setPixelColor(8, pixels, 255, 255, 255); + } + neoPixel.show(); + + #pragma region Debug Printing + String debugString = "Stick X: " + String(stickPos.x, DEC) + + " Stick Y: " + String(stickPos.y, DEC) + " Stick Angle: " + String(stickPos.GetSignedAngle()*180/M_PI, DEC) + + " Stick Unisgned Angle: " + String(stickPos.GetUnsignedAngle()*180/M_PI, DEC) + + " Pixel: " + String(pixels, DEC);// + "Stick Y: " + stickY; + Serial.println(debugString); + #pragma endregion } + diff --git a/NeoPixel.cpp b/NeoPixel.cpp new file mode 100644 index 0000000..364b53d --- /dev/null +++ b/NeoPixel.cpp @@ -0,0 +1,9 @@ +#include "NeoPixel.h" + +void NeoPixel::setPixelColor(int segmentCnt, int segmentNum, uint8_t r, uint8_t g, uint8_t b){ + int segmentSize = numPixels()/segmentCnt; + for (int pixel = segmentSize * (segmentNum - 1) + 1; pixel <= segmentSize * segmentNum; pixel++) + { + Adafruit_NeoPixel::setPixelColor(pixel, r, g, b); + } +} \ No newline at end of file diff --git a/NeoPixel.h b/NeoPixel.h new file mode 100644 index 0000000..4406351 --- /dev/null +++ b/NeoPixel.h @@ -0,0 +1,8 @@ +#include +class NeoPixel : public Adafruit_NeoPixel +{ + public: + using Adafruit_NeoPixel::Adafruit_NeoPixel; + using Adafruit_NeoPixel::setPixelColor; + void setPixelColor(int segmentCnt, int n, uint8_t r, uint8_t g, uint8_t b); +}; \ No newline at end of file diff --git a/Vector2.cpp b/Vector2.cpp new file mode 100644 index 0000000..cad303b --- /dev/null +++ b/Vector2.cpp @@ -0,0 +1,14 @@ +#include +#include +#include "Vector2.h" + +double Vector2::GetMagnitude(){ + return sqrt(x*x + y*y); +} +double Vector2::GetSignedAngle(){ + return atan2(y, x); +} +double Vector2::GetUnsignedAngle(){ + double angle = atan2(y, x); + return angle < 0 ? (2*M_PI)+angle : angle; +} \ No newline at end of file diff --git a/Vector2.h b/Vector2.h new file mode 100644 index 0000000..d1ee1e7 --- /dev/null +++ b/Vector2.h @@ -0,0 +1,8 @@ +class Vector2{ + public: + int x; + int y; + double GetMagnitude(); + double GetSignedAngle(); + double GetUnsignedAngle(); +}; \ No newline at end of file