Greetings all. I've got a circuit where an arduino nano reads analog inputs from three potentiometers, converts these readings into a scale of 0-255, and displays them on an LCD 1602A IC2. The three potentiometers are controlling the red, green, and blue values for a strip of addressable LEDs (NeoPixels). The circuit is designed to let me choose color values for LED projects without having to upload values to the board, adjust, and then repeat continuously.
When the circuit first powers up, the red, green, and blue values on the LCD are all zero, and when turned up to maximum they are all 255. This works great. But when I turn them back down to zero, the numbers on the LCD don't fall all the way to zero, which looks sloppy.
Here's the code:
#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int red, green, blue = 0;
//I2C pins declaration
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIN 2
#define NUMPIXELS 5
//Initialize the neopixel strip
Adafruit_NeoPixel strip(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
//Set the pins connected to the potentiometer as inputs
pinMode(A0, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
//LCD
lcd.begin(16, 2);
lcd.backlight();
lcd.setCursor(0,0); //Defining positon to write from first row,first column
lcd.print("Home again,"); //You can write 16 Characters per line
delay(1000);
lcd.setCursor(0,1);
lcd.print("home again");
delay(1000);
lcd.setCursor(0,0);
lcd.print("jiggity jig.");
lcd.setCursor(0,1);
lcd.print(" ");
delay(2000);
lcd.clear();
delay(500);
lcd.setCursor(0,0);
lcd.print("Good evening,");
lcd.setCursor(0,1);
lcd.print("Thomas.");
delay(3000);
lcd.clear();
//NeoPixels
strip.begin();
strip.show(); // Initialize all pixels to 'off'
rainbowCycle(1);//Flash rainbows at the begining
}
void loop() {
//Read and store the potentiometer values
//We are scaling them from a 12bit scale to an 8 bit scale
red = map(analogRead(A0), 0, 4095, 0, 255);
green = map(analogRead(A2), 0, 4095, 0, 255);
blue = map(analogRead(A3), 0, 4095, 0, 255);
//set colors of all the eight neopixles
for (int pixel = 0; pixel < 16; pixel++)
{
strip.setPixelColor(pixel, red, green, blue);
}
strip.show();//Update the strip with new color values
delay(50);
//LCD code
lcd.setCursor(0,0); //Defining positon to write from first row,first column.
lcd.print("Red ");
lcd.print(analogRead(A0)/4);
lcd.setCursor(8, 0);
lcd.print("Grn ");
lcd.print(analogRead(A2)/4);
lcd.setCursor(0, 1);
lcd.print("Blue ");
lcd.print(analogRead(A3)/4);
}
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}
// Helper function for rainbows
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
I should also mention that some of the LCD readouts jump outside of the 0-255 range, seemingly at random—my gut tells me these problems are related. Any ideas how to fix these issues?