I'm pretty new to Arduino and this is my first project I'm working on and I'm really struggling with why my code isn't working properly.
The LED strip should be able to adjust the colour with one potentiometer that I can take out to so I don't use 3 potentiometers and just use that can adjust all colours. Then once it's on mode 4 it should keep the colour and blink repeatedly.
I'm using an Arduino Uno but once my project if finished I'll use the Arduino Nano. I don't know if I'll need to change the code but if I do I think I could do that on my own.
I just don't know why my LED strip is always stuck on off (255).
// set pin numbers:
int ledPinR = 11; // the number of the LED R pin
int ledPinG = 10; // the number of the LED G pin
int ledPinB = 9; // the number of the LED B pin
int buttonPin = 7; // the number of the push button pin
int mode = 0; //to change the mode
int bluelev = 0; //save the blue led value
int greenlev = 0; //save thegreen led value
int redlev = 0; // save the red led value
void setup() {
// initialize the LED pin as an output:
pinMode(ledPinR, OUTPUT);
pinMode(ledPinG, OUTPUT);
pinMode(ledPinB, OUTPUT);
//make the led strip white at boot
digitalWrite(ledPinR, 0);
digitalWrite(ledPinG, 0);
digitalWrite(ledPinB, 0);
}
void loop() {
// when the button is pressed changes the mode value by one
if (digitalRead(buttonPin) == LOW) {
++mode;
delay(500);
}
if (mode == 1) { //red config
int adcValue; // Define a variable to save the ADC value
// Convert analog of A0 port into digital, and work as PWM duty cycle of ledPinR port
adcValue = analogRead(A0);
analogWrite(ledPinR, map(adcValue, 0, 1023, 0, 255));
(redlev = ledPinR); // saves the red led level
}
if (mode == 2) { //green config
int adcValue; // Define a variable to save the ADC value
// Convert analog of A1 port into digital, and work as PWM duty cycle of ledPinG port
adcValue = analogRead(A1);
analogWrite(ledPinG, map(adcValue, 0, 1023, 0, 255));
(greenlev = ledPinG); // saves the green led level
}
if (mode == 3) { //Blue config
int adcValue; // Define a variable to save the ADC value
// Convert analog of A2 port into digital, and work as PWM duty cycle of ledPinB port
adcValue = analogRead(A2);
analogWrite(ledPinB, map(adcValue, 0, 1023, 0, 255));
(bluelev = ledPinB); // saves the blue led level
}
if (mode == 4) {
digitalWrite(ledPinR, 255); // switch off LED
digitalWrite(ledPinG, 255); // switch off LED
digitalWrite(ledPinB, 255); // switch off LED
digitalWrite(8, 0); // switch on LED
delay(1000); //wait 1 second
digitalWrite(ledPinR, redlev); // switch on LED
digitalWrite(ledPinG, greenlev); // switch on LED
digitalWrite(ledPinB, bluelev); // switch on LED
digitalWrite(8, 225); // switch on LED
delay(1000); //wait 1 second
}
if (mode == 5) {
mode -= 5; //sets the mode value back to 0
}
}
Here is the setup.
Pretend the RGB LED is a LED strip and the potentiometers are just slots for one to be put in to change the colour when I want to. So when I'm not changing the colours I can use the potentiometer for other projects and not waste 3.