I need a counting system for arduino 21 key remote. sorry if in wrong topic, im new

im trying to have the one key count to three. first count sets it to 100% brightness then 50% then 10% for R then reset when pressed a forth time and the same with the two and three key (two changes G brightness then three changes B) then the power button to set R, G and B to low but high when pressed a second time then repeat
heres the code, thank you

#include <DIYables_IRcontroller.h> // DIYables_IRcontroller library
#define IR_RECEIVER_PIN 7 // The Arduino pin connected to IR controller

DIYables_IRcontroller_21 irController(IR_RECEIVER_PIN, 200); // debounce time is 200ms
const int R = 5;
const int G = 6;
const int B = 4;
int PowerCounter = 0;
int PowerState = 0;
int LastPowerState = 0;
void setup() {
  Serial.begin(9600);
  irController.begin();
  pinMode(R, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(B, OUTPUT);
}

void loop() {
  Key21 key = irController.getKey();
  if (key != Key21::NONE) {
    switch (key) {
      case Key21::KEY_CH_MINUS:
        Serial.println("Power");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_CH:
        Serial.println("+");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_CH_PLUS:
        Serial.println("Func");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_PREV:
        Serial.println("<<");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_NEXT:
        Serial.println(">||");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_PLAY_PAUSE:
        Serial.println(">>");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_VOL_MINUS:
        Serial.println("CH_Down");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_VOL_PLUS:
        Serial.println("-");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_EQ:
        Serial.println("CH_Up");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_100_PLUS:
        Serial.println("EQ");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_200_PLUS:
        Serial.println("ST");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_0:
        Serial.println("0");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_1:
        Serial.println("1");
        // TODO: YOUR CONTROL
        analogWrite(R, 255);
        break;

      case Key21::KEY_2:
        Serial.println("2");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_3:
        Serial.println("3");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_4:
        Serial.println("4");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_5:
        Serial.println("5");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_6:
        Serial.println("6");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_7:
        Serial.println("7");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_8:
        Serial.println("8");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_9:
        Serial.println("9");
        // TODO: YOUR CONTROL
        break;

      default:
        Serial.println("WARNING: undefined key:");
        break;
    }
  }
}

what have you tried?

I'm not very good at coding so I've tried using button counting and that didn't work and I couldn't find anything similar

you might benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction

could you by chance tell me how I could do one part of the counting with the 21 button remote control :question:

Could you by chance tell me if the sketch you published works?

What key is supposed to count?

      case Key21::KEY_EQ:
        Serial.println("CH_Up");
        // TODO: YOUR CONTROL   why not count right here
        break;

or pick another button.

a7

yeah it work but i want the counting and i want it to count the one, two and three button

What does that mean?

You want to count how many times each of '1', '2' and '3' are pressed? Just count them.

Counting can be done by adding one to a variable each time another event you want to count happens.

HTH

a7

So you want it to count to 4. 0-3, inclusive.
set up a

byte oneCounter = 0;

then under that case

Add a sub switch case where it says // TODO: YOUR CONTROL add in your own wedge, like this

void loop() {
  // etc etc to here
  switch (key) {
    // etc etc to here
    case Key21::KEY_1:
      oneCounter++;
      if (oneCounter >= 4) {
        oneCounter = 0;
      }
      Serial.println("1");
      switch (oneCounter) {
        case 0:
          analogWrite(R, 0);
          break;
        case 1:
          analogWrite(R, 255);
          break;
        case 2:
          analogWrite(R, 127);
          break;
        case 3:
          analogWrite(R, 25);
          break;
      }                // end of switch (oneCounter)
      break;     // break for case Key21::KEY_1:
  }                  // end of switch(key)

                    // etc etc to end of void loop
}                  // end of loop
1 Like

i can do that?

thanks!

1 Like

can you put it into my code so i know if im doing it properly?

It's all commented there. You'll be alright. Watch that the curly braces encompass the block of code it's supposed to, and if you click immediately to the right of a bracket or curly brace, the IDE will automagically draw a little rectangle around its match.

Oh, always make another copy of good working code and make changes to the new copy. Just name it the same thing x2 if the first one was named x. That way it's super easy to roll back to the good one before you took a chance and maybe surprised yourself.

You've got this.

In fact, why not make a third version of the code that just does the one key and then add the others in after? Keep it small and manageable. small, manageable changes is a best practice in coding. Never try to change a bunch of things at one time.

could you also make the power button count and third count reset and first turn on and second turn off all as sort of a override and turn all of them to there off state when turned back on, just wondering

sorry if im asking for too much

Who's hobby is this? The secret is to take it one thing at a time. Problem is, when you're new, you don't know what "one thing" should look like. meaning how to structure a code in a consistent way that starts to develop your own style. That'll come with practice, like anything.

I won't do it all for you since you really should learn (and type out your own) the switch/case/break control structure if you plan on going further with Arduino to make things that can do more than one thing and not trip over its own feet.

You seem to be about the age of someone who like video games, the switch/case/break (one good way to build what's known as a finite state machine) control structure is like leveling up your skills to allow you to make games or interactive props.

Here's the third version I mentioned in post #14, what I did was make a second tab and comment out everything but the section I was working on. If I was you, I'd add in each bit as you work along. So make a second tab and call it whatever you like (doesn't matter as long as it's all commented out) and try stuff out.

tab 1 (main sketch):

#include <DIYables_IRcontroller.h> // DIYables_IRcontroller library
#define IR_RECEIVER_PIN 7 // The Arduino pin connected to IR controller

DIYables_IRcontroller_21 irController(IR_RECEIVER_PIN, 200); // debounce time is 200ms
const int R = 5;
const int G = 6;
const int B = 4;
int PowerCounter = 0;
int PowerState = 0;
int LastPowerState = 0;

byte oneCounter = 0; // keeps track of how many times you've pressed the 1 button

void setup() {
  Serial.begin(9600);
  irController.begin();
  pinMode(R, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(B, OUTPUT);
}

void loop() {
  Key21 key = irController.getKey();
  if (key != Key21::NONE) {
    switch (key) {
      // etc etc to here, all the other cases
      case Key21::KEY_1:
        oneCounter++;
        if (oneCounter >= 4) {
          oneCounter = 0;
        }
        Serial.println("1");
        switch (oneCounter) {
          case 0:
            analogWrite(R, 0);
            break;
          case 1:
            analogWrite(R, 255);
            break;
          case 2:
            analogWrite(R, 127);
            break;
          case 3:
            analogWrite(R, 25);
            break;
        }
        break; // break on case Key21::KEY_1:
      default:
        Serial.println("WARNING: undefined key:");
        break;
    } // end of switch (key)
  } // end of if (key != Key21::NONE) 
}



tab two (scratch pad if you will)


/*
case Key21::KEY_CH_MINUS:
Serial.println("Power");
// TODO: YOUR CONTROL
break;

case Key21::KEY_CH:
Serial.println("+");
// TODO: YOUR CONTROL
break;

case Key21::KEY_CH_PLUS:
Serial.println("Func");
// TODO: YOUR CONTROL
break;

case Key21::KEY_PREV:
Serial.println("<<");
// TODO: YOUR CONTROL
break;

case Key21::KEY_NEXT:
Serial.println(">||");
// TODO: YOUR CONTROL
break;

case Key21::KEY_PLAY_PAUSE:
Serial.println(">>");
// TODO: YOUR CONTROL
break;

case Key21::KEY_VOL_MINUS:
Serial.println("CH_Down");
// TODO: YOUR CONTROL
break;

case Key21::KEY_VOL_PLUS:
Serial.println("-");
// TODO: YOUR CONTROL
break;

case Key21::KEY_EQ:
Serial.println("CH_Up");
// TODO: YOUR CONTROL
break;

case Key21::KEY_100_PLUS:
Serial.println("EQ");
// TODO: YOUR CONTROL
break;

case Key21::KEY_200_PLUS:
Serial.println("ST");
// TODO: YOUR CONTROL
break;

case Key21::KEY_0:
Serial.println("0");
// TODO: YOUR CONTROL
break;

*/


/*
  case Key21::KEY_1:
        Serial.println("1");
        // TODO: YOUR CONTROL
        analogWrite(R, 255);
        break;
*/


/*
case Key21::KEY_2:
Serial.println("2");
// TODO: YOUR CONTROL
break;

case Key21::KEY_3:
Serial.println("3");
// TODO: YOUR CONTROL
break;

case Key21::KEY_4:
Serial.println("4");
// TODO: YOUR CONTROL
break;

case Key21::KEY_5:
Serial.println("5");
// TODO: YOUR CONTROL
break;

case Key21::KEY_6:
Serial.println("6");
// TODO: YOUR CONTROL
break;

case Key21::KEY_7:
Serial.println("7");
// TODO: YOUR CONTROL
break;

case Key21::KEY_8:
Serial.println("8");
// TODO: YOUR CONTROL
break;

case Key21::KEY_9:
Serial.println("9");
// TODO: YOUR CONTROL
break;

*/

Note that this is untested, I don't even have that library. Good luck, but you won't need it.
Edit:
almost forgot to comment tab two stuff all out, lol
Also, for every key you want to cycle through different values or numbers or whatever, just make new variables named twoCounter, threeCounter, whateverCounter

Almost forgot the oneCounter variable globally in tab one, oops!

Make sure you have it!

thank you so much btw i got it working (:

1 Like

W mans

It works fr

@tomne23 what does your first post here in the forum means?