About led starting point

hello all, i,v successfully created color mixing project myself and i,m so happy . and thanks all of you here who helped me in every problem. and now i,m planing to shift dimmer on push buttons and add third button for switch mode but before this topic, i want to fix color,s starting position, last two color,s starting point should be @ 0/dim and first color should be @ full brightened/HIGH. and how can i do this any help will be appreciated, thanks in advance.
here is my code

uint16_t GREEN = 4; //Led's and pins
uint16_t RED = 2;
uint16_t BLUE = 5;
int analog = A0;
int analogState = 0;
const byte button3 = 3;
const byte button4 = 13;
float dimmer = 0;
int button3S;
int button4S;
int mode;

void setup() {
  // put your setup code here, to run once:
 Serial.print(9600);
  pinMode(GREEN, OUTPUT); //Pinmodes of the leds
  pinMode(RED, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pinMode(analog, INPUT);
  pinMode(button3,INPUT_PULLUP);
  pinMode(button4,INPUT_PULLUP);

}

void loop() {
  // put your main code here, to run repeatedly:
analogState = analogRead(analog);  //Read the voltage on the Potentiometer
 dimmer = (255./1023.) * analogState; //Calculate Write Value for LED
button3S = digitalRead(button3);
button4S = digitalRead(button4);
if(button3S == LOW)
  {
    delay(300);  
    mode++;
     if(mode>2)
    {
      mode = 2;
       }
  }
 if(button4S == LOW)
  {
    delay(300);  
    mode--;
     if(mode<0)
    {
      mode = 0;
       }
   }
//BLUE
 if (mode == 0) {
  analogWrite(BLUE, dimmer);

  }
//GREEN
 if (mode == 1) {
 
  analogWrite(GREEN, dimmer);
  
}
//RED
 if (mode == 2) {
 
  analogWrite(RED, dimmer);
 }
}

Clearly not a working code. Please retry cutting and pasting your working code. If you embed comments about what should be happening, make them proper comments.
Would be best if you review How to get the best out of this forum and re-edit your first post for clarity.
Thanks

Looks much better now.
To turn an analogRead value ( 0 .. 1023 ) to an analogWrite byte value (0 .. 255) simply divide by 4 or shift by 2 bit ( >> 2 )
Intermediate float isn't useful here.

Now what's your actual problem? I bet a user/tester isn't too sure which mode is currently active.
On which arduino are 2, 4, 5 PWM pins ?

1 Like

Not all of these are PWM capable pins so they won't work with an analogWrite. The pins you need to use normally have a ~ (tilda) next to the PIN number on your board.

i,m using esp8266,

You should have said. Sorry I know very little about that device as it this not a proper Arduino.

thanks for reply mate, facing two issues first when i start project all lights are ON by default what i want OFF at startup point.
and second is beyond this topic, i just shift dimmer function to push buttons. and second issue is color reset for example when i set blue color in mode = 0 to 85 and whenever i switch back to mode =0 that color resets.

i,m laughing at myself how easy was it, i fixed all issues. lol :rofl:

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