Hello, a newbie here. So I have a couple of problems that have been killing me for hours now and Im desperate. You see, I managed to connect my Arduino to my LED Strip (12V). I set up a program, where I can change the brightness of each color (rgb) depending on Serial Communication. With a button I attached, I could change it to the next color. So I put a number into Serial, and it would modify the brightness on my LED Strip, to make all sorts of colors. Okay I got that program working nice and swiftly, and I decided it would be cooler to incorporate an IR receiver so that instead of typing on Serial, I could just put the number on my IR remote, and the brightness would change. So when I was working on that, I tested it a couple of times to make sure I was doing everything correctly. Then I discovered the RED on my LED strip has suddenly stopped Working. I change to green and it works great, I change it to blue, works great. But Red doesn't work, and the weird part is that It does work If I put 255 as brightness. but anything below that, it basically makes it 0. I know that must sound pretty confusing. Then I started tinkering around with some of my code. And I realized erasing one line of code solves everything.
Im using 12V LEDS and IRFZ44N MOSFETS if it somehow helps.
Here's my code.
#include <LiquidCrystal_I2C.h>
#include <IRremote.h>
#define BLUE_LED 9
#define RED_LED 11
#define GREEN_LED 10
#define CHANGE_BUTTON 2
#define IR_PIN 5
// Starts up Liquid Crystal
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Button debounce variables
unsigned long lastButtonChange = millis();
unsigned long debounceDelay = 60;
byte buttonState = LOW;
// Variable that gives current Color the Serial is changing
int currentColor = 1;
// Arrays for IR buttons
const byte buttonCode[10] = { 74, 22, 12, 24, 94, 8, 28, 90, 66, 82 };
const byte digit[10] = { 9, 0, 1, 2, 3, 4, 5, 6, 7, 8 };
// Button for enter
const byte hash = 64; // # <-----<<<<< use a button and it's code from your remote
//To get number
unsigned int myNumber;
// ************************************************************************************************************************************************
// ****************************************************************** Color LEDS ******************************************************************
// Function to change current color selected brightness
void colorCurrentColor(int value) {
switch (currentColor) {
case 1:
{
analogWrite(RED_LED, value);
break;
}
case 2:
{
analogWrite(GREEN_LED, value);
break;
}
case 0:
{
analogWrite(BLUE_LED, value);
break;
}
}
} // END of colorCurrentColor()
// **************************************************************************************************************************************************
// ****************************************************************** COLOR ON LCD ******************************************************************
// Function for LCD
void setCurrentColorOnLCD() {
// Clearing top half
for (int i = 0; i < 16; i++) {
lcd.setCursor(i, 0);
lcd.print(" ");
}
switch (currentColor) {
case 1:
{
lcd.setCursor(0, 0);
lcd.print("Current: RED");
break;
}
case 2:
{
lcd.setCursor(0, 0);
lcd.print("Current: GREEN");
break;
}
case 3:
{
lcd.setCursor(0, 0);
lcd.print("Current: BLUE");
break;
}
}
} // END of setCurrentColorOnLCD()
// *************************************************************************************************************************************************
// ****************************************************************** Initial LCD ******************************************************************
// Little menu for when you start up the program
void initialLCDScreen() {
// TOP PART
lcd.setCursor(0, 0);
lcd.print("Current: RED");
//BOTTOM PART
lcd.setCursor(0, 1);
lcd.print("R0 G0 B0 ");
} // END of intialLCDScreen()
// ************************************************************************************************************************************************
// ****************************************************************** RGB ON LCD ******************************************************************
// Prints out the rgb values on the LCD screen
void rgbValuesOnLCD(int value) {
switch (currentColor) {
case 1:
{
for (int i = 1; i < 5; i++) {
lcd.setCursor(i, 1);
lcd.print(" ");
}
lcd.setCursor(1, 1);
lcd.print(value);
break;
}
case 2:
{
for (int i = 6; i < 10; i++) {
lcd.setCursor(i, 1);
lcd.print(" ");
}
lcd.setCursor(6, 1);
lcd.print(value);
break;
}
case 0:
{
for (int i = 11; i < 16; i++) {
lcd.setCursor(i, 1);
lcd.print(" ");
}
lcd.setCursor(11, 1);
lcd.print(value);
break;
}
}
} // End of rgbValuesOnLCD()
// **************************************************************************************************************************************************
// ****************************************************************** CHECK IR CODE *****************************************************************
// Checks What value IR remote comes up with
void checkIRcode() {
//**************************************
for (byte x = 0; x < 10; x++) {
//***************
if (IrReceiver.decodedIRData.command == buttonCode[x]) {
Serial.print("Button = ");
Serial.print(digit[x]);
myNumber = myNumber * 10 + digit[x];
break;
}
}
Serial.println("");
//***************
if (IrReceiver.decodedIRData.command == hash) {
Serial.print("My number = ");
Serial.println(myNumber);
Serial.println("");
rgbValuesOnLCD(myNumber);
colorCurrentColor(myNumber);
myNumber = 0;
}
//needed to block repeat codes
IrReceiver.decodedIRData.command = 0;
} //END of checkIRcode()
// *******************************************************************************************************************************************
// ****************************************************************** SETUP ******************************************************************
void setup() {
// Serial Functions
Serial.begin(115200);
Serial.setTimeout(50);
//LCD intilize
lcd.init();
lcd.backlight();
// Pins to add
pinMode(CHANGE_BUTTON, INPUT);
pinMode(BLUE_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
initialLCDScreen();
//Starts IR remote
IrReceiver.begin(IR_PIN); // <----- Erasing this fixes it
} // END of setup()
// ******************************************************************************************************************************************
// ****************************************************************** LOOP ******************************************************************
void loop() {
// Button Debounce
unsigned long timeNow = millis();
if (timeNow - lastButtonChange > debounceDelay) {
lastButtonChange = timeNow;
byte newButtonState = digitalRead(CHANGE_BUTTON);
if (buttonState != newButtonState) {
buttonState = newButtonState;
if (buttonState == HIGH) {
currentColor += 1;
setCurrentColorOnLCD();
if (currentColor == 3) {
currentColor = 0;
}
}
}
}
// Check Serial Info
if (Serial.available() > 0) {
int data = Serial.parseInt();
if (data > 255) {
data = 255;
} else if (data < 0) {
data = 0;
}
colorCurrentColor(data);
rgbValuesOnLCD(data);
}
// IR Receiver
if (IrReceiver.decode()) {
//Enable receiving of the next value
IrReceiver.resume();
}
//have we received a new IR code form the remote ?
if (IrReceiver.decodedIRData.command != 0) {
checkIRcode();
}
} //END of loop()
The particular line of code im talking about is in setup(). Line 197.
Hope someone can help me, thanks.
