How to number Loops with IR Remote

Hi I am very new to Arduino. How do I number my for loops with an IR Remote?

Ex. I Press 3 on IR Remote and the code loops 3 times.

The code consists of two dc motors, servo motor, and a LED light so I think a switch case will be a lot of copy and paste and compiling. Or can I condense the code somehow and turn it into a function that I can repeat?

The code:
#include <IRremote.h>
#include <Servo.h>
////// DATA //////
enum phase {
powertest,wait ,stage1, stage2, stage3, STOP
};

#define power 0xFF30CF
#define led 4
#define BM 9
#define I1 8
#define I2 5
#define FM 11
#define I3 13
#define I4 12
int RECV_PIN=3;
int track;
int posi=0;
int pos=150;

IRrecv irrecv(RECV_PIN);
decode_results results;

Servo j;
////////////////

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
irrecv.enableIRIn();
pinMode(led,OUTPUT);
pinMode(8,OUTPUT);
pinMode(5,OUTPUT);
pinMode(9,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);

j.attach(10);
j.write(posi);

}

void loop() {
// put your main code here, to run repeatedly:
switch(track){
case powertest:
//// light test ///
digitalWrite(led,HIGH);
delay(1000);
digitalWrite(led,LOW);

//// motor test////
//// motor BM test ////
digitalWrite(I1,LOW);
digitalWrite(I2,HIGH);
analogWrite(BM,255);
delay(1500);
digitalWrite(I1,HIGH);
digitalWrite(I2,LOW);
analogWrite(BM,255);
delay(1500);
analogWrite(BM,0);
/// motor bm test end////
///motor fm test////
delay(2000);
digitalWrite(I3,HIGH);
digitalWrite(I4,LOW);
analogWrite(FM,255);
delay(1500);
digitalWrite(I3,LOW);
digitalWrite(I4,HIGH);
analogWrite(FM,255);
delay(1500);
analogWrite(FM,0);
/// motor fm test end///
//// servo test////
j.write(pos);
delay(1500);
j.write(posi);

track = wait;
break;

case wait:
if(irrecv.decode(&results)){
  unsigned int value = results.value;
  switch(value){
    case power:
    digitalWrite(led,HIGH);
    break;
  }
  irrecv.resume();
}
delay(1500);
digitalWrite(led,LOW);

break; 
track = stage1;

case stage1:

break;
}

}

not sure what you want to do, do something 3 times when a specific IR code is received?

the code has 3 states. in the initial state it toggles LEDs with delays between, then changes to state wait.

in wait it monitors the IR and sets an LED if it receives the "power" value, then turns it off after 1.5 sec.

there may have been a desire to change state, but it's preceded by a break, so it is never set

        break;
        track = stage1;

in general, seems that you would want loop() the code inside your "wait" case to be the primary code inside loop()

void loop () {
    if (irrecv.decode (&results)){
        unsigned int value = results.value;
        switch (value) {
        case power:
            ...
            break;

        case other-key-codes:
            ...
            break;
        }

        irrecv.resume ();
    }
}

if things needs to be done separately from when an IR code is received, a simple switch statement based state machine can also be included in loop() where state may be set when a specific IR codes are received

    switch (state){
    case action1:
        ...
        state = ??;
        break;

    case action2:
        ...
        state = ??;
        break;

    case action3:
        ...
        state = ??;
        break;
    }

and if some timed processing is required, loop() can also include the following where timeout is set when an IR code is received or in a state

    static unsigned long msecLst;
           unsigned long msec     = millis ();
    if (timout && (msec - msecLst) > timeout)  {
        timeout = 0;     // OR msecLst = msec
        ...
    }
1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.