I’ve used the L298N to drive an Alps motorised volume contol. The motor rotates intermittently. This eliminates the risk of going to fast to loud. I used part of the code from “Simple IR Remote for Motorized Alps pot for Arduino”. This is the code:
#include
#define enA 9
#define UP_PIN_1 2
#define DOWN_PIN_1 7
int IR_RECEIVE_PIN = 12;
int volDelay = 15; // motor running time after short press
int pressDelay = 200; // to buffer the time between two repeat signals
int press = 0; // no press, short press, long press: 0, 1, 2
int Up = 0; // Rotation Up, Down: 1, 2
unsigned long lastPressTime = 0;
unsigned long lastRepeatTime = 0;
const unsigned long upCodes[] = {0xF50ACA35, 0xBD42CA35, 0xFE01CA35, 0xFD02CA35};
const unsigned long downCodes[] = {0xF609CA35, 0xBE41CA35, 0xFF00CA35, 0xFA05CA35};
void setup() {
Serial.begin(9600);
IrReceiver.begin(IR_RECEIVE_PIN);
pinMode(enA, OUTPUT);
pinMode(UP_PIN_1, OUTPUT);
pinMode(DOWN_PIN_1, OUTPUT);
lastPressTime = millis();
Serial.println(“Volume control started”);
motorOff();
}
void loop() {
if (IrReceiver.decode()) { // signal received
unsigned long currentIRValue = IrReceiver.decodedIRData.decodedRawData;
Serial.print(“Ontvangen IR-code: 0x”);
Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX);
bool commandProcessed = false; // IR signal received
// Check for “up” codes
for (int i = 0; i < 4; i++) {
if (currentIRValue == upCodes) {
motorUp();
lastPressTime = millis();
commandProcessed = true;
press = 1;
Up = 1;
break;
}
}
// Check for "down" codes
if (!commandProcessed) {
for (int i = 0; i = volDelay)) {
motorOff();
press = 0;
}
// If next signal is repeat; motor ON again
else if (press == 2 && (millis() – lastRepeatTime < pressDelay)) {
if (Up == 1){
motorUp();
}
if(Up == 2) {
motorDown();
}
press = 1; // stops motor until next repeat
}
// Stop the motor if unknown signal is received
else if (press == 0) {
press = 0;
lastPressTime = millis();
}
}
// Functies voor motorbesturing
void motorUp() {
digitalWrite(UP_PIN_1, HIGH);
digitalWrite(DOWN_PIN_1, LOW);
analogWrite(enA, 255); // Maximale snelheid (PWM)
Serial.println("Motor runs UP");
}
void motorDown() {
digitalWrite(UP_PIN_1, LOW);
digitalWrite(DOWN_PIN_1, HIGH);
analogWrite(enA, 255); // Maximale snelheid (PWM)
Serial.println("Motor runs DOWN");
}
void motorOff() {
digitalWrite(UP_PIN_1, LOW);
digitalWrite(DOWN_PIN_1, LOW);
analogWrite(enA, 0);
Serial.println("Motor OFF");
}