How to add an if function that results in the rainbow effect

I am presently working on a project to have 3 potentiometers which control RGB for 3 NeoPixel Rings. Using my code below, how could I include the following:

  1. Set minimum value of 5 on potentiometer and any values under that will result in a 0.
  2. Add an IF function so that if all 3 potentiometer are set to 0, it results in a rainbow effect for the NeoPixel Rings
#include <Adafruit_NeoPixel.h>

#define PIN 2

Adafruit_NeoPixel ring = Adafruit_NeoPixel(72, PIN, NEO_GRB + NEO_KHZ800);


int sensorValue = A0;
int sensorValue2 = A1;
int sensorValue3 = A2;
int currentColourValue;
int currentColourValue2;
int currentColourValue3;


void setup() {
  ring.begin();
  ring.show(); // Initialize all pixels to 'off'

}

void loop() { 
 currentColourValue = (0 + map( analogRead(sensorValue), 0, 1024, 0, 255 ) );
 currentColourValue2 = (0 + map( analogRead(sensorValue2), 0, 1024, 0, 255 ) );
 currentColourValue3 = (0 + map( analogRead(sensorValue3), 0, 1024, 0, 255 ) );
 
 colorWipe(ring.Color(currentColourValue, currentColourValue2, currentColourValue3), 50);
 
 }

void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<ring.numPixels(); i++) {
      ring.setPixelColor(i, c);
      ring.show();
      delay(0);
  }
}

pseudo-code:

if (pot_value < 5) pot_value = 0;
 currentColourValue = (pot_value, 0, 1024, 0, 255 );
if ((currentColourValue == 0) && (currentColourValue2 == 0) && (currentColourValue3 == 0)) 
  { 
    RainBow();
}
1 Like

currentColourValue =map (pot_value, 0, 1023, 0, 255 );

or
currentColourValue = pot_value/4;
or
currentColourValue = pot_value>>2;

2 Likes

oops ^) Thank you

Awesome thank you!

currentColourValue = map (pot_value, 0, 1024, 0, 256 ); will give the same value as pot_value/4.

Thank you for your help! I have one last question. Is there way to make it so that if I change the value to either 3 potentiometer greater than the minimum value of 5, the rainbow loop breaks before finishing the full cycle? Presently, if I change either potentiometer to a value that is not 0, the rainbow cycle needs to finish first before the new colour is set. Below is my updated code:

#include <Adafruit_NeoPixel.h>

#define PIXEL_PIN 2
#define PIXEL_COUNT 72

Adafruit_NeoPixel ring = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);


int sensorValue = A0;
int sensorValue2 = A1;
int sensorValue3 = A2;
int currentColourValue;
int currentColourValue2;
int currentColourValue3;



void setup() {
  ring.begin();
  ring.show(); // Initialize all pixels to 'off'

}

void loop() {
 if (sensorValue < 5) {sensorValue = 0;}
 if (sensorValue2 < 5) {sensorValue2 = 0;}
 if (sensorValue3 < 5) {sensorValue3 = 0;}

currentColourValue = (0 + map( analogRead(sensorValue), 0, 1024, 0, 255 ) );
currentColourValue2 = (0 + map( analogRead(sensorValue2), 0, 1024, 0, 255 ) );
currentColourValue3 = (0 + map( analogRead(sensorValue3), 0, 1024, 0, 255 ) );

 if ((currentColourValue == 0) && (currentColourValue2 == 0) && (currentColourValue3 == 0))
  {rainbow(30);}
 else {colorWipe(ring.Color(currentColourValue, currentColourValue2, currentColourValue3), 15);}
 
 }

void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<ring.numPixels(); i++) {
      ring.setPixelColor(i, c);
      ring.show();
      delay(wait);
  }
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<ring.numPixels(); i++) {
      ring.setPixelColor(i, Wheel((i+j) & 255));
    }
    ring.show();
    delay(wait);
  }
}

uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
   return ring.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return ring.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return ring.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

With your present code the answer is no.
Your rainbow() function is written in blocking style with using a delay. You should first rewrite it unblocking, using, for example, millis() method.
See the BlinkWithoutDelay tutorial for idea.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.