2 different operation modes for a motor

hi. i want to press “unu” button of the remote, and the motor to rotate continuously, and when i press “sound” i want the motor to rotate untill the hall sensor detects a magnetic field. any ideas please?

#include <TLE94112.h>
#include <Tle94112Motor.h>
#include <IRremote.h>

#define unu 0x1
#define sound 0x55


int RECV_PIN = 2;
int hallSensorPin1 = 3;
int state1;
int oldState1 = 1;

Tle94112 controller = Tle94112();
Tle94112Motor motor1(controller);

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{ irrecv.enableIRIn();
  controller.begin();
  
motor1.connect(motor1.HIGHSIDE, controller.TLE_HB1);
  motor1.connect(motor1.LOWSIDE, controller.TLE_HB2);
  motor1.setPwm(motor1.HIGHSIDE, controller.TLE_PWM2);
  motor1.setPwmFreq(motor1.HIGHSIDE, controller.TLE_FREQ200HZ);

  motor1.begin();
  }

void loop()
{
 if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume();
    
if(results.value==unu||results.value==0x801||results.value==0x2049){
motor1.start(63);
}
if(results.value==sound||results.value==0x55||results.value==0x2103||results.value==0x837||results.value==0x37){
      motor1.start(63);}
      }
  state1 = digitalRead(hallSensorPin1);

   
  if (state1 == HIGH) {
    oldState1 = 1; 
  }
  if (state1 == LOW && oldState1 == 1) {
    motor1.coast();
    oldState1 = 0;
  }
  }

What's your problem?

zwieblum:
What's your problem?

in this configuration, even if i press "unu" or "sound", the motor rotates untill the hall sensor detects a magnetic field.

Add debug output to your code and try again.

If you use AutoFormat (Ctrl-T) in the IDE you should easily see the problem. The sensor handling code is outside all of the if statements.

Steve

Your code is very badly laid out so it is almost impossible to see how the blocks of code are related. If you use the AutoFormat tool it will lay the code out consistently - something like this

#include <TLE94112.h>
#include <Tle94112Motor.h>
#include <IRremote.h>

#define unu 0x1
#define sound 0x55


int RECV_PIN = 2;
int hallSensorPin1 = 3;
int state1;
int oldState1 = 1;

Tle94112 controller = Tle94112();
Tle94112Motor motor1(controller);

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup() {
    irrecv.enableIRIn();
    controller.begin();
    
    motor1.connect(motor1.HIGHSIDE, controller.TLE_HB1);
    motor1.connect(motor1.LOWSIDE, controller.TLE_HB2);
    motor1.setPwm(motor1.HIGHSIDE, controller.TLE_PWM2);
    motor1.setPwmFreq(motor1.HIGHSIDE, controller.TLE_FREQ200HZ);

    motor1.begin();
}

void loop() {
    
    if (irrecv.decode(&results)) {
        Serial.println(results.value, HEX);
        irrecv.resume();
        
        if(results.value==unu||results.value==0x801||results.value==0x2049){
            motor1.start(63);
        }
        if(results.value==sound||results.value==0x55||results.value==0x2103||results.value==0x837||results.value==0x37) {
            motor1.start(63);
        }
    }
    state1 = digitalRead(hallSensorPin1);

    if (state1 == HIGH) {
        oldState1 = 1; 
    }
    if (state1 == LOW && oldState1 == 1) {
        motor1.coast();
        oldState1 = 0;
    }
}

The way the code is written at the moment it seems to do the exact same thing regardless of what value comes from the remote. Think about that.

…R

i put the sensor handling code inside the if statement of "sound" button, and now the motor rotates continuously when i press any of the buttons.
and i dont know what debug output means, im new in Arduino.

Google is broken, too.

tincu_ioan:
i put the sensor handling code inside the if statement of "sound" button, and now the motor rotates continuously when i press any of the buttons.

Please post the latest version of your program in your next Reply so we can see exactly what you did

and i dont know what debug output means, im new in Arduino.

Put some Serial.print() statements in your program so you can see what the program is doing

...R

this is what i did:

#include <TLE94112.h>
#include <Tle94112Motor.h>
#include <IRremote.h>

#define unu 0x1
#define sound 0x55


int RECV_PIN = 2;
int hallSensorPin1 = 3;
int state1;
int oldState1 = 1;

Tle94112 controller = Tle94112();
Tle94112Motor motor1(controller);

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{ irrecv.enableIRIn();
  controller.begin();

  motor1.connect(motor1.HIGHSIDE, controller.TLE_HB1);
  motor1.connect(motor1.LOWSIDE, controller.TLE_HB2);
  motor1.setPwm(motor1.HIGHSIDE, controller.TLE_PWM2);
  motor1.setPwmFreq(motor1.HIGHSIDE, controller.TLE_FREQ200HZ);

  motor1.begin();
}

void loop()
{
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume();

    if (results.value == unu || results.value == 0x801 || results.value == 0x2049) {
      motor1.start(63);
    }

    if (results.value == sound || results.value == 0x55 || results.value == 0x2103 || results.value == 0x837 || results.value == 0x37) {
      motor1.start(63);
      state1 = digitalRead(hallSensorPin1);

      if (state1 == HIGH) {
        oldState1 = 1;
      }
      if (state1 == LOW && oldState1 == 1) {
        motor1.coast();
        oldState1 = 0;
      }
    }
  }
}

Try this

    if (results.value == sound || results.value == 0x55 || results.value == 0x2103 || results.value == 0x837 || results.value == 0x37) {
      motor1.start(63);
      oldState1 = state1;
      state1 = digitalRead(hallSensorPin1);

      if (state1 == LOW && oldState1 == 1) {
        motor1.coast();
      }
    }

I am assuming you only want the motor to coast when the state changes from HIGH to LOW

If this was my project I would name the variable hallSensorState rather than state1

...R

it works exactly the same. continuous no matter wich of the buttons i press :frowning:

Maybe you should try to learn some basic programming first?

tincu_ioan:
it works exactly the same. continuous no matter wich of the buttons i press :frowning:

Please always post the complete latest version of your code so we can see exactly what you have done. The devil is in the detail.

Also, have you carefully considered why my changes have not had any effect. The purpose of my suggestion was to give you food for thought as much as anything else. Successful programming requires attention to the causes of failure as well as of success.

...R

Robin2:
Please always post the complete latest version of your code so we can see exactly what you have done. The devil is in the detail.

…R

this is the last configuration i used:

#include <TLE94112.h>
#include <Tle94112Motor.h>
#include <IRremote.h>

#define unu 0x1
#define sound 0x55


int RECV_PIN = 2;
int hallSensorPin1 = 3;
int state1;
int oldState1 = 1;

Tle94112 controller = Tle94112();
Tle94112Motor motor1(controller);

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{ irrecv.enableIRIn();
  controller.begin();

  motor1.connect(motor1.HIGHSIDE, controller.TLE_HB1);
  motor1.connect(motor1.LOWSIDE, controller.TLE_HB2);
  motor1.setPwm(motor1.HIGHSIDE, controller.TLE_PWM2);
  motor1.setPwmFreq(motor1.HIGHSIDE, controller.TLE_FREQ200HZ);

  motor1.begin();
}

void loop()
{
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume();

    if (results.value == unu || results.value == 0x801 || results.value == 0x2049) {
      motor1.start(63);
    }

    if (results.value == sound || results.value == 0x55 || results.value == 0x2103 || results.value == 0x837 || results.value == 0x37) {
      motor1.start(63);

      oldState1 = 1;
      state1 = digitalRead(hallSensorPin1);


      if (state1 == LOW && oldState1 == 1) {
        motor1.coast();


      }
    }
  }
}

the problem is im so busy cose this is just a part from a school project and i have to do a complete documentation for it, but i see i need to think a lot about the code too.

I wonder if it should be like this

    if (results.value == sound || results.value == 0x55 || results.value == 0x2103 || results.value == 0x837 || results.value == 0x37) {
      motor1.start(63);

      oldState1 = 1;
    }
    state1 = digitalRead(hallSensorPin1);

    if (state1 == LOW && oldState1 == 1) {
        motor1.coast();
        oldState1 = 0;
    }

so that it checks the state of the sensor every time and no just when a value is received

If that is the case then oldState1 is a poor name for the variable. motorActive might be a better name so it would be like this

    if (results.value == sound || results.value == 0x55 || results.value == 0x2103 || results.value == 0x837 || results.value == 0x37) {
      motor1.start(63);

      motorActive = true;
    }
    state1 = digitalRead(hallSensorPin1);

    if (state1 == LOW && motorActive == true ) {
        motor1.coast();
       motorActive = false;
    }

...R

I’m not certain if this iw what Robin2 is saying, but I believe that the motor control needs to be separated from the conditional of reading a new value.

  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume();
  }//add bracket here
#include <TLE94112.h>
#include <Tle94112Motor.h>
#include <IRremote.h>

#define unu 0x1
#define sound 0x55

int RECV_PIN = 2;
int hallSensorPin1 = 3;
int state1;
int oldState1 = 1;

Tle94112 controller = Tle94112();
Tle94112Motor motor1(controller);

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{ irrecv.enableIRIn();
  controller.begin();

  motor1.connect(motor1.HIGHSIDE, controller.TLE_HB1);
  motor1.connect(motor1.LOWSIDE, controller.TLE_HB2);
  motor1.setPwm(motor1.HIGHSIDE, controller.TLE_PWM2);
  motor1.setPwmFreq(motor1.HIGHSIDE, controller.TLE_FREQ200HZ);

  motor1.begin();
}

void loop()
{
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume();
  }//add bracket here

  if (results.value == unu || results.value == 0x801 || results.value == 0x2049) {
    motor1.start(63);
  }

  if (results.value == sound || results.value == 0x55 || results.value == 0x2103 || results.value == 0x837 || results.value == 0x37) {
    motor1.start(63);

    oldState1 = 1;
    state1 = digitalRead(hallSensorPin1);

    if (state1 == LOW && oldState1 == 1) {
      motor1.coast();

    }
  }
}

cattledog:
I'm not certain if this iw what Robin2 is saying, but I believe that the motor control needs to be separated from the conditional of reading a new value.

Well spotted. There were two problems and I missed that one. This is the way I would do it

void loop() {

  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume();
 
    if (results.value == unu || results.value == 0x801 || results.value == 0x2049) {
      motor1.start(63);
    }

    if (results.value == sound || results.value == 0x55 || results.value == 0x2103 || results.value == 0x837 || results.value == 0x37) {
      motor1.start(63);

      oldState1 = 1;
    }
  } // ----   added bracket here
  state1 = digitalRead(hallSensorPin1);

  if (state1 == LOW && oldState1 == 1) {
        motor1.coast();
  }
}

...R

Robin2:
Well spotted. There were two problems and I missed that one. This is the way I would do it

void loop() {

if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume();

if (results.value == unu || results.value == 0x801 || results.value == 0x2049) {
      motor1.start(63);
    }

if (results.value == sound || results.value == 0x55 || results.value == 0x2103 || results.value == 0x837 || results.value == 0x37) {
      motor1.start(63);

oldState1 = 1;
    }
  } // ----  added bracket here
  state1 = digitalRead(hallSensorPin1);

if (state1 == LOW && oldState1 == 1) {
        motor1.coast();
  }
}




...R

it stops when the magnetic field is detected for both buttons and when i press a second time any of the buttons, the motor receives a short impulse, and by pressing many times the button, after many impulses , when the magnetic field is not detected anymore, the motor rotates again until the magnetic field is found again.

cattledog:
I’m not certain if this iw what Robin2 is saying, but I believe that the motor control needs to be separated from the conditional of reading a new value.

  if (irrecv.decode(&results)) {

Serial.println(results.value, HEX);
    irrecv.resume();
  }//add bracket here





#include <TLE94112.h>
#include <Tle94112Motor.h>
#include <IRremote.h>

#define unu 0x1
#define sound 0x55

int RECV_PIN = 2;
int hallSensorPin1 = 3;
int state1;
int oldState1 = 1;

Tle94112 controller = Tle94112();
Tle94112Motor motor1(controller);

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{ irrecv.enableIRIn();
  controller.begin();

motor1.connect(motor1.HIGHSIDE, controller.TLE_HB1);
  motor1.connect(motor1.LOWSIDE, controller.TLE_HB2);
  motor1.setPwm(motor1.HIGHSIDE, controller.TLE_PWM2);
  motor1.setPwmFreq(motor1.HIGHSIDE, controller.TLE_FREQ200HZ);

motor1.begin();
}

void loop()
{
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume();
  }//add bracket here

if (results.value == unu || results.value == 0x801 || results.value == 0x2049) {
    motor1.start(63);
  }

if (results.value == sound || results.value == 0x55 || results.value == 0x2103 || results.value == 0x837 || results.value == 0x37) {
    motor1.start(63);

oldState1 = 1;
    state1 = digitalRead(hallSensorPin1);

if (state1 == LOW && oldState1 == 1) {
      motor1.coast();

}
  }
}

this one works fine for continuous rotation, but when the magnetic field is detected, the motor receives small pulses without pressing any button, and again when its not detecting a magnetic field, rotates normally till it finds again the magnetic field.

hi. i want to press “unu” button of the remote, and the motor to rotate continuously, and when i press “sound” i want the motor to rotate untill the hall sensor detects a magnetic field. any ideas please?

OK so you want the motor to stop when the hall sensor detects the field.

Try setting results.value back to 0. when you want to stop the motor until a new ir button press is received. With results.value = 0 your code will not enter any of the motor control sections until a new value is received. Is this what you want?

if (state1 == LOW && oldState1 == 1) {
      motor1.coast();
      results.value = 0;

    }
#include <TLE94112.h>
#include <Tle94112Motor.h>
#include <IRremote.h>

#define unu 0x1
#define sound 0x55

int RECV_PIN = 2;
int hallSensorPin1 = 3;
int state1;
int oldState1 = 1;

Tle94112 controller = Tle94112();
Tle94112Motor motor1(controller);

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{ irrecv.enableIRIn();
  controller.begin();

  motor1.connect(motor1.HIGHSIDE, controller.TLE_HB1);
  motor1.connect(motor1.LOWSIDE, controller.TLE_HB2);
  motor1.setPwm(motor1.HIGHSIDE, controller.TLE_PWM2);
  motor1.setPwmFreq(motor1.HIGHSIDE, controller.TLE_FREQ200HZ);

  motor1.begin();
}

void loop()
{
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume();
  }//add bracket here

  if (results.value == unu || results.value == 0x801 || results.value == 0x2049) {
    motor1.start(63);
  }

  if (results.value == sound || results.value == 0x55 || results.value == 0x2103 || results.value == 0x837 || results.value == 0x37) {
    motor1.start(63);

    oldState1 = 1;
    state1 = digitalRead(hallSensorPin1);

    if (state1 == LOW && oldState1 == 1) {
      motor1.coast();
      results.value = 0;
    }
  }
}