Problem using while statement. Please help

I am receiving a signal from ir receiver based on the receiver value, I am using a function in void loop(). In this function I am again receiving ir value and using switch statement to direct it to a case. Within that case I am using while statement (I am again receiving ir values) to run this function for two minutes. But the statements within the while statements are not running. Please help what I am doing wrong. My program is as follows.

#include <Wire.h>

#include <Adafruit_MotorShield.h>

#include “utility/Adafruit_PWMServoDriver.h”

#include <IRremote.h>

// Create the motor shield object with the default I2C address

Adafruit_MotorShield AFMS = Adafruit_MotorShield();

// Or, create it with a different I2C address (say for stacking)

// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);

// Select which ‘port’ M1, M2, M3 or M4. In this case, M1

Adafruit_DCMotor *myMotor1 = AFMS.getMotor(1);

Adafruit_DCMotor *myMotor2 = AFMS.getMotor(2);

Adafruit_DCMotor *myMotor3 = AFMS.getMotor(3);

Adafruit_DCMotor *myMotor4 = AFMS.getMotor(4);

// You can also make another motor on port M2

//Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);

int RECV_PIN = 11;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()

{

Serial.begin(9600);

irrecv.enableIRIn(); // Start the receiver

AFMS.begin(); // create with the default frequency 1.6KHz

//AFMS.begin(1000); // OR with a different frequency, say 1KHz

if (irrecv.decode(&results)) {

Serial.println(results.value, HEX);

}

}

void loop()

{

if (irrecv.decode(&results)) // have we received an IR signal?

{

Serial.println(results.value, HEX);

translateIR2();

irrecv.resume();

}

}

void translateIR2() {

if (irrecv.decode(&results))

switch(results.value)

{

case 0x21:

unsigned long sec;

unsigned long A;

A=millis();

while(sec < 120000){

// Statements…;

sec=millis()-A;

Serial.println(sec,HEX);

break;

default:

Serial.println(" other button ");

}

}

}

Before you make a decision based on the variable “sec” (which you do as a condition for the while-loop) it needs to have a value assigned to it. If you don’t then it could be any random value based on how many electrons God ( replace with deity of your choice) decided to sprinkle into those memory flipflops. Never expect the memory to be 0 when it has booted up. Always give it a pre-defined value which is safe to draw initial conclusions on. Or make sure you set a value to it before you base logical conclusions and computations on it.

In this translateIR2 code the while loop may not ever start, if the memory content of sec happens to be larger than 120000. The switch case would end (see below) and it simply would end the function. Until the function is called again, and then nothing is changed in regards of sec. Apart from the received IR data, it pretty much acts like an infinite loop. (as in always trying, but never be able to enter the while loop) Only if sec happens to be less than 120000 initially then the while loop is entered and you’ll notice that because something is sent out of the serial port.

In this code you seem to have the switch-case and while loop tangled up into eachother. The while loop is part of the case 0x21 code, but the while loop isn’t ended and contains the default case code before the ending }-bracket. I’m surprised if it even compiles correctly. I don’t understand the purpose of the break command inside the while loop. Are you sure you placed the } bracket correctly? I doubt it.