Reed switch controlling the colour of an RGB LED Pixel Ring (With a UNO)

Good evening,

My latest project is for a level indicator on my chicken coops water supply. I am using an Arduino UNO, a reed float switch and a 16 pixel RGB LED Ring (adafruit Neopixel). I am wanting to light the RGB LED ring a different colour depending on if switch is open or closed.

I have the LED ring working. I also can have the switch working on its own. But not controlling the LED.
I have no idea what to do next.

I am hoping someone can help? It seems a simple task for the arduino to do but I am very new at this so I’m struggling a little bit.

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

#define PIN 6

const int buttonPin2 = 2;     // the number of the floatswitch pin 2
const int ledPin13 =  13;      // the number of the LED pin 3
int buttonState = 0;  

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.

void setup() {
// initialize the LED pin as an output:
  pinMode(ledPin13, OUTPUT);

pinMode(buttonPin2, INPUT);
    // initialize the LED pin as an output:
  strip.begin();
  strip.setBrightness(10);
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  // Some example procedures showing how to display to the pixels:
  colorWipe(strip.Color(255, 0, 0), 50); // Red
colorWipe(strip.Color(0, 0, 0), 50); // Blank/off
//colorWipe(strip.Color(0, 0, 0, 255), 50); // White RGBW

}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }  
  
// read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin2);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin13, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin13, LOW);
  
  }  }

At the moment i have the ring “colour chasing” red at the right speed. And i have the reed switch controlling the LED on Pin 13. I need the arduino to turn the ring green when the switch is made and go back to red when the switch is open.

Thank you all in advance.

Hi

I suggest moving the button/reed switch code from colorWipe() to loop(). That way, you can pass a different colour to colorWipe() when it is pressed vs. not pressed.

Have you considered using an accelerometer module rather than the tilt switch? That would allow you to use the led ring to indicate the direction and angle of the tilt. MPU-6050 units seem to be cheapest on eBay.

Paul

I think I've tried that. I keep getting a problem with the colourwipe? Any chance you could walk me through it. Baby Steps :-).

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif

#define PIN 6

const int buttonPin2 = 2;     // the number of the floatswitch pin 2
const int ledPin13 =  13;      // the number of the LED pin 3
int buttonState = 0;

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin13, OUTPUT);

  pinMode(buttonPin2, INPUT);
  // initialize the LED pin as an output:
  strip.begin();
  strip.setBrightness(10);
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {

  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin2);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    colorWipe(strip.Color(255, 0, 0), 50); // Red
  } else {
    colorWipe(strip.Color(0, 255, 0), 50); // Green?
  }

  colorWipe(strip.Color(0, 0, 0), 50); // Blank/off

}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for (uint16_t i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}