How to break out loop within class member function with IR remote control

Hello everyone, I have a problem with my program , I try to use PWM control with IR remote control , but when I enter PWM function which has while(1) loop I can't break out of it using IR remote controller,
here is the code:

#include "IRremote.h"
#define MAXNUM 10
#define MAXCTRL 5
// PWM with turning left and right control + added Remote Control(8/10/16)
// I L293D
const int in1Pin = P1_7; // PIN 2 
const int in2Pin = P1_6; // PIN 7 
const int in3Pin = P2_3; // PIN 9 
const int in4Pin = P2_2; // PIN 15 
// II L293D
const int in5Pin = P2_1; // PIN 2 
const int in6Pin = P2_0; // PIN 7
const int in7Pin = P1_5; // PIN 9
const int in8Pin = P1_4; // PIN 15

// PWM variables
const int PERIOD = 10000;
int pulseWidth;
int keyPressed;
char controlButton;
int speedButton;

//IR remote declaration
const int irRecvPin = P2_5;
IRrecv IRr(irRecvPin);
decode_results results;

void setup()
{
  IRr.enableIRIn();
  Serial.begin(9600);
  // I L293D outputs
  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);
  pinMode(in3Pin, OUTPUT);
  pinMode(in4Pin, OUTPUT);
  // II L293D outputs
  pinMode(in5Pin, OUTPUT);
  pinMode(in6Pin, OUTPUT);
  pinMode(in7Pin, OUTPUT);
  pinMode(in8Pin, OUTPUT);
}

class Pwm{
  public:
    Pwm(int);
    int PERIOD;
    void duration(int duration);
    int pin1 , pin2 , pin3 , pin4 , pin5 , pin6 , pin7 , pin8;
    void attachPins(const int pin1 , const int pin2,
    const int pin3 , const int pin4, 
    const int pin5 , const int pin6,
    const int pin7 , const int pin8);
    void forward(int);
    void backward(int);
    void right(int);
    void left(int);
};
Pwm::Pwm(int period)
{
  PERIOD = period;
}
void Pwm::attachPins(const int pin1 , const int pin2,
const int pin3 , const int pin4, 
const int pin5 , const int pin6,const int pin7 , const int pin8){
    Pwm::pin1 = pin1;
    Pwm::pin2 = pin2; 
    Pwm::pin3 = pin3;
    Pwm::pin4 = pin4;
    Pwm::pin5 = pin5;
    Pwm::pin6 = pin6;
    Pwm::pin7 = pin7;
    Pwm::pin8 = pin8;
}

void Pwm::backward(int pulseWidth){
  Serial.println("Backward");
  digitalWrite(pin2 , LOW);
  digitalWrite(pin4 , LOW);
  digitalWrite(pin6 , LOW);
  digitalWrite(pin8 , LOW);
  for(int i = 1; ; i++){
    digitalWrite(pin1, HIGH);
    digitalWrite(pin3, HIGH);
    digitalWrite(pin5, HIGH);
    digitalWrite(pin7, HIGH);
    delayMicroseconds(pulseWidth);
    digitalWrite(pin1, LOW);
    digitalWrite(pin3, LOW);
    digitalWrite(pin5, LOW);
    digitalWrite(pin7, LOW);
    delayMicroseconds(PERIOD-pulseWidth);
    if( IRr.decode(&results) )
      break;
  }
}
void Pwm::forward(int pulseWidth){
  Serial.println("Forward");
  digitalWrite(pin2 , HIGH);
  digitalWrite(pin4 , HIGH);
  digitalWrite(pin6 , HIGH);
  digitalWrite(pin8 , HIGH);
  for(int i = 1; ; i++){
    digitalWrite(pin1, LOW);
    digitalWrite(pin3, LOW);
    digitalWrite(pin5, LOW);
    digitalWrite(pin7, LOW);
    delayMicroseconds(pulseWidth);
    digitalWrite(pin1, HIGH);
    digitalWrite(pin3, HIGH);
    digitalWrite(pin5, HIGH);
    digitalWrite(pin7, HIGH);
    delayMicroseconds(PERIOD-pulseWidth);
    if( IRr.decode(&results) )
      break;
  }
}
void Pwm::left(int pulseWidth){
  
  Serial.println("Left");
  digitalWrite(in2Pin, LOW);
  digitalWrite(in3Pin , LOW);
  digitalWrite(in6Pin , LOW);
  digitalWrite(in7Pin , LOW);
  while(1){
    digitalWrite(in1Pin, HIGH);
    digitalWrite(in4Pin , HIGH);
    digitalWrite(in5Pin , HIGH);
    digitalWrite(in8Pin , HIGH);
    delayMicroseconds(pulseWidth);
    digitalWrite(in1Pin , LOW);
    digitalWrite(in4Pin , LOW);
    digitalWrite(in5Pin , LOW);
    digitalWrite(in8Pin , LOW);
    delayMicroseconds(PERIOD-pulseWidth);
    if( IRr.decode(&results) )
      break;
  } 
}
void Pwm::right(int pulseWidth){
  
  Serial.println("Left");

  digitalWrite(in1Pin , LOW);
  digitalWrite(in4Pin , LOW);
  digitalWrite(in5Pin , LOW);
  digitalWrite(in8Pin , LOW);
  while(1){
    digitalWrite(in2Pin, HIGH);
    digitalWrite(in3Pin , HIGH);
    digitalWrite(in6Pin , HIGH);
    digitalWrite(in7Pin , HIGH);
    delayMicroseconds(pulseWidth);
    digitalWrite(in2Pin, LOW);
    digitalWrite(in3Pin , LOW);
    digitalWrite(in6Pin , LOW);
    digitalWrite(in7Pin , LOW);
    delayMicroseconds(PERIOD-pulseWidth);
    if( IRr.decode(&results) )
      break;
  } 
}
void Pwm::duration(int duration){
  digitalWrite(pin2 , LOW);
  digitalWrite(pin4 , LOW);
  digitalWrite(pin6 , LOW);
  digitalWrite(pin8 , LOW);
  for(int i = 1; ; i++){
    digitalWrite(pin1, HIGH);
    digitalWrite(pin3, HIGH);
    digitalWrite(pin5, HIGH);
    digitalWrite(pin7, HIGH);
    delay(duration);
    digitalWrite(pin1, LOW);
    digitalWrite(pin3, LOW);
    digitalWrite(pin5, LOW);
    digitalWrite(pin7, LOW);
    delay(duration);
    if( i == 1){
      float avgVoltage = (9.0 * duration) / PERIOD;
      Serial.println("Average voltage: ");
      Serial.println(avgVoltage);
    }
    if( Serial.available() )
      break;
  }
}

Problem with class Pwm,
member functions:
void forward(int);
void backward(int);
void right(int);
void left(int);
If you look at each of those class Pwm member functions you can see there is while(1) loop
and when I was controlling it from the keyboard I used this line to break out of the loop

if( Serial.available() )
      break;

Seems with IR and this line of code that it doesn't work.

if( IRr.decode(&results) )
      break;

I added just the part of the code since it has more than 400 lines and I couldn't post it , I can post the whole code later and provide a github link if that helps.
Does anyone have an idea how to solve this problem?

Doesn'tIRr.decode(&results)require aIRr.resume();after results is captured (or ignored)?

Does your bitbanging output do the trick ?
If yes, ok: If it works fine, it's good
Else and besides that:

  • with awhile(1)you compete for the ugliest arduino solution competition. :wink:

michael_x:
Doesn'tIRr.decode(&results)require aIRr.resume();after results is captured (or ignored)?

Does your bitbanging output do the trick ?
If yes, ok: If it works fine, it's good
Else and besides that:

  • with awhile(1)you compete for the ugliest arduino solution competition. :wink:

I haven't done bitbaning yet,
what do you suggest ?

Changed the code

void Pwm::right(int pulseWidth){
  int isButtonPressed = 0;
  Serial.println("Left");

  digitalWrite(in1Pin , LOW);
  digitalWrite(in4Pin , LOW);
  digitalWrite(in5Pin , LOW);
  digitalWrite(in8Pin , LOW);
  while(isButtonPressed == 0){
    digitalWrite(in2Pin, HIGH);
    digitalWrite(in3Pin , HIGH);
    digitalWrite(in6Pin , HIGH);
    digitalWrite(in7Pin , HIGH);
    delayMicroseconds(pulseWidth);
    digitalWrite(in2Pin, LOW);
    digitalWrite(in3Pin , LOW);
    digitalWrite(in6Pin , LOW);
    digitalWrite(in7Pin , LOW);
    delayMicroseconds(PERIOD-pulseWidth);
    if( IRr.decode(&results) ){
      isButtonPressed = 1;
      IRr.resume();
    }
    else
      isButtonPressed = 0;
  }
}

Still it doesn't work and I don't know why
I tried on this example and it shows that when you press some key it stores integer 1 into variable 'isKeyPressed' and 0 integer otherwise:

#include <IRremote.h>

int RECV_PIN = 5; //define input pin on Arduino

IRrecv irrecv(RECV_PIN);
decode_results results;
int isButtonPressed = 0;

void setup()
{

  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(RECV_PIN,INPUT);
}

void loop() {

  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value
    isButtonPressed = 1;
  }
  else 
    isButtonPressed = 0;
    
   Serial.println(isButtonPressed);
   delay(1000);
}

I don't get why it doesnt work at my example above.

Okay managed to solve it.
Here is the whole code:
http://hostcode.sourceforge.net/view/7745
From the line 372 you can see that I added at line 375

 if ( controlButton == 'w' )
     {
      PWM.forward(pulseWidth);
      controlButton = 'x';
     }
    else if ( controlButton == 's')
     {
      PWM.backward(pulseWidth );
      controlButton = 'x';
     }
    else if ( controlButton == 'd')
    {
      PWM.right(pulseWidth );
      controlButton = 'x';
    }  
    else if ( controlButton == 'a')
    {
      PWM.left(pulseWidth );
      controlButton = 'x';
    }

it assused after function call finishes that I don't end up calling that function all over again.
Now I have another problem , whenever I change pulseWidth of PWM or change direction motor stops.
I don't want it to stop when chaning speed , somehow I should implement a function with violate pointer that
should be changing the speed ( pulsewidth).