neopixel with joystick

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Freeheeler19
 
Posts: 24
Joined: Tue Dec 17, 2019 12:19 pm

neopixel with joystick

Post by Freeheeler19 »

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

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();










  

}
 }
  }

User avatar
adafruit_support_bill
 
Posts: 88142
Joined: Sat Feb 07, 2009 10:11 am

Re: neopixel with joystick

Post by adafruit_support_bill »

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

Code: Select all

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();
}

User avatar
Freeheeler19
 
Posts: 24
Joined: Tue Dec 17, 2019 12:19 pm

Re: neopixel with joystick

Post by Freeheeler19 »

Very cool! And so simple!

thanks a lot!

cyril

User avatar
Freeheeler19
 
Posts: 24
Joined: Tue Dec 17, 2019 12:19 pm

Re: neopixel with joystick

Post by Freeheeler19 »

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?

User avatar
adafruit_support_bill
 
Posts: 88142
Joined: Sat Feb 07, 2009 10:11 am

Re: neopixel with joystick

Post by adafruit_support_bill »

Something like:

Code: Select all

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();
}


User avatar
Freeheeler19
 
Posts: 24
Joined: Tue Dec 17, 2019 12:19 pm

Re: neopixel with joystick

Post by Freeheeler19 »

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

#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();
}

User avatar
adafruit_support_bill
 
Posts: 88142
Joined: Sat Feb 07, 2009 10:11 am

Re: neopixel with joystick

Post by adafruit_support_bill »

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

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();
}

User avatar
Freeheeler19
 
Posts: 24
Joined: Tue Dec 17, 2019 12:19 pm

Re: neopixel with joystick

Post by Freeheeler19 »

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

User avatar
adafruit_support_bill
 
Posts: 88142
Joined: Sat Feb 07, 2009 10:11 am

Re: neopixel with joystick

Post by adafruit_support_bill »

Code: Select all

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();
}


User avatar
Freeheeler19
 
Posts: 24
Joined: Tue Dec 17, 2019 12:19 pm

Re: neopixel with joystick

Post by Freeheeler19 »

Thats it! Cool.
Thanks for your support!

Cyril

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”