Help with changing the code

I have a code for a digital potentiometer and I want to use ir remote instead of buttons. I want help with that.
in this code, it has a physical button labeled as updown pin and a button named move pin. in this code the updown pin act as increasing the increment of the digipot and when the move pin is activated acts as decreasing the increment. I want to use IR remote with this instead of buttons and I want to separate the buttons in this way:
“20DF40BF” this button increase the increment
“20DFC03F” this one decrease it
“20DF906F” this one acts as a mute button for the sound as I use this pot for the speakers.
I don’t know how to code. I would appreciate the help. Thanks in advance.
here is the original code:

[code]
#include "Arduino.h"

#define upDownPin A1
#define movePin A2
#define resistancePin A5

// X9C103 pins
#define CS	10
#define INC	9
#define UD	8
#define PWR 7

// Current mode is INC or DEC volume?
bool isINC = true;

void setup() {
	Serial.begin(9600);
	delay(1000);

	Serial.println("Send any value to continue");
	while (!Serial.available()){}

	// CS LOW is active
	pinMode(CS, OUTPUT);
	digitalWrite(CS, LOW); // ON

	// INC - LOW is active
	pinMode(INC, OUTPUT);
	digitalWrite(INC, HIGH); // OFF

	// UP is high, DOWN is low
	pinMode(UD, OUTPUT);

	// Power up device
	Serial.println("Powering up X9C103");
	pinMode(PWR, OUTPUT);
	digitalWrite(PWR, HIGH);
	delay(10);

	// Retrieve stored volume setting
	Serial.print("Restoring volume to ");
	Serial.println(readVolume());

	// Demo switches, direction and push button
	pinMode(upDownPin, INPUT);
	pinMode(movePin, INPUT_PULLUP);

	Serial.println("Setup completed");
}

void loop() {
	// Are we INC or DEC the value?
	isINC = digitalRead(upDownPin) == HIGH;

	// Did we change the volume flag
	bool volChange = false;

	// If the UD pin is pressed initiate a change to resistance
	while (digitalRead(movePin) == LOW) {

		// Set the direction, up/down
		digitalWrite(UD, isINC ? HIGH : LOW);

		// Increment or Decrement as per the UD value
		digitalWrite(INC, LOW);
		delay(10);

		// Done!
		digitalWrite(INC, HIGH);

		// Get the new resistance
		Serial.println(readVolume());

		// Stop demo runaway!
		delay(100);

		// Set the flag that shows we have changed the volume
		volChange = true;
	}

	if (volChange) storeVolume();
	delay(100);
}

long readVolume() {
	// Get the new resistance
	return map(analogRead(resistancePin), 0, 1023, 0, 100);
}

void storeVolume() {
	// Ensure INC is HIGH then move CS HIGH and back to LOW
	digitalWrite(INC, HIGH);
	delayMicroseconds(1);
	digitalWrite(CS, HIGH);
	delay(20);
	digitalWrite(CS, LOW);
	Serial.println("Volume stored.");
}
[/code]

You are using the Arduink Uno board only right ?

I’m using arduino nano

Do you have any idea on the basic code of ir remote ?

I just know how to extract those hex codes for different buttons. other than that I don’t know much about coding yet. but I’m learning.

You cab find some good codes with explanation on the Web. You can start the search from the forum itself only, where you will get the codes. Meanwhile I will try my best from my end to find and share the final one to you. Although I am little busy on other work. But surely will try to sort the issue of yours

1 Like

I’ll search and work on it.
thank you very much. I’ll really appreciate your help.

1 Like

No issues, always there to help. Although you can easily modify your code. Even I will work on that

1 Like

I also worked on this project.
I did not test this sketch, I only compiled it and showed no compilation errors.
I believe it must meet your need.

RV mineirin


#include "Arduino.h"

#include <IRremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;

#define resistancePin A5

#define CS  10                            // X9C103 pins
#define INC 9
#define UD  8
#define PWR 7

bool isINC = true;                        // Current mode is INC or DEC volume?
//----------------------------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  delay(1000);

  irrecv.enableIRIn(); // Start the receiver

  Serial.println("Send any value to continue");
  while (!Serial.available()) {}

  pinMode(CS, OUTPUT);                      // CS LOW is active
  digitalWrite(CS, LOW);                    // ON
  pinMode(INC, OUTPUT);                     // INC - LOW is active
  digitalWrite(INC, HIGH);                  // OFF
  pinMode(UD, OUTPUT);                      // UP is high, DOWN is low
  Serial.println("Powering up X9C103");     // Power up device
  pinMode(PWR, OUTPUT);
  digitalWrite(PWR, HIGH);
  delay(10);
  Serial.print("Restoring volume to ");     // Retrieve stored volume setting
  Serial.println(readVolume());
  Serial.println("Setup completed");
}
//----------------------------------------------------------------------------------------
void loop()
{
  /*
    “20DF40BF” this button increase the increment
    “20DFC03F” this one decrease it
    “20DF906F” this one acts as a mute button for the sound as I use this pot for the speakers.
  */
  bool volChange = false;
  if (irrecv.decode(&results))
  {
    long int decCode = results.value;
    Serial.println(decCode);
    switch (results.value)
    {
      case 0x20DF40BF:
        digitalWrite(UD, HIGH);                     // Set the direction
        digitalWrite(INC, LOW);                     // Increment or Decrement as per the UD value
        delay(10);
        digitalWrite(INC, HIGH);                    // Done!
        Serial.println(readVolume());               // Get the new resistance
        delay(100);                                 // Stop demo runaway!
        volChange = true;                           // Set the flag that shows we have changed the volume
        break;

      case 0x20DFC03F:
        digitalWrite(UD,  LOW);                     // Set the direction
        digitalWrite(INC, LOW);                     // Increment or Decrement as per the UD value
        delay(10);
        digitalWrite(INC, HIGH);                    // Done!
        Serial.println(readVolume());               // Get the new resistance
        delay(100);                                 // Stop demo runaway!
        volChange = true;                           // Set the flag that shows we have changed the volume
        break;

      case 0x20DF906F:                              // Mute
        digitalWrite(PWR, !digitalRead(PWR));
        break;

      default:
        Serial.println("Waiting ...");
        break;
    }

    if (volChange)
      storeVolume();
    delay(100);
  }
}
//----------------------------------------------------------------------------------------
long readVolume()
{
  return map(analogRead(resistancePin), 0, 1023, 0, 100);       // Get the new resistance
}
//----------------------------------------------------------------------------------------
void storeVolume()
{
  digitalWrite(INC, HIGH);                            // Ensure INC is HIGH then move CS HIGH and back to LOW
  delayMicroseconds(1);
  digitalWrite(CS, HIGH);
  delay(20);
  digitalWrite(CS, LOW);
  Serial.println("Volume stored.");
}

1 Like

Thank you so much. It has one problem though. when I press a button it continues to work and on the serial monitor it says:
Volume stored.
551502015
0
and it goes on a loop until I restart the Arduino.
Do you know any solution to this? Thanks in advance.

Sorry I forgot to write a line, ( irrecv.resume(); // Receive the next value)
try this sketck now:

#include "Arduino.h"

#include <IRremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;

#define resistancePin A5

#define CS  10                            // X9C103 pins
#define INC 9
#define UD  8
#define PWR 7

bool isINC = true;                        // Current mode is INC or DEC volume?
//----------------------------------------------------------------------------------------
void setup() {
  Serial.begin(115200);
  delay(1000);

  irrecv.enableIRIn(); // Start the receiver

  Serial.println("Send any value to continue");
  while (!Serial.available()) {}

  pinMode(CS, OUTPUT);                      // CS LOW is active
  digitalWrite(CS, LOW);                    // ON
  pinMode(INC, OUTPUT);                     // INC - LOW is active
  digitalWrite(INC, HIGH);                  // OFF
  pinMode(UD, OUTPUT);                      // UP is high, DOWN is low
  Serial.println("Powering up X9C103");     // Power up device
  pinMode(PWR, OUTPUT);
  digitalWrite(PWR, HIGH);
  delay(10);
  Serial.print("Restoring volume to ");     // Retrieve stored volume setting
  Serial.println(readVolume());
  Serial.println("Setup completed");
}
//----------------------------------------------------------------------------------------
void loop()
{
  /*
    “20DF40BF” this button increase the increment
    “20DFC03F” this one decrease it
    “20DF906F” this one acts as a mute button for the sound as I use this pot for the speakers.
  */
  bool volChange = false;
  if (irrecv.decode(&results))
  {
    long int decCode = results.value;
   // Serial.println(decCode);
    switch (results.value)
    {
      case 0x20DF40BF:
        digitalWrite(UD, HIGH);                     // Set the direction
        digitalWrite(INC, LOW);                     // Increment or Decrement as per the UD value
        delay(10);
        digitalWrite(INC, HIGH);                    // Done!
        Serial.println(readVolume());               // Get the new resistance
        delay(100);                                 // Stop demo runaway!
        volChange = true;                           // Set the flag that shows we have changed the volume
        break;

      case 0x20DFC03F:
        digitalWrite(UD,  LOW);                     // Set the direction
        digitalWrite(INC, LOW);                     // Increment or Decrement as per the UD value
        delay(10);
        digitalWrite(INC, HIGH);                    // Done!
        Serial.println(readVolume());               // Get the new resistance
        delay(100);                                 // Stop demo runaway!
        volChange = true;                           // Set the flag that shows we have changed the volume
        break;

      case 0x20DF906F:                              // Mute
        digitalWrite(PWR, !digitalRead(PWR));
        break;

      default:
        Serial.println("Waiting ...");
        break;
    }

    if (volChange)
      storeVolume();
    delay(100);
    irrecv.resume(); // Receive the next value
  }

}
//----------------------------------------------------------------------------------------
long readVolume()
{
  return map(analogRead(resistancePin), 0, 1023, 0, 100);       // Get the new resistance
}
//----------------------------------------------------------------------------------------
void storeVolume()
{
  digitalWrite(INC, HIGH);                            // Ensure INC is HIGH then move CS HIGH and back to LOW
  delayMicroseconds(1);
  digitalWrite(CS, HIGH);
  delay(20);
  digitalWrite(CS, LOW);
  Serial.println("Volume stored.");
}
1 Like

Thank you so much. that problem is fixed but when I press any buttons it says waiting and nothing happens

Send any value to continue
Powering up X9C103
Restoring volume to 65
Setup completed
Waiting ...
Waiting ...
Waiting ...

Try uncommenting the line that prints what you got from the remote. If it’s printing welcome, you apparently got something, but not what you expected.

I’ve replaced Serial.println(“Waiting …”); with Serial.println(readVolume()); and apparently it does not change the volume and whatever I press it sticks with the previous one.

Sure, so find out what code it’s actually sending.

I couldn't undeerstand what is the problem here. can anyone help me. the remote just works fine but the digipot does not work. at first, it chooses whatever it likes, and when I press a button it sticks with just one value and does not increase or decrease.

ok, I Have a working code that I've tested with my speakers and It works perfectly.
could anyone help me with replacing two buttons on this code with Ir remote buttons?
these are two of my ir remote codes:
“20DF40BF” this button to act as buttonPotUp1
“20DFC03F” this one act as buttonPotDn1
“20DF906F” this one act as buttonPotMax and if pressed again act as buttonPotMid
Thanks in advance.

[code]

#include <X9C.h>                // X9C pot library
#include <Bounce2.h>            // button debounce library

#define UD              10      // pot up/down mode pin
#define INC             11      // pot increment pin
#define CS              12      // pot chip select pin
#define buttonPotMin    3       // button to set pot to min point
#define buttonPotMid    4       // button to set pot to mid point
#define buttonPotMax    5       // button to set pot to max point
#define buttonPotUp10   6       // button to inc pot by 10
#define buttonPotDn10   7       // button to dec pot by 10
#define buttonPotUp1    8       // button to inc pot by 1
#define buttonPotDn1    9       // button to dec pot by 1

// X9C wiring:  pin 3[High Terminal] -- R1 -- pin 5[Wiper] -- R2 -- pin 6[Low Terminal]

// The "X9C_UP" direction refers to the amount of resistance being created between the wiper and the "High" terminal 
// rather than the position of the wiper itself moving up toward the high terminal 
// (which would reduce resistance from wiper to High).
// i.e. setPot(70, false) will set the resistance between the X9C device pins 5 and 3 to 70% of maximum resistance
// where R1 = 70% of max, R2 = 30% of max

const int debounceInterval = 10;    // debounce time (ms) for button readings

X9C pot;                            // instantiate a pot controller
Bounce buttonPotMinDB  = Bounce();  // instantiate a bounce object for each button
Bounce buttonPotMidDB  = Bounce();
Bounce buttonPotMaxDB  = Bounce();
Bounce buttonPotUp10DB = Bounce();
Bounce buttonPotDn10DB = Bounce();
Bounce buttonPotUp1DB  = Bounce();
Bounce buttonPotDn1DB  = Bounce();


void setup() {
 
  pot.begin(CS, INC, UD);

  pinMode(buttonPotMin,  INPUT_PULLUP);
  pinMode(buttonPotMid,  INPUT_PULLUP);
  pinMode(buttonPotMax,  INPUT_PULLUP);
  pinMode(buttonPotUp10, INPUT_PULLUP);
  pinMode(buttonPotDn10, INPUT_PULLUP);
  pinMode(buttonPotUp1,  INPUT_PULLUP);
  pinMode(buttonPotDn1,  INPUT_PULLUP);

  // attach buttons to debouncers
  buttonPotMinDB.attach(buttonPotMin);
  buttonPotMinDB.interval(debounceInterval);      // interval in ms

  buttonPotMidDB.attach(buttonPotMid);
  buttonPotMidDB.interval(debounceInterval);      // interval in ms

  buttonPotMaxDB.attach(buttonPotMax);
  buttonPotMaxDB.interval(debounceInterval);      // interval in ms

  buttonPotUp10DB.attach(buttonPotUp10);
  buttonPotUp10DB.interval(debounceInterval);      // interval in ms

  buttonPotDn10DB.attach(buttonPotDn10);
  buttonPotDn10DB.interval(debounceInterval);      // interval in ms

  buttonPotUp1DB.attach(buttonPotUp1);
  buttonPotUp1DB.interval(debounceInterval);      // interval in ms

  buttonPotDn1DB.attach(buttonPotDn1);
  buttonPotDn1DB.interval(debounceInterval);      // interval in ms

}

void loop() {

  // update the Bounce instances
  buttonPotMinDB.update();
  buttonPotMidDB.update();
  buttonPotMaxDB.update();
  buttonPotUp10DB.update();
  buttonPotDn10DB.update();
  buttonPotUp1DB.update();
  buttonPotDn1DB.update();

  // change potentiometer setting if required based on button presses,
  // storing the setting in the chip if "true" is passed to the pot controller

  if ( buttonPotMinDB.fell()) {
    pot.setPotMin(false);                // set pot wiper to 0 resistance (pot will have wiper resistance per datasheet)
  }

  if ( buttonPotMidDB.fell()) {
    pot.setPot(49, false);               // set pot wiper to about half way (between 0 and 99)
  }

  if ( buttonPotMaxDB.fell()) {
    pot.setPotMax(false);                // set pot wiper to full resistance
  }

  if ( buttonPotUp10DB.fell()) {
    pot.trimPot(10, X9C_UP, true);      // move pot wiper 10 positions up ***AND STORE SETTING IN X9C CHIP***
  }

  if ( buttonPotDn10DB.fell()) {
    pot.trimPot(10, X9C_DOWN, false);    // move pot wiper 10 positions down
  }

  if ( buttonPotUp1DB.fell()) {
    pot.trimPot(1, X9C_UP, false);       // move pot wiper 1 position up
  }

  if ( buttonPotDn1DB.fell()) {
    pot.trimPot(1, X9C_DOWN, true);     // move pot wiper 1 position down
    
  
  }
}
[/code]