diff --git a/Finishing-Project.ino b/Finishing-Project.ino index d86015d..425b6da 100644 --- a/Finishing-Project.ino +++ b/Finishing-Project.ino @@ -1,6 +1,9 @@ +#include +#include + #include #include -#include +#include #include "NeoPixel.h" #include "Vector2.h" #ifdef __AVR__ @@ -13,17 +16,19 @@ ezButton butt(BTN_PIN); NeoPixel neoPixel(16,4,NEO_GRB + NEO_KHZ800); -LiquidCrystal_I2C lcd(0x28,16,2); +LiquidCrystal_Wire lcd(0x28,16,2); Vector2 stickPos; +uint32_t colors[] = {neoPixel.Color(239, 71, 111), neoPixel.Color(75, 163, 195), neoPixel.Color(89, 60, 143), neoPixel.Color(249, 219, 189)}; void setup() { + lcd.print("שלום עולם"); neoPixel.begin(); neoPixel.clear(); for(int pixel = 0; pixel < 16; pixel++){ - neoPixel.setPixelColor(pixel, neoPixel.ColorHSV(pixel*5461,255,255-pixel)); + neoPixel.setPixelColor(pixel, neoPixel.ColorHSV(pixel*4096,255,255-pixel)); neoPixel.show(); delay(50); } @@ -33,6 +38,7 @@ void setup() neoPixel.show(); delay(50); } + neoPixel.setBrightness(30); Serial.begin(9600); } @@ -40,21 +46,24 @@ void loop() { stickPos.x = analogRead(STCK_X_PIN)-512; stickPos.y = analogRead(STCK_Y_PIN)-511; - - int pixels = (stickPos.GetUnsignedAngle()*180/M_PI)/45; + int uAngle = stickPos.GetUnsignedAngle(); + double magnitude = stickPos.GetMagnitude(); + int pixels = (uAngle*180/M_PI)/(360/4) - floor((uAngle*180/M_PI)/(360/4))>= 0.5 ? floor((uAngle*180/M_PI)/(360/4)) + 1 : floor((uAngle*180/M_PI)/(360/4)); neoPixel.clear(); - if(stickPos.GetMagnitude() >= 5){ - neoPixel.setPixelColor(8, pixels, 255, 255, 255); + if(stickPos.GetMagnitude() > 5){ + neoPixel.setPixelColor(4, pixels, colors[pixels]); } 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; + + " Stick Unisgned Angle: " + String(uAngle*180/M_PI, DEC) + + " Magnitude: " + String(magnitude, DEC) + + " Pixels: " + String(pixels, DEC);// + "Stick Y: " + stickY; Serial.println(debugString); #pragma endregion + } diff --git a/NeoPixel.cpp b/NeoPixel.cpp index 364b53d..dbd4d57 100644 --- a/NeoPixel.cpp +++ b/NeoPixel.cpp @@ -2,8 +2,15 @@ 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++) + for (int pixel = segmentSize * segmentNum; pixel < segmentSize * (segmentNum+1); pixel++) { Adafruit_NeoPixel::setPixelColor(pixel, r, g, b); } +} +void NeoPixel::setPixelColor(int segmentCnt, int segmentNum, uint32_t c){ + int segmentSize = numPixels()/segmentCnt; + for (int pixel = segmentSize * segmentNum; pixel < segmentSize * (segmentNum+1); pixel++) + { + Adafruit_NeoPixel::setPixelColor(pixel, c); + } } \ No newline at end of file diff --git a/NeoPixel.h b/NeoPixel.h index 4406351..e2afa35 100644 --- a/NeoPixel.h +++ b/NeoPixel.h @@ -5,4 +5,5 @@ class NeoPixel : public Adafruit_NeoPixel using Adafruit_NeoPixel::Adafruit_NeoPixel; using Adafruit_NeoPixel::setPixelColor; void setPixelColor(int segmentCnt, int n, uint8_t r, uint8_t g, uint8_t b); + void setPixelColor(int segmentCnt, int segmentNum, uint32_t c); }; \ No newline at end of file diff --git a/Vector2.h b/Vector2.h index d1ee1e7..f96f852 100644 --- a/Vector2.h +++ b/Vector2.h @@ -1,7 +1,7 @@ class Vector2{ public: - int x; - int y; + double x; + double y; double GetMagnitude(); double GetSignedAngle(); double GetUnsignedAngle();