Analog and Digital command mixup

NOTE:- I WANT TO TURN "ON" LED(PIN NO13) WHEN THE LED (PIN NO 9)(ANALOG) REACHES TO ITS MAX BRIGHTNESS(255) AND AGAIN TURN OFF THE LED(PIN13) WHEN BRIGHTNESS OF PIN 9 IS LESS THAN 255

MY PROGRAM:-

//LED Blink (half brightness)

int ledPin = 9;//the Arduino pin that is connected to the LED
int brightness = 0;
void setup() {
pinMode(ledPin, OUTPUT);// initialize the pin as an output
pinMode(13, OUTPUT);
}

void loop() {
// int brightness = 0;

analogWrite(ledPin, brightness);//brightness = 0
delay(50);// short delay
brightness += 1;

if(brightness>=255)
{
digitalWrite(13,HIGH);
}
else
{
digitalWrite(13,LOW);
}

REMEDY:- WHEN PIN NO 9 REACHES ITS MAX BRIGHTNESS LED(PIN 13) REMAINS ON EVERYTIME,IT DONT GETS OFF

PLEASE HAVE A LOOK ON THE CODE AND HELP ME ON THIS,THANKS FOR GIVING TIME!

Yes, because "brightness" keeps incrementing forever. Well, until it reaches 32,767.

BUT MAX FOR ANALOG IS 255? SHOULD I ALTER THE IF STATEMENT TO =255

FURTHER HELP WOULD BE APPRICIATED

THANK YOU

Please stop SHOUTING

@UKHeliBob may i know what is wrong? i am sorry for any wrong thing,but can u help me in this?

change

// int brightness = 0;

to

if (brightness >255) int brightness = 0;

When you are finished that, please read this post:

How to use this forum - please read.

THANK YOU @aarg.
the code worked for me ,thanks a lot

i also tried this

//LED Blink (half brightness)

int ledPin = 9;//the Arduino pin that is connected to the LED
int brightness = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);// initialize the pin as an output
pinMode(13, OUTPUT);
}

void loop()
{
// int brightness = 0;

analogWrite(ledPin, brightness);//brightness = 0
delay(50);// short delay
brightness += 1;

Serial.println(brightness);
if(brightness==255)
{
digitalWrite(13,HIGH);
brightness=0;
}
else
digitalWrite(13,LOW);

//keep repeating until brightness = 255 (full brightness)
}

sorry aarg ,but may i know what i am doing wrong in this forum?

may i know what i am doing wrong in this forum?

It was your use of capitals that I was commenting on but I see that you are now a reformed character.

Try making brightness a byte instead of an int and you will not need to reset it to zero after it reaches 255.

@UKHeliBob Thank you for your time and i appreciate your reply which i executed just now and saved my 2lines of code making the code smaller.

Thanks for being kind towards a beginner!

rushabh007:
sorry aarg ,but may i know what i am doing wrong in this forum?

You are not putting your code inside code tags. I think SHOUTING is generally frowned on as well.

@aarg it wont be repeated again

Thank you!

i have one more doubt,please help with the same.

let pin no 9=LED1 and pin no 13=LED 2

ORIGNAL CODE

//LED Blink (half brightness)

int ledPin = 9;//the Arduino pin that is connected to the LED,only pin no 3,5,6,9,10,11 are allowed here(PWM) pin 
byte brightness = 0;
void setup() {    
  Serial.begin(9600);            
  pinMode(ledPin, OUTPUT);// initialize the pin as an output
  pinMode(13, OUTPUT);
}

void loop() 
{
  analogWrite(ledPin, brightness);//brightness = 0
  delay(50);// short delay
  brightness += 1;

Serial.println(brightness);
if(brightness==255)
{
  digitalWrite(13,HIGH);
  delay(5000);
}
else
digitalWrite(13,LOW);

  //keep repeating until brightness = 255 (full brightness)
}

change from
int ledPin = 9;

to
int ledPin =4;

the LED1 turns on only after brightness>126bytes

why is this so?

note:-3,5,6,9,10,11 are pwm for my board

change in the code is only from pin 9 to 4

Good for using code tags, but can you please post the whole of the program that does not do what you want as we don't know exactly what you have changed.

@UKHeliBob i have edited the post

Because analogWrite uses PWM. I'm a bit confused, because your comment line lists PWM pins, so you seem to already know this. As you indicated, your LED is on a pin that lacks PWM.

My point was, that if analogwrite works only on pwm pin than y after 126byte the led is turning on on pin no 4

It's an undocumented feature of analogWrite().

Okay @aag it isn't related to dutycycle?I thought more than 50%depth will turn on led on nonpwm pins

rushabh007:
Okay @aag it isn't related to dutycycle?I thought more than 50%depth will turn on led on nonpwm pins

wiring_analog.c:

case NOT_ON_TIMER:
 default:
 if (val < 128) {
 digitalWrite(pin, LOW);
 } else {
 digitalWrite(pin, HIGH);
 }