Weird stuff happening with while, for and other conditional loops

Hey everybody,
i have a very wieid problem, for some reason my IDE ignores while, for etc. loops. I have checked it with this simple code:

int led = 11;           // the PWM pin the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // set the brightness of pin 9:
for(int x; x > 255; x++){
  analogWrite(led, x);
  delay(30);
}
  // wait for 30 milliseconds to see the dimming effect
}

Yes, it is the fade example, that i modified to run the for loop, and it didn’t work while the original example code did, which narrows the problem to this.
I had no errors comming up from the compiler, i’m running on 32-bit Linux Ubuntu 14.04 with Arduino IDE 1.6.7 and uploading the code to a genuine Arduino Uno R3.

So i wonder, what did i do wrong if i did anything wrong?

Do you have a LED and resistor connected to pin 11 ?

You didn’t initialize x in your for loop:

for(int x = 0; x > 255; x++){

Edited to add, and your end condition is backwards too:

for(int x = 0; x < 255; x++){

This is wrong:

for(int x; x > 255; x++)

x is not initialized.
The second expression is interpreted as "while", so will never be true even if you do start with x=0

  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);

That is NOT what that code does.

for(int x; x > 255; x++){

A for loop consists of an initialization section, a while clause, and a section to define what to do between iterations.

In this statement, you have declared that x is a variable of type int, so the compiler reserved an address for it. The variable is local to the function, so the compiler did NOT initialize it. That is YOUR job. So x starts at some unknown value, and the loop repeats as long as x is greater than 255. If it happens to start with x equal to 275, the loop will repeat until x rolls over at 32767. That is hardly useful.

Yes, it is the fade example, that i modified

You misspelled butchered.

i did repair the inicialization, but guess what, it still does not work...

this is the code after repairing the for loop:

int led = 11;           // the PWM pin the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // set the brightness of pin 9:
for(int x = 0; x > 255; x++){
  analogWrite(led, x);
  delay(30);
}
  // wait for 30 milliseconds to see the dimming effect
}

EEPblog:
i did repair the inicialization, but guess what, it still does not work...

this is the code after repairing the for loop:

int led = 11;           // the PWM pin the LED is attached to

int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // set the brightness of pin 9:
for(int x = 0; x > 255; x++){
  analogWrite(led, x);
  delay(30);
}
  // wait for 30 milliseconds to see the dimming effect
}

The while section of your for loop says "continue while x is greater than 255" and you have initialized x to zero.

Try less than.

Basicly this is what i have written so far, and based on the COMM debug i found out the while loop in the code just does not work…

#include <LiquidCrystal.h>

#define backlight 11 //PWM
#define fan 10 //PWM
#define rele 12

#define grn 2
#define red 3

#define termo A0 //LM35
#define bat A1 //baterie 12V

LiquidCrystal lcd(9, 8, 7, 6, 5, 4);

byte degree[8] = {2, 5, 2, 0, 0, 0, 0, 0};
int temperature;
const int backlightTimeout = 5; //sekund
int jas = 255;
int timeout = 250;
int setT = 25;
int offsetT = 5;
int fanSpeed = 50;
int drawScreen = 50;

void setup() {
  lcd.begin(16, 2);
  Serial.begin(9600);
  analogWrite(backlight, jas);
  analogWrite(fan, 255);
  lcd.createChar(0, degree);
  pinMode(rele, 0);
  pinMode(grn, 1);
  pinMode(red, 1);
  lcd.clear();
  lcd.print("Termostat v1.3");
  lcd.setCursor(0, 1);
  lcd.print("Baterie: ");
  float voltage = (analogRead(bat) / 1024.0) * 5.0;
  lcd.print(voltage * 3.2);
  lcd.print("V");
  delay(2500);
  lcd.clear();
  analogWrite(fan, fanSpeed);
}

void loop() {
  if (digitalRead(grn) == 1 || digitalRead(red) == 1) {
     bool setScreen = 0;
  timeout = 250;
  analogWrite(podsvit, jas);
  digitalWrite(rele, 0);
  Serial.println("set");
  while (setScreen == 1) {
    if (digitalRead(red) == 1) {
      setT++;
      timeout = 250;
    }
    else {
      if (digitalRead(grn) == 1) {
        setT--;
        timeout = 250;
      }
    }
    if (drawScreen == 25) {
      lcd.setCursor(0, 0);
      lcd.print("Nastav. teploty:");
      lcd.setCursor(6, 1);
      lcd.print(setT);
      lcd.write(byte(0));
      lcd.print("C    ");
    }
    drawScreen++;
    timeout--;
    if (timeout == 0) {
      setScreen = 1;
    }
    Serial.println(setT);
    delay(20);
  }
  timeout = 250;
  }
  if (drawScreen == 50) {
    lcd.setCursor(0, 0);
    lcd.print("  akt. teplota:");
    measure(termo);
    lcd.setCursor(6, 1);
    lcd.print(temperature);
    lcd.write(byte(0));
    lcd.print("C   ");
    if (temperature < (setT - offsetT)) {
      digitalWrite(rele, 1);
    }
    else {
      if (temperature > (setT + offsetT)) {
        digitalWrite(rele, 0);
      }
    }
    drawScreen = 0;


  }
  timeout--;
  if (timeout == 0) {
    backlightOff();
    timeout == 250;
  }
  drawScreen++;
  delay(20);
}

int measure(int analog) {
  int RawADC = analogRead(analog);
  double Temp;
  Temp = log(((10240000 / RawADC) - 10000));
  Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp )) * Temp );
  Temp = Temp - 273.15;// Convert Kelvin to Celcius
  temperature = int(Temp);
  return temperature;
}

void backlightOff() {
  analogWrite(backlight, 0);
}

In that last code the while(setScreen == 1) case can never be true since setScreen is set to 0 just before that loop. So I wouldn't expect anything in the while loop to ever execute.

BulldogLowell:
The while section of your for loop says "continue while x is greater than 255" and you have initialized x to this is the code after repairing the for loop:

int led = 11;           // the PWM pin the LED is attached to

void setup() {
 // declare pin 9 to be an output:
 pinMode(led, OUTPUT);
}

OMG!

The only weird thing I see is the commenting of your code.

The code actually sets pin-11 as OUTPUT, but the comment tells " // declare pin 9 to be an output:"

jurs:
OMG!

The only weird thing I see is the commenting of your code.

The code actually sets pin-11 as OUTPUT, but the comment tells " // declare pin 9 to be an output:"

i have the backlight led of an 1602 lcd connected to pin 11 in my wiring, i didn't bother to change the comments, it was just a test. So that's not the problem.

Delta_G:
In that last code the while(setScreen == 1) case can never be true since setScreen is set to 0 just before that loop. So I wouldn't expect anything in the while loop to ever execute.

that's ctually true now that i'm taking a look at the arduino reference... And i had in my mind that the while loops runs until the expression is true...
Well then, thanks everybody for the useful help!

  pinMode(rele, 0);
  pinMode(grn, 1);
  pinMode(red, 1);

What have you got against the usual (meaningful) INPUT and OUTPUT?

     bool setScreen = 0;

Boolean variables should be assigned true or false. NOTHING else.

    if (drawScreen == 25) {

Is that magic number supposed to mean anything?

That while while loop is confusing. Some comments are really needed. timeout seems to go up or down based on the value read from red or green, with no clue as to what is attached to those pins. I would not be using time in the name of a variable that was not based on time.