# How to stop the FOR cycle when reaching the maximum value added?

I apologize for the inconvenience, but I am urged to solve my problem.
1: I read the value of the potentiometer in ranges from 11 to 125.

2: use two IFs,
2a: first IF to have maximum value or equal to 60, then the function is executed. begins to add from 1 to 255. LED brightness.
2b: second IF having a minimum value or equal to 50, is executed. begins to subtract from 255 to 1. led brightness.

3: what I want is that in any of the IFs to reach their maximum or minimum values remain and yano return. they only increase or decrease according to the FIs.
help me please, I put them in while (1); and it stays still.

``````#define ac 3
#define bc 9
#define pot A0

int tiempo;
int respot;

void setup()
{
Serial.begin(9600);
pinMode(ac, OUTPUT);
pinMode(bc, OUTPUT);
pinMode(pot, INPUT);
}

void loop()
{
tiempo=map(respot,0,1023,11,125);
Serial.print("valor:");
Serial.println( tiempo);
delayMicroseconds(100);

if(tiempo >= 60)

for(int i =1; i<= 255; i++)
{
analogWrite(ac, i);
analogWrite(bc, i);
delayMicroseconds (12000);
}

if(tiempo <= 50)
for(int i =255; i>= 1; i--)
{
analogWrite(ac, i);
analogWrite(bc, i);
delayMicroseconds(12000);
}
}
``````

Try this:

``````   if(tiempo >= 60)

for(int i =1; i<= 255; i++)
{
if(tiempo < 60)
break;
analogWrite(ac, i);
analogWrite(bc, i);
delayMicroseconds (12000);
}

if(tiempo <= 50)
for(int i =255; i>= 1; i--)
{
if(tiempo > 50)
break;
analogWrite(ac, i);
analogWrite(bc, i);
delayMicroseconds(12000);
}
}
``````

Try this. LEDs(?) will get brighter as long as the input is greater than or equal to 60. The LEDs(?) will get dimmer as long as the input is less than or equal to 50. When the input is between 50 and 60 the LEDs(?) will not change.

``````#define ac 3
#define bc 9
#define pot A0

int tiempo;
int respot;
int brightness = 0;

void setup()
{
Serial.begin(9600);
pinMode(ac, OUTPUT);
pinMode(bc, OUTPUT);
pinMode(pot, INPUT);
}

void loop()
{
tiempo = map(respot, 0, 1023, 11, 125);
Serial.print("valor:");
Serial.println( tiempo);
delayMicroseconds(100);

if (tiempo >= 60 && brightness < 255)
{
brightness++;
}

if (tiempo <= 50 && brightness > 0)
{
brightness--;
}
analogWrite(ac, brightness);
analogWrite(bc, brightness);
delayMicroseconds(12000);
}
``````

how do you change brightness if your turning the pot the opposite way when mapped <50 or >60? The LED will just stay FULL bright or FULL dark, or to whatever level of brightness was reached. Not just between 50-60, but while above and below respective thresholds. until you pass the 50-60 threshold again…

Do you have to use another variable to see if the mapped output is changing pos or neg?

``````if (tiempo >= 60 && brightness < 255)
{
brightness++;
}
``````

or want to increase brightness when <50:

``````  if (tiempo <= 50 && brightness > 0)
{
brightness--;
}
``````