I'm making a small water level indicator using a pressure transducer and an Arduino NANO (Chinese).
For the indicator I'm using a WS2812B RGB LED Strip and the FastLED by Daniel Garcia Library;
I'm also using a 16x2 LCD Display with a I2C backpack with the LiquidCrystal_I2C.h by FMalpartida library.
I made a small PCB with terminal block connectors.
The problem, sometimes the arduino hangs, it freezes, no serial connection, no updating the LCD display, nor the LED strip, sometimes it may resume sometimes it just stays frozen until a hard reset.
I believe it's the I2C connection hanging because it seems to work fine without it, but i cannot be sure because it's not always that happens, although it seems to happen more often with the LCD display and the I2C backpack connected.
Attached there's a picture of how everything is connected, both sides of the PCB, and here's my code.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <FastLED.h>
#define LED_PIN 3
#define NUM_LEDS 8
CRGB leds[NUM_LEDS];
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
const uint8_t sensorPin = A0;
void setup() {
FastLED.addLeds<WS2812, LED_PIN, RGB>(leds, NUM_LEDS);
FastLED.setBrightness(25);
leds[0] = CRGB(0 , 0 , 255);
leds[1] = CRGB(0 , 0 , 255);
leds[2] = CRGB(0 , 0 , 255);
leds[3] = CRGB(0 , 0 , 255);
leds[4] = CRGB(0 , 0 , 255);
leds[5] = CRGB(0 , 0 , 255);
leds[6] = CRGB(0 , 0 , 255);
leds[7] = CRGB(0 , 0 , 255);
FastLED.show();
lcd.begin(16,2);
lcd.backlight();
lcd.setCursor(2,0);
lcd.print("TORNOS LEON");
lcd.setCursor(0,1);
lcd.print("PRESION:#.## PSI");
pinMode(sensorPin, INPUT);
/*Serial.begin(9600);*/
}
void loop() {
float voltage = 5.0/1024.0 * (analogRead(sensorPin) + 0.5);
float pressure = 174.05 / 4.0 * (voltage - 0.5);
pressure = constrain( pressure, 0, 174.05);
if ( pressure >= 4.80 ) {
leds[0] = CRGB(255 , 0 , 0);
leds[1] = CRGB(255 , 255 , 0);
leds[2] = CRGB(255 , 255 , 0);
leds[3] = CRGB(255 , 255 , 0);
leds[4] = CRGB(0 , 255 , 0);
leds[5] = CRGB(0 , 255 , 0);
leds[6] = CRGB(0 , 255 , 0);
leds[7] = CRGB(0 , 255 , 0);
} else if ( pressure >= 4.59 && pressure < 4.80 ) {
leds[0] = CRGB(255 , 0 , 0);
leds[1] = CRGB(255 , 255 , 0);
leds[2] = CRGB(255 , 255 , 0);
leds[3] = CRGB(255 , 255 , 0);
leds[4] = CRGB(0 , 255 , 0);
leds[5] = CRGB(0 , 255 , 0);
leds[6] = CRGB(0 , 255 , 0);
leds[7] = CRGB(0 , 0 , 0);
} else if ( pressure >= 4.38 && pressure < 4.59 ) {
leds[0] = CRGB(255 , 0 , 0);
leds[1] = CRGB(255 , 255 , 0);
leds[2] = CRGB(255 , 255 , 0);
leds[3] = CRGB(255 , 255 , 0);
leds[4] = CRGB(0 , 255 , 0);
leds[5] = CRGB(0 , 255 , 0);
leds[6] = CRGB(0 , 0 , 0);
leds[7] = CRGB(0 , 0 , 0);
} else if ( pressure >= 4.17 && pressure < 4.38 ) {
leds[0] = CRGB(255 , 0 , 0);
leds[1] = CRGB(255 , 255 , 0);
leds[2] = CRGB(255 , 255 , 0);
leds[3] = CRGB(255 , 255 , 0);
leds[4] = CRGB(0 , 255 , 0);
leds[5] = CRGB(0 , 0 , 0);
leds[6] = CRGB(0 , 0 , 0);
leds[7] = CRGB(0 , 0 , 0);
} else if ( pressure >= 3.96 && pressure < 4.17 ) {
leds[0] = CRGB(255 , 0 , 0);
leds[1] = CRGB(255 , 255 , 0);
leds[2] = CRGB(255 , 255 , 0);
leds[3] = CRGB(255 , 255 , 0);
leds[4] = CRGB(0 , 0 , 0);
leds[5] = CRGB(0 , 0 , 0);
leds[6] = CRGB(0 , 0 , 0);
leds[7] = CRGB(0 , 0 , 0);
} else if ( pressure >= 3.75 && pressure < 3.96 ) {
leds[0] = CRGB(255 , 0 , 0);
leds[1] = CRGB(255 , 255 , 0);
leds[2] = CRGB(255 , 255 , 0);
leds[3] = CRGB(0 , 0 , 0);
leds[4] = CRGB(0 , 0 , 0);
leds[5] = CRGB(0 , 0 , 0);
leds[6] = CRGB(0 , 0 , 0);
leds[7] = CRGB(0 , 0 , 0);
} else if ( pressure >= 3.54 && pressure < 3.75 ) {
leds[0] = CRGB(255 , 0 , 0);
leds[1] = CRGB(255 , 255 , 0);
leds[2] = CRGB(0 , 0 , 0);
leds[3] = CRGB(0 , 0 , 0);
leds[4] = CRGB(0 , 0 , 0);
leds[5] = CRGB(0 , 0 , 0);
leds[6] = CRGB(0 , 0 , 0);
leds[7] = CRGB(0 , 0 , 0);
} else if ( pressure < 3.54 ) {
leds[0] = CRGB(255 , 0 , 0);
leds[1] = CRGB(0 , 0 , 0);
leds[2] = CRGB(0 , 0 , 0);
leds[3] = CRGB(0 , 0 , 0);
leds[4] = CRGB(0 , 0 , 0);
leds[5] = CRGB(0 , 0 , 0);
leds[6] = CRGB(0 , 0 , 0);
leds[7] = CRGB(0 , 0 , 0);
}
lcd.clear();
lcd.setCursor(2,0);
lcd.print("TORNOS LEON");
lcd.setCursor(0,1);
lcd.print("PRESION:");
lcd.print(pressure);
lcd.print(" PSI");
FastLED.show();
FastLED.delay(250);
/*printStatus(voltage, pressure);*/
}
/*void printStatus(float voltage, float pressure) {*/
/* Serial.print("Status: ");*/
/* Serial.print(voltage);*/
/* Serial.print(" v | ");*/
/* Serial.print(pressure);*/
/* Serial.println(" psi");*/
/*}*/
I've already changed the whole LCD display and I2C Backpack, the problem is not as often but still happens.