Hi there,
I got a 16-Neopixel-ring from adafruit and the thing I want to do with it seems to be just too simple to find it via google: I just want 4 red Pixels in a row spinning around in a circle as long as one button is pressed. Then I would like to implement a second button to activate the same function, but with the pixels spinning in a different color.
So far I managed to simplify the code from this site (Arduino Code | Kaleidoscope Eyes (Trinket-Powered NeoPixel LED Ring Goggles) | Adafruit Learning System ), so that I have now 2 Pixels on each side of the ring spinning around:
#include <Adafruit_NeoPixel.h>
#define PIN 7
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(16, PIN);
uint8_t offset = 0; // Position of spinny eyes
uint32_t color = 0xFF0000; // Start red
void setup() {
pixels.begin();
pixels.setBrightness(85); // 1/3 brightness
}
void loop() {
for(int i=0; i<16; i++) {
uint32_t c = 0;
if(((offset + i) & 7) < 2) c = color;
pixels.setPixelColor( i, c); // First eye
}
pixels.show();
offset++;
delay(80);
}
My arduino-coding experience is just not good enough to reduce it to just one pixel and add the button function (like here: Adafruit_NeoPixel/buttoncycler.ino at master · adafruit/Adafruit_NeoPixel · GitHub ). It would be really great if someone could help me with this!
I just want 4 red Pixels in a row spinning around in a circle
My arduino-coding experience is just not good enough to reduce it to just one pixel
Do you want 4 LEDs lit at teh same time or 1 ?
Riva
April 25, 2016, 1:11pm
3
Untested but maybe something like this...
#include <Adafruit_NeoPixel.h>
#define PixelCount 16
#define PixelPin 8
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PixelCount, PixelPin, NEO_GRB + NEO_KHZ800);
void setup(){
pixels.begin();
}
void loop(){
for(byte x = 0; x < 20; x++){
rotatePixels(true);
delay(500);
}
for(byte x = 0; x < 20; x++){
rotatePixels(false);
delay(500);
}
}
void rotatePixels(bool pixDirection){
static char currentPos = PixelCount;
if(pixDirection == true){
currentPos++;
}
else {
currentPos--;
}
pixels.setPixelColor((currentPos - 1) % PixelCount, 0, 0, 0);
pixels.setPixelColor((currentPos + 0) % PixelCount, 255, 0, 0);
pixels.setPixelColor((currentPos + 1) % PixelCount, 255, 0, 0);
pixels.setPixelColor((currentPos + 2) % PixelCount, 255, 0, 0);
pixels.setPixelColor((currentPos + 3) % PixelCount, 255, 0, 0);
pixels.setPixelColor((currentPos + 4) % PixelCount, 0, 0, 0);
pixels.show();
}
Thanks for the quick replys!
@UKHeliBob
That was an typing error, sorry I meant 4 pixels in a row.
@Riva
Thanks a lot for the code! It is working, but still a bit buggy. There are 4 pixels in a row spinning, but they are changing their direction every 20 pixels.
they are changing their direction every 20 pixels.
for(byte x = 0; x < 20; x++){
rotatePixels(false);
delay(500);
}
Comment out this for loop.
Riva
April 27, 2016, 9:25am
6
balduin:
@Riva
Thanks a lot for the code! It is working, but still a bit buggy. There are 4 pixels in a row spinning, but they are changing their direction every 20 pixels.
Sorry, misread your initial post and thought you wanted direction to reverse when second button pressed but now I see you want a different colour.
#include <Adafruit_NeoPixel.h>
#define PixelCount 16
#define PixelPin 8
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PixelCount, PixelPin, NEO_GRB + NEO_KHZ800);
void setup(){
pixels.begin();
}
void loop(){
for(byte x = 0; x < 20; x++){
rotatePixels(true);
delay(500);
}
for(byte x = 0; x < 20; x++){
rotatePixels(false);
delay(500);
}
}
void rotatePixels(bool pixColour){
static char currentPos = PixelCount;
uint32_t colour = 0xFF0000; // Default to Red
currentPos++;
if(pixColour == true){
colour = 0xFF00; // Green
}
pixels.setPixelColor((currentPos - 1) % PixelCount, 0);
pixels.setPixelColor((currentPos + 0) % PixelCount, colour);
pixels.setPixelColor((currentPos + 1) % PixelCount, colour);
pixels.setPixelColor((currentPos + 2) % PixelCount, colour);
pixels.setPixelColor((currentPos + 3) % PixelCount, colour);
pixels.show();
}
@UKHeliBob
I tried this aswell - it is working for 8 rounds, but then the pixel-row comes to a halt and one pixel remains blinking. After some seconds the spinning starts again for the next 8 rounds and so on.
@Riva
Good idea to put the colour in a separate variable, so it has not to be repeated in every single pixel-declaration! But I think it is better to focus on the simple spinner at first and do the button-control thing as the next step.
So I combined both of your codes - you can see it below. But the Problem with the pixel-row stopping and blinking after 8 rounds is still there. Anybody got an idea how to fix this?
#include <Adafruit_NeoPixel.h>
#define PixelCount 16
#define PixelPin 8
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PixelCount, PixelPin, NEO_GRB + NEO_KHZ800);
void setup(){
pixels.begin();
pixels.setBrightness(85);
}
void loop(){
for(byte x = 0; x < 20; x++){
rotatePixels(true);
delay(50);
}
}
void rotatePixels(bool){
static char currentPos = PixelCount;
uint32_t colour = 0xFF0000;
currentPos++;
pixels.setPixelColor((currentPos - 1) % PixelCount, 0);
pixels.setPixelColor((currentPos + 0) % PixelCount, colour);
pixels.setPixelColor((currentPos + 1) % PixelCount, colour);
pixels.setPixelColor((currentPos + 2) % PixelCount, colour);
pixels.setPixelColor((currentPos + 3) % PixelCount, colour);
pixels.show();
}
Riva
April 27, 2016, 5:09pm
8
Try this...
#include <Adafruit_NeoPixel.h>
#define PixelPin 8
#define PixelCount 16
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PixelCount, PixelPin, NEO_GRB + NEO_KHZ800);
void setup(){
pixels.begin();
}
void loop(){
for(byte x = 0; x < 20; x++){
rotatePixels(true);
delay(500);
}
for(byte x = 0; x < 20; x++){
rotatePixels(false);
delay(500);
}
}
void rotatePixels(bool pixColour){
static byte currentPos = 0;
uint32_t colour = 0xFF0000; // Default to Red
if(++currentPos >= PixelCount){
currentPos = 0;
}
if(pixColour == true){
colour = 0xFF00; // Green
}
pixels.setPixelColor((currentPos + PixelCount - 1) % PixelCount, 0);
pixels.setPixelColor((currentPos + PixelCount + 0) % PixelCount, colour);
pixels.setPixelColor((currentPos + PixelCount + 1) % PixelCount, colour);
pixels.setPixelColor((currentPos + PixelCount + 2) % PixelCount, colour);
pixels.setPixelColor((currentPos + PixelCount + 3) % PixelCount, colour);
pixels.show();
}
#include <Adafruit_NeoPixel.h>
#define PixelPin 8
#define PixelCount 16
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PixelCount, PixelPin, NEO_GRB + NEO_KHZ800);
void setup(){
pixels.begin();
}
uint32_t colour = 0xFF0000; // Default to Red
void loop(){
// int start = (millis()/1000) % PixelCount; // one pixel per second
int start = (millis()%1000) * PixelCount / 1000L; // one rev per second
for(int i = 0; i<4; i++) {
pixels.setPixelColor((i+start) % PixelCount, colour);
}
for(int i = 4; i<PixelCount; i++) {
pixels.setPixelColor((i+start) % PixelCount, 0);
}
pixels.show();
}
Thanks for the new approach, Paul! Works perfect!
I should also have had a closer look to Rivas code code in the first place, sorry. I was just a bit confused, as the pixels where changing their color between red and green in every round. But it stays red after removing these lines:
if(pixColour == true){
colour = 0xFF00; // Green
}
So now I got even two versions of a simple spinner - first step taken!
The next thing I would like to do is to control the spinner with two buttons.
No button pressed: Nothing happens
While Button 1 is pressed: Spinning pixels in red
While Button 2 is pressed: Spinning pixels in green
I am already tinkering around with the neopixel buttoncycler-example but did not get very far with it.
Riva
April 29, 2016, 6:41am
11
Just connect a couple of switches. One side of switch to pins 3 & 4 and the other side to GND
Code untested...
#include <Adafruit_NeoPixel.h>
#define PixelPin 7
#define PixelCount 16
#define redButton 3
#define greenButton 4
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PixelCount, PixelPin, NEO_GRB + NEO_KHZ800);
void setup(){
pixels.begin();
pinMode(redButton,INPUT_PULLUP); // Configure red button
pinMode(greenButton,INPUT_PULLUP); // Configure green button
}
void loop(){
while(digitalRead(redButton) == LOW){ // Red button pressed?
rotatePixels(0xFF0000); // Rotate Red colour
delay(100); // Delay a bit (reduce value to speed up rotate speed)
} // Back to while to see if button still pressed
while(digitalRead(greenButton) == LOW){
rotatePixels(0xFF00);
delay(100);
}
}
void rotatePixels(uint32_t pixColour){
static byte currentPos = 0;
if(++currentPos >= PixelCount){
currentPos = 0;
}
pixels.setPixelColor((currentPos + PixelCount - 1) % PixelCount, 0); // Clear previous last pixel
pixels.setPixelColor((currentPos + PixelCount + 0) % PixelCount, pixColour); // Set the next 4 pixels
pixels.setPixelColor((currentPos + PixelCount + 1) % PixelCount, pixColour);
pixels.setPixelColor((currentPos + PixelCount + 2) % PixelCount, pixColour);
pixels.setPixelColor((currentPos + PixelCount + 3) % PixelCount, pixColour);
pixels.show(); // Show pixels
}
Almost perfect!
After pushing a button for the first time and then releasing it, the pixels remain lit and just stop spinning. If I push a button again, they proceed spinning from the position they stopped at. This is a nice effect, but I prefer to have all pixels off if no button is pressed. So I added this to the code:
while(digitalRead(greenButton && redButton) == HIGH){
rotatePixels(0x000000);
delay(100);
}
It works, but this way the pixels keep on spinning "dark" if no button is pressed. So after pressing a button again, they do not proceed from the point at which they turned dark. So how can I just stop the rotation and switch the pixels off while no button is pressed?
Riva
April 30, 2016, 6:55am
13
Both versions untested...
Ramping up/down
#include <Adafruit_NeoPixel.h>
#define PixelPin 7
#define PixelCount 16
#define redButton 3
#define greenButton 4
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PixelCount, PixelPin, NEO_GRB + NEO_KHZ800);
void setup(){
pixels.begin();
pinMode(redButton,INPUT_PULLUP); // Configure red button
pinMode(greenButton,INPUT_PULLUP); // Configure green button
}
void loop(){
while(digitalRead(redButton) == LOW){ // Red button pressed?
rotatePixels(0xFF0000, true); // Rotate Red colour
delay(100); // Delay a bit (reduce value to speed up rotate speed)
} // Back to while to see if button still pressed
while(digitalRead(greenButton) == LOW){
rotatePixels(0xFF00, true);
delay(100);
}
rotatePixels(0x00, false); // Clear only
delay(100);
}
void rotatePixels(uint32_t pixColour, bool onOff){
static byte currentPos = 0;
pixels.setPixelColor((currentPos + 0) % PixelCount, 0); // Clear previous last pixel
if(++currentPos >= PixelCount){
currentPos = 0;
}
if(onOff == true) pixels.setPixelColor((currentPos + 3) % PixelCount, pixColour);
pixels.show(); // Show pixels
}
Dead stop/start
#include <Adafruit_NeoPixel.h>
#define PixelPin 7
#define PixelCount 16
#define redButton 3
#define greenButton 4
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PixelCount, PixelPin, NEO_GRB + NEO_KHZ800);
void setup(){
//Serial.begin(115200);
pixels.begin();
pinMode(redButton,INPUT_PULLUP); // Configure red button
pinMode(greenButton,INPUT_PULLUP); // Configure green button
}
void loop(){
while(digitalRead(redButton) == LOW){ // Red button pressed?
rotatePixels(0xFF0000, true); // Rotate Red colour
delay(100); // Delay a bit (reduce value to speed up rotate speed)
} // Back to while to see if button still pressed
while(digitalRead(greenButton) == LOW){
rotatePixels(0xFF00, true);
delay(100);
}
for(byte x = 0; x < PixelCount; x++){
pixels.setPixelColor(x % PixelCount, 0);
}
pixels.show(); // Show pixels
}
void rotatePixels(uint32_t pixColour, bool onOff){
static byte currentPos = 0;
pixels.setPixelColor((currentPos + 0) % PixelCount, 0); // Clear previous last pixel
if(++currentPos >= PixelCount){
currentPos = 0;
}
pixels.setPixelColor((currentPos + 0) % PixelCount, pixColour); // Set the next 4 pixels
pixels.setPixelColor((currentPos + 1) % PixelCount, pixColour);
pixels.setPixelColor((currentPos + 2) % PixelCount, pixColour);
pixels.setPixelColor((currentPos + 3) % PixelCount, pixColour);
pixels.show(); // Show pixels
}
Yep, thats it!
Thank you very much for your help - I am happy now
As reading midi-messages is also part of my actual project, I just came up with the idea to include the midi-thing directly into this code. But I see this as a different topic, so I opened an extra post for it: Neopixel-ring controlled by MIDI