Leds only showing values max Power

I have a Led Arduino circuit that runs 4 parallel leds. They only run at max values of 255 of anything. Here is my code.

#include <Servo.h>

 

int redPin = 10;
int greenPin = 11;
int bluePin = 9;
int angle=0;
Servo serb; 
void setup()
{
  Serial.begin(9600);
  serb.attach(8);//Associate serbiy with pin 9
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  
}

void loop()
{
 int red, green, blue;  


  	red = 0;
  	green = 0;
  	blue = 0; 
  for(green=0;green<=50;green+=1)
  {
    for(blue=255;blue>=0;blue=blue-1)
   {
     
     for(red=255;red>=0;red=red-1)
     {
       
       
        if (red==255) {
         if (angle==0) {angle=180; 
            serb.write(angle);
       analogWrite(redPin, red);//Turns on led at pin 13
       analogWrite(greenPin, green);//Turns on led at pin 13
       analogWrite(bluePin, blue);//Turns on led at pin 13
       delay(250);
         }           
       }
        if (red==127) {
         if (angle==180) {angle=0;       
            serb.write(angle);
       analogWrite(redPin, red);//Turns on led at pin 13
       analogWrite(greenPin, green);//Turns on led at pin 13
       analogWrite(bluePin, blue);//Turns on led at pin 13
       delay(250);

                     
       }
      }
       
       analogWrite(redPin, red);//Turns on led at pin 13
       analogWrite(greenPin, green);//Turns on led at pin 13
       analogWrite(bluePin, blue);//Turns on led at pin 13
       delay(250);

       Serial.print(red);
       Serial.print(" , ");
       Serial.print(green);
       Serial.print(" , ");
       Serial.print(blue);
       Serial.print(" , ");
       Serial.println(angle);
      
      
    
   }   
  }   
 }   
}

Welcome to the forum.

Which Arduino board do you have ?

Ardunio Uno Make speical edition i think also here is my circut

image

It is not safe to power any motor directly from an Arduino. Use a separate power supply with enough current capability for your motor.

There may be a problem with the placement of all the { and } in your code, but it difficult to be sure about that because the indentation is a mess. Please Auto-Format the code and re-post it.

It's possible that the Servo library is attempting to use the same timer that your PWM pins use. Try using different PWM pins.

#include <Servo.h>

 

int redPin = 10;
int greenPin = 11;
int bluePin = 9;
int angle=0;
Servo serb; 
void setup()
{
  Serial.begin(9600);
  serb.attach(8);//Associate serbiy with pin 9
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  
}

void loop()
{
 int red, green, blue;  


  	red = 0;
  	green = 0;
  	blue = 0; 
  for(green=0;green<=50;green+=1)
  {
    for(blue=255;blue>=0;blue=blue-1)
   {
     
     for(red=255;red>=0;red=red-1)
     {
       
       
        if (red==255) {
         if (angle==0) {angle=180; 
            serb.write(angle);
       analogWrite(redPin, red);//Turns on led at pin 13
       analogWrite(greenPin, green);//Turns on led at pin 13
       analogWrite(bluePin, blue);//Turns on led at pin 13
       delay(250);
         }           
       }
        if (red==127) {
         if (angle==180) {angle=0;       
            serb.write(angle);
       analogWrite(redPin, red);//Turns on led at pin 13
       analogWrite(greenPin, green);//Turns on led at pin 13
       analogWrite(bluePin, blue);//Turns on led at pin 13
       delay(250);

                     
       }
      }
       
       analogWrite(redPin, red);//Turns on led at pin 13
       analogWrite(greenPin, green);//Turns on led at pin 13
       analogWrite(bluePin, blue);//Turns on led at pin 13
       delay(250);

       Serial.print(red);
       Serial.print(" , ");
       Serial.print(green);
       Serial.print(" , ");
       Serial.print(blue);
       Serial.print(" , ");
       Serial.println(angle);
      
      
    
   }   
  }   
 }   
}

I didn;t figure out how too use the auto formaat but i am using a micro servo and it is fine

It's a function of the IDE.

Your loop() takes 9.7 days to run. You have keep looking at it for more than a week to see that the leds are dimmed.
It it supposed to take that long ?

I made your project in Wokwi simulation:

If you want to test if the leds can be used with PWM, then add test code in the setup() function. Let each led go from 0 to 255 with a small delay. Test each led on its own.

After the blue goes down to 254 you cant see anything beside red flashes

That is okay the delay is supposed to 0

What should the project do (in your own words, without thinking about code).

Describing something from the real world in a logical way is the hardest part of writing code.

@28ri0203
There are several things wrong.
The Servo library disables PWM on pins 9 and 10 so you need to use other pins.
You can't drive 4 LEDs from an Arduino, only one LED.
So pick new pins and try it with just 1 RGB led to see if it works.

1 Like

Thanks it works

Glad I could help
Have fun!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.