im controlling some small dc motors using arduino uno and tle94112el shield. currently, i can independently controll motors by pressing remote's buttons, but i wanna keep one motor rotating by holding a button. for example, by holding "unu" button, i wanna power the motor with 50% duty cycle, as i currently do by just pressing "unu". thank you!
#include <TLE94112.h>
#include <Tle94112Motor.h>
#include <IRremote.h>
#define unu 0x1
#define patru 0x4
#define sapte 0x7
int RECV_PIN = 2;
// Tle94112 Object
Tle94112 controller = Tle94112();
// Tle94112Motor Objects
Tle94112Motor motor1(controller);
Tle94112Motor motor2(controller);
Tle94112Motor motor3(controller);
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
// Enable MotorController Tle94112
// Note: Required to be done before starting to configure the motor
irrecv.enableIRIn();
controller.begin();
// Connect motor1 to HB1 and HB3
motor1.connect(motor1.HIGHSIDE, controller.TLE_HB1);
motor1.connect(motor1.LOWSIDE, controller.TLE_HB2);
// Drive HB1 with signal from PWM1
// Note: This allows to control the speed of the motor
motor1.setPwm(motor1.HIGHSIDE, controller.TLE_PWM2);
// Set PWM Frequency, default is 80 Hz
motor1.setPwmFreq(motor1.HIGHSIDE, controller.TLE_FREQ200HZ);
motor1.begin();
}
void loop()
{
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
if (results.value == unu)
{
motor1.start(127);
}
if (results.value == patru)
{
motor1.start(255);
}
if (results.value == sapte)
{
motor1.start(0);
}
}
}
thanks for replying. i followed your instructures. when i hold the button, my remote sends continuously the same code it sends for a single press too. so in the brackets of second "if", i used the same condition that i used in first "if". following your instructions, when i prees the button, motor starts, even if i release or keep holding the button, the motor keeps rotating, but when i press the same button second time, the motor stops. so instead of "timestamp = millis()", i used "timestamp < millis()", so in this situation, the motor was rotating as long as i kept the button pressed. now the problem is that the motor is powered with pulses, aprox. 2 impulses per second, it doesnt rotate smoothly. i tryed incresing and decreasing TIMEOUT with no effects. i also used all the pwm frequencies (80, 100, 200 hz) that the shield provides, and different duty cycles, also with no effects. but when i add 2 more motors, to controll them in the same way, each with it's own button, the pulses period was even bigger, with aprox one impuls on second. do you have any ideea about this? sorry, but its my first arduino project, i also have no experience on engineering forums. thaks for your time.
here is the code im using now:
#include <TLE94112.h>
#include <Tle94112Motor.h>
#include <IRremote.h>
#define unu 0x1
unsigned long timestamp = 0;
#define TIMEOUT 200
int RECV_PIN = 2;
// Tle94112 Object
Tle94112 controller = Tle94112();
// Tle94112Motor Objects
Tle94112Motor motor1(controller);
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
// Enable MotorController Tle94112
// Note: Required to be done before starting to configure the motor
irrecv.enableIRIn();
controller.begin();
// Connect motor1 to HB1 and HB3
motor1.connect(motor1.HIGHSIDE, controller.TLE_HB1);
motor1.connect(motor1.LOWSIDE, controller.TLE_HB2);
// Drive HB1 with signal from PWM1
// Note: This allows to control the speed of the motor
motor1.setPwm(motor1.HIGHSIDE, controller.TLE_PWM2);
// Set PWM Frequency, default is 80 Hz
motor1.setPwmFreq(motor1.HIGHSIDE, controller.TLE_FREQ200HZ);
motor1.begin();
}
void loop()
{
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
if (results.value == unu)
{
motor1.start(127);
timestamp < millis();
}
if(results.value == unu){
timestamp < millis();
}
if(millis() - timestamp > TIMEOUT){
motor1.start(0);
}
}
}
{}
button or press control+K