Hi guys, I'm doing my first project that uses Arduino to control a 5050RGB LED light strip with MOSFET.
Here is the code:
#define ITERATION 15
#define DELAY 2
#define FAULT_DELAY 100
#define MAX_BVAL 255
#define rPin 3
#define gPin 5
#define bPin 6
int NumberSounds = 0;
int SoundPin = 2;
int RawValue = 0;
int NumberClaps = 0;
int LightOn = 0;
unsigned long SoundDetectedTime = 0;
unsigned long PreviousSoundDetectedTime = 0;
int UniqueClapMinTime = 100;
int LEDPin = 13;
unsigned long PreviousClapTime = 0;
unsigned long CurrentClapTime = 0;
unsigned long MaxTimeBetweenClaps = 2000;
unsigned long lastMillisec = 0;
unsigned long current_time = 0;
unsigned long elapsed_time = 0;
int sample = 0;
long totalValue = 0;
int count = 0;
int counter = 1;
int aState;
int aLastState;
int rBr = 0;
int bBr = 0;
int gBr = 0;
boolean state = false;
int lastClap = 2;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(rPin, OUTPUT);
pinMode(gPin, OUTPUT);
pinMode(bPin, OUTPUT);
pinMode(SoundPin, INPUT);
pinMode(LEDPin, OUTPUT);
}
int IsSoundPartOfUniqueClap()
{
int result = 0;
unsigned long ElapsedTime =
SoundDetectedTime -
PreviousSoundDetectedTime;
if (ElapsedTime >= UniqueClapMinTime)
{
result = 1;
}
return result;
}
int CheckTurnOnOffLight()
{
int result = 0;
unsigned long ElapsedTime =
CurrentClapTime - PreviousClapTime;
if (ElapsedTime <= MaxTimeBetweenClaps)
{
if (NumberClaps == 2)
{
result = 1;
NumberClaps = 0;
}
}
else
{
NumberClaps = 1;
}
return result;
}
void off()
{
setRed(0);
setGreen(0);
setBlue(0);
}
void setRed(int br)
{
int accu = br > rBr ? 1 : -1;
for (rBr; rBr != br; rBr += accu)
{
analogWrite(rPin, rBr);
delay(DELAY);
}
}
void setGreen(int br)
{
int accu = br > gBr ? 1 : -1;
for (gBr; gBr != br; gBr += accu)
{
analogWrite(gPin, gBr);
delay(DELAY);
}
}
void setBlue(int br)
{
int accu = br > bBr ? 1 : -1;
for (bBr; bBr != br; bBr += accu)
{
analogWrite(bPin, bBr);
delay(DELAY);
}
}
void hsv(int value)
{
if (value <= 60)
{
setRed(255);
setBlue(0);
setGreen(map(value, 0, 60, 0, 255));
}
else if (value > 60 && value <= 120)
{
setGreen(255);
setBlue(0);
setRed(255 - map(value, 60, 120, 0, 255));
}
else if (value > 120 && value <= 180)
{
setRed(0);
setGreen(255);
setBlue(map(value, 120, 180, 0, 255));
}
else if (value > 180 && value <= 240)
{
setRed(0);
setBlue(255);
setGreen(255 - map(value, 180, 240, 0, 255));
}
else if (value > 240 && value <= 300)
{
setBlue(255);
setGreen(0);
setRed(map(value, 240, 300, 0, 255));
}
else if (value > 300 && value <= 360)
{
setRed(255);
setGreen(0);
setBlue(255 - map(value, 300, 360, 0, 255));
}
}
void loop()
{
RawValue = digitalRead(SoundPin);
if (RawValue == 0)
{
NumberSounds++;
// Process raw data for claps
PreviousSoundDetectedTime =
SoundDetectedTime;
SoundDetectedTime = millis();
if (IsSoundPartOfUniqueClap())
{
NumberClaps++;
// Update Clap Times
PreviousClapTime =
CurrentClapTime;
CurrentClapTime = millis();
// Turn Light ON/OFF as needed
if (CheckTurnOnOffLight())
{
LightOn = ~LightOn;
if (LightOn)
{
state = true;
}
else
{
state = false;
}
}
}
}
if (state)
{
int sensorValue = analogRead(A0);
totalValue += sensorValue;
if (count++ == ITERATION)
{
hsv(map(totalValue / ITERATION, 0, 1023, 0, 255));
totalValue = 0;
count = 1;
}
digitalWrite(13, HIGH);
}
else
{
off();
digitalWrite(13, LOW);
}
}
I'm following this youtube video for guidance and the led wiring part is identical.
LED video
This is the schematic I created myself:
I've tested it on a breadboard and It works fine, after that, I make a board that looks like this:
But when I power it on, The light should be off completely but it still shows a dim light:
I'm pretty sure all my wirings are done correctly and tested connectivity.
and also, the middle MOTFES seems to not work right because when I give 4.0 voltage to the gate, the drain only gets around 5v.
Wonder why is this happening, could anyone help me, please?