Incrementing a counter, that runs through the elements in an array

Main goal: using an IR remote, increment a PWM with preset outputs. It can be thought of just like a volume adjustment from the IR remote.

My hacked up code runs incorrectly. It waits for my remote button to run HIGH, and then it increments the counter every 2 seconds, then waits for the remote button again. I understand why, but I don't know how to control it. I believe I need control the count<5 inside 'for (count=0;count<5;count++)' to keep the loop from just running over and over. I want it to wait for the remote button before the counter increments again.

Any direction towards the goal is much appreciated!
Thanks

// Include IR Remote Library by Ken Shirriff
#include <IRremote.h>

//define the board pin names
const int analogOutPin = 9; //PWM (Motor) Output
const int RECV_PIN = 8; //RF Reciever input

int outputValue = 0; //value of output
int SpeedArray[5]={254, 220, 190, 160, 145};
int count = 0;

// Define IR Receiver and Results Objects
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup() {
  // put your setup code here, to run once:
  // Enable the IR Receiver
  irrecv.enableIRIn();
}

void loop() {
  // put your main code here, to run repeatedly:
  if (irrecv.decode(&results)){
  switch(results.value){
  case 0x150C: //one of the IR remote buttons to increase speed
  for (count=0;count<5;count++){
  analogWrite(analogOutPin, SpeedArray[count]);
  delay(2000);
  analogWrite(analogOutPin, 0);
  }
  break;
  }
  irrecv.resume();
}
}

Goat1985:
My hacked up code runs incorrectly. It waits for my remote button to run HIGH, and then it increments the counter every 2 seconds, then waits for the remote button again.

How do you want it to work?

Power_Broker:
How do you want it to work?

I would like the counter to increment once after pressing the button on the remote.

I cleaned up the code a little to make it easier to read (functionally the same, though):

#include <IRremote.h>


const int analogOutPin = 9; //PWM (Motor) Output
const int RECV_PIN = 8; //RF Reciever input


int outputValue = 0;
int SpeedArray[5] = {254, 220, 190, 160, 145};
int count = 0;

IRrecv irrecv(RECV_PIN);
decode_results results;


void setup()
{
  // Enable the IR Receiver
  irrecv.enableIRIn();
}


void loop()
{
  if (irrecv.decode(&results))
  {
    if (results.value = 0x150C)
    {
      for (count = 0; count < 5; count++)
      {
        analogWrite(analogOutPin, SpeedArray[count]);
        delay(2000);
        analogWrite(analogOutPin, 0);
      }
    }
    irrecv.resume();
  }
}

I'll be honest, I've never used IR transmitter/receivers. Do you have a link to how your specific remote/receiver works? Mainly, I would like to know where the speedarray and 2 second delay come from. Once I know that, I might be able to help fix the problem.

Power_Broker:
I'll be honest, I've never used IR transmitter/receivers. Do you have a link to how your specific remote/receiver works? Mainly, I would like to know where the speedarray and 2 second delay come from. Once I know that, I might be able to help fix the problem.

Thanks for the clean up, I see what you did there.

I inserted the speedarray and delay. The delay will not remain, I will have different code there as I get stuff working.

And here is a link to the page I studied for using the IR remote with Arduino:

I took dronebot's IR to LED sketch, where he turns on an LED with a button from the IR remote, and started pulling it apart to do other things with it, like what I'm asking here.

Here is the IR_to_LED code from dronebot:

/*
  IR Receiver Demonstration 3
  IR-Rcv-Demo3.ino
  Control LED's using Unused IR Remote keys
 
  DroneBot Workshop 2017
  http://dronebotworkshop.com
*/
 
// Include IR Remote Library by Ken Shirriff
#include <IRremote.h>
 
// Define sensor pin
const int RECV_PIN = 4;
 
// Define LED pin constants
//const int ; 
const int yellowPin = 7;
 
// Define integer to remember toggle state
int togglestate = 0;
 
// Define IR Receiver and Results Objects
IRrecv irrecv(RECV_PIN);
decode_results results;
 
 
void setup(){
  // Enable the IR Receiver
  irrecv.enableIRIn();
  // Set LED pins as Outputs
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(yellowPin, OUTPUT);
}
 
 
void loop(){
    if (irrecv.decode(&results)){
 
        switch(results.value){
          case 0x150C: //Red Keypad Button
        // Turn on LED for 2 Seconds
        digitalWrite(LED_BUILTIN, HIGH);
        delay(2000);
        digitalWrite(LED_BUILTIN, LOW);
        break;
   
          case 0xFE8A75: //Yellow Keypad Button
        // Toggle LED On or Off
        if(togglestate==0){
        digitalWrite(yellowPin, HIGH);
        togglestate=1;
        }
        else {
        digitalWrite(yellowPin, LOW);
        togglestate=0;
        }
        break;
        
    }
    irrecv.resume(); 
  }
 
}

Thanks for your time!

Ok, I'll assume you don't need delay at all, so I wonder if we can just remove it and adjust where we turn off the PWM:

#include <IRremote.h>


const int analogOutPin = 9; //PWM (Motor) Output
const int RECV_PIN = 8; //RF Reciever input


int outputValue = 0;
int SpeedArray[5] = {254, 220, 190, 160, 145};
int count = 0;

IRrecv irrecv(RECV_PIN);
decode_results results;


void setup()
{
  // Enable the IR Receiver
  irrecv.enableIRIn();
}


void loop()
{
  if (irrecv.decode(&results))
  {
    if (results.value = 0x150C)
    {
      for (count = 0; count < 5; count++)
      {
        analogWrite(analogOutPin, SpeedArray[count]);
      }
    analogWrite(analogOutPin, 0);
    }
    irrecv.resume();
  }
}

Try that and see if it works

Nope. Not quite it. I think I need to control the condition of the 'for' control structure but I can't bring myself to figure out how.

The attachment shows what Arduino says about the 'for' control structure.

Each time though the loop the the control is tested. My case, I have counter > 5, so as long as the counter is less than 5, it continues counting until it reaches 5.

How do I count up per pulse of the remote button?

Thanks for the refresher, but I already know how for-loops work :slight_smile:

Goat1985:
Each time though the loop the the control is tested. My case, I have counter > 5, so as long as the counter is less than 5, it continues counting until it reaches 5.

Yes, the loop executes 5 times so that you call "analogWrite()" for each element of SpeedArray. Is that not what you want it to do?

Goat1985:
How do I count up per pulse of the remote button?

Create an integer variable and increment it (++) every time "if (results.value = 0x150C)" evaluates true.

Do you just want to increment once per button press? 0 - 4, then start over again?

evanmars:
Do you just want to increment once per button press? 0 - 4, then start over again?

Yes.

(I think I need to reevaluate how I ask for help. I'm confusing people, and I apologize.)

Power_Broker:
Thanks for the refresher, but I already know how for-loops work :slight_smile:

Sorry, that wasn't necessary for you, but so I could point out what I meant by saying "control", I meant the control for the for-loop.

Power_Broker:
Create an integer variable and increment it (++) every time "if (results.value = 0x150C)" evaluates true.

This is what I'm trying to use the counter for. I'm not sure what that would look like inside the for-loop line of code?

(My sincere apologies for the confusion. I'll get better at getting to the point faster as I use forums more and more)

Goat1985:
This is what I'm trying to use the counter for. I'm not sure what that would look like inside the for-loop line of code?

In that case, you shouldn't use a for loop. If you have a variable named "count" that never increments past 4, you can do "analogWrite(analogOutPin, SpeedArray[count]);" without the for loop. That is, assuming SpeedArray corresponds to individual and specific volume values.

Power_Broker:
In that case, you shouldn't use a for loop. If you have a variable named "count" that never increments past 4, you can do "analogWrite(analogOutPin, SpeedArray[count]);" without the for loop. That is, assuming SpeedArray corresponds to individual and specific volume values.

Yes, SpeedArray provides the values that the remote button "steps' the output through. I will want one going down the array elements as well.

For that case, you will decrement the counter and continue to use it as previously mentioned