Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

neopixel with joystick
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

neopixel with joystick

by Freeheeler19 on Thu Oct 15, 2020 2:07 pm

Hi,

im having troubles controlling a neopixwl strip with a joystick.

I have a strip of neopixels. now I would like to have one single led light up in the middle of the strip. using the joystick i would like this single led to move left or right on the strip according to the joystick inputs.

The problem i am facing is, that the mapping resolution doesnt seem to be right. Its working, but it only works if I move the joystick very slowly. If I move it too fast, its missing out some led`s. (serial print confirms that...)


thank you very much in advance, any input is appreciated.

cheers cyril


thats the code im running currently (I tried to copy it together from different sketches....)



Code: Select all | TOGGLE FULL SIZE
int VRx = A0;
int VRy = A1;
int SW = 2;

int xPosition = 0;
int yPosition = 0;
int SW_state = 0;
int mapX = 0;
int mapY = 0;
int LedPos = 0;






#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
 #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1:
#define LED_PIN     6

// How many NeoPixels are attached to the Arduino?
#define LED_COUNT  60

// NeoPixel brightness, 0 (min) to 255 (max)
#define BRIGHTNESS 50

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);







void setup() {
 
  Serial.begin(9600);
 
  pinMode(VRx, INPUT);
  pinMode(VRy, INPUT);
  pinMode(SW, INPUT_PULLUP);




// These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  // Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif
  // END of Trinket-specific code.

  strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  strip.show();            // Turn OFF all pixels ASAP
  strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)

}




void loop() {


 // strip.setPixelColor((LED_COUNT/2), 255, 0, 0, 0);
 strip.show();
 
 
  xPosition = analogRead(VRx);
  LedPos = map(xPosition, 0, 1023, 0, LED_COUNT);
 
  Serial.print("LedPosition: ");
  Serial.println(LedPos);


Serial.print("xposition: ");
  Serial.println(xPosition);



 for (int i = 0; i < LED_COUNT/2; i++) {

  strip.setPixelColor(LedPos, 255, 0, 0, 0);
    strip.setPixelColor(LedPos+1, 0, 0, 0, 0);
strip.setPixelColor(LedPos-1, 0, 0, 0, 0);
 

 for (int i = 0; i > LED_COUNT/2; i++) {

  strip.setPixelColor(LedPos, 255, 0, 0, 0);
    strip.setPixelColor(LedPos+1, 0, 0, 0, 0);
    strip.setPixelColor(LedPos-1, 0, 0, 0, 0); //previous led=LedPos-1

  //strip.show();










 

}
 }
  }

Freeheeler19
 
Posts: 22
Joined: Tue Dec 17, 2019 12:19 pm

Re: neopixel with joystick

by adafruit_support_bill on Thu Oct 15, 2020 2:17 pm

The mapping is fine. You just need to remember the previous pixel position so you can erase it.

Code: Select all | TOGGLE FULL SIZE
int OldLedPos = 0;

void loop()
{
   xPosition = analogRead(VRx);
   LedPos = map(xPosition, 0, 1023, 0, LED_COUNT);

   Serial.print("LedPosition: ");
   Serial.println(LedPos);

   Serial.print("xposition: ");
   Serial.println(xPosition);

   strip.setPixelColor(LedPos, 255, 0, 0, 0);
   strip.setPixelColor(OldLedPos, 0, 0, 0, 0);
   OldLedPos = LedPos;

   strip.show();
}

adafruit_support_bill
 
Posts: 78960
Joined: Sat Feb 07, 2009 10:11 am

Re: neopixel with joystick

by Freeheeler19 on Thu Oct 15, 2020 2:36 pm

Very cool! And so simple!

thanks a lot!

cyril

Freeheeler19
 
Posts: 22
Joined: Tue Dec 17, 2019 12:19 pm

Re: neopixel with joystick

by Freeheeler19 on Thu Oct 15, 2020 4:58 pm

what if I would like to have several leds moving along the strip? like a car?

is that possible with the above code or do I need another approach?

Freeheeler19
 
Posts: 22
Joined: Tue Dec 17, 2019 12:19 pm

Re: neopixel with joystick

by adafruit_support_bill on Thu Oct 15, 2020 7:04 pm

Something like:

Code: Select all | TOGGLE FULL SIZE
int OldLedPos = 0;
const int CarLength = 3;

void loop()
{
   xPosition = analogRead(VRx);
   LedPos = map(xPosition, 0, 1023, 0, LED_COUNT);

   Serial.print("LedPosition: ");
   Serial.println(LedPos);

   Serial.print("xposition: ");
   Serial.println(xPosition);

   for (int i = 0; i < CarLength; i++)
   {
      strip.setPixelColor(OldLedPos + CarLength, 0, 0, 0, 0);
      strip.setPixelColor(LedPos + CarLength, 255, 0, 0, 0);
   }
   OldLedPos = LedPos;

   strip.show();
}


adafruit_support_bill
 
Posts: 78960
Joined: Sat Feb 07, 2009 10:11 am

Re: neopixel with joystick

by Freeheeler19 on Fri Oct 16, 2020 4:37 am

hey Bill,

thanks for your help.

I tried your last code.
It somehow isnt working as I wish....

There is only one red LED moving along the strip instead of 4...

in the loop, we write the OldLedPos +CarLength to black, and then we write the LedPos +CarLength to 255 red.

But why is there only one led, there should be 4 (LedPos +CarLenght (3))



thanks for any advise...


code:

Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_NeoPixel.h>





int VRx = A0;
int xPosition = 0;
int LedPos = 0;
int OldLedPos = 0;
const int CarLength = 3;



// Which pin on the Arduino is connected to the NeoPixels?
#define LED_PIN     6

// How many NeoPixels are attached to the Arduino?
#define LED_COUNT  60

// NeoPixel brightness, 0 (min) to 255 (max)
#define BRIGHTNESS 50

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);






void setup() {
 
  Serial.begin(9600);
 
  pinMode(VRx, INPUT);
 
  strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  strip.show();            // Turn OFF all pixels ASAP
  strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)

}




void loop() {


   xPosition = analogRead(VRx);
   LedPos = map(xPosition, 0, 1023, 0, LED_COUNT);



   
   Serial.print("LedPosition: ");
   Serial.println(LedPos);

   Serial.print("xposition: ");
   Serial.println(xPosition);



   for (int i = 0; i < CarLength; i++)
   {
     
      strip.setPixelColor(OldLedPos + CarLength, 0, 0, 0, 0);
      strip.setPixelColor(LedPos + CarLength, 255, 0, 0, 0);   
         
   }
   
   OldLedPos = LedPos;
   strip.show();
}

Freeheeler19
 
Posts: 22
Joined: Tue Dec 17, 2019 12:19 pm

Re: neopixel with joystick

by adafruit_support_bill on Fri Oct 16, 2020 6:34 am

Sorry, i didn't think that one through. You need to completely erase the old position before starting to draw the new position.
Code: Select all | TOGGLE FULL SIZE
int OldLedPos = 0;
const int CarLength = 3;

void loop()
{
   xPosition = analogRead(VRx);
   LedPos = map(xPosition, 0, 1023, 0, LED_COUNT);

   Serial.print("LedPosition: ");
   Serial.println(LedPos);

   Serial.print("xposition: ");
   Serial.println(xPosition);

   for (int i = 0; i < CarLength; i++)
   {
      strip.setPixelColor(OldLedPos + CarLength, 0, 0, 0, 0);
   }
   for (int i = 0; i < CarLength; i++)
   {
      strip.setPixelColor(LedPos + CarLength, 255, 0, 0, 0);
   }
   OldLedPos = LedPos;

   strip.show();
}

adafruit_support_bill
 
Posts: 78960
Joined: Sat Feb 07, 2009 10:11 am

Re: neopixel with joystick

by Freeheeler19 on Fri Oct 16, 2020 10:15 am

i tried that already....its not working. still only one led moving.

Freeheeler19
 
Posts: 22
Joined: Tue Dec 17, 2019 12:19 pm

Re: neopixel with joystick

by adafruit_support_bill on Fri Oct 16, 2020 10:24 am

Code: Select all | TOGGLE FULL SIZE
int OldLedPos = 0;
const int CarLength = 3;

void loop()
{
   xPosition = analogRead(VRx);
   LedPos = map(xPosition, 0, 1023, 0, LED_COUNT);

   Serial.print("LedPosition: ");
   Serial.println(LedPos);

   Serial.print("xposition: ");
   Serial.println(xPosition);

   for (int i = 0; i < CarLength; i++)
   {
      strip.setPixelColor(OldLedPos + i, 0, 0, 0, 0);
   }
   for (int i = 0; i < CarLength; i++)
   {
      strip.setPixelColor(LedPos + i, 255, 0, 0, 0);
   }
   OldLedPos = LedPos;

   strip.show();
}


adafruit_support_bill
 
Posts: 78960
Joined: Sat Feb 07, 2009 10:11 am

Re: neopixel with joystick

by Freeheeler19 on Fri Oct 16, 2020 3:52 pm

Thats it! Cool.
Thanks for your support!

Cyril

Freeheeler19
 
Posts: 22
Joined: Tue Dec 17, 2019 12:19 pm

Please be positive and constructive with your questions and comments.