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?