1

with this sketch i can independently control 4 dc motors cw, ccw, and rotating each of them until the hall sensor related to it detects an magnetic field. what im trying to do now is to create a mode of operation in wich:just pressing button "nine", and first motor starts rotating until its hall sensor detects a magnetic field, after five seconds, the second motor acts the same and so on, ather the fourth motor, after 5 seconds, the first motor starts again. i spent like 5 hours trying but still cant do it. maybe for a pro its not that big deal cose its my first serious Arduino project.

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

#define unu 0x1
#define doi 0x2
#define trei 0x3
#define patru 0x4
#define sound 0x55
#define picture 0x54
#define feature 0x50
#define install 0x52
#define cinci 0x5
#define sase 0x6
#define sapte 0x7
#define opt 0x8
#define tv 0x63
#define av 0x56
#define e 0x60
#define eo 0x42

int RECV_PIN = 2;
int hallSensorPin1 = 3;
int hallSensorPin2 = 4;
int hallSensorPin3 = 5;
int hallSensorPin4 = 6;
int state1;
int oldState1 = 1;
int runMode1 = 0;
int state2;
int oldState2 = 1;
int runMode2 = 0;
int state3;
int oldState3 = 1;
int runMode3 = 0;
int state4;
int oldState4 = 1;
int runMode4 = 0;

Tle94112 controller = Tle94112();
Tle94112Motor motor1(controller);
Tle94112Motor motor2(controller);
Tle94112Motor motor3(controller);
Tle94112Motor motor4(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);

  motor2.connect(motor2.LOWSIDE, controller.TLE_HB3);
  motor2.connect(motor2.HIGHSIDE, controller.TLE_HB4);
  motor2.setPwm(motor2.HIGHSIDE, controller.TLE_PWM2);
  motor2.setPwmFreq(motor2.HIGHSIDE, controller.TLE_FREQ200HZ);

  motor3.connect(motor3.HIGHSIDE, controller.TLE_HB5);
  motor3.connect(motor3.LOWSIDE, controller.TLE_HB6);
  motor3.setPwm(motor3.HIGHSIDE, controller.TLE_PWM2);
  motor3.setPwmFreq(motor3.HIGHSIDE, controller.TLE_FREQ200HZ);

  motor4.connect(motor4.LOWSIDE, controller.TLE_HB7);
  motor4.connect(motor4.HIGHSIDE, controller.TLE_HB8);
  motor4.setPwm(motor4.HIGHSIDE, controller.TLE_PWM2);
  motor4.setPwmFreq(motor4.HIGHSIDE, controller.TLE_FREQ200HZ);

  motor1.begin();
  motor2.begin();
  motor3.begin();
  motor4.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);
      runMode1 = 0;
    }
    if (results.value == cinci || results.value == 0x805 || results.value == 0x2053) {
      motor1.start(-63);
      runMode1 = 0;
    }
    if (results.value == tv || results.value == 0x63 || results.value == 0x2111 || results.value == 0x83F || results.value == 0x3F) {
      motor1.coast();
      runMode1 = 0;
    }
    if (results.value == sound || results.value == 0x55 || results.value == 0x2103 || results.value == 0x837 || results.value == 0x37) {
      motor1.start(63);
      runMode1 = 1;
    }
    if (results.value == doi || results.value == 0x802 || results.value == 0x2050) {
      motor2.start(63);
      runMode2 = 0;
    }
    if (results.value == sase || results.value == 0x806 || results.value == 0x2054) {
      motor2.start(-63);
      runMode2 = 0;
    }
    if (results.value == av || results.value == 0x56 || results.value == 0x2104 || results.value == 0x838 || results.value == 0x38) {
      motor2.coast();
      runMode2 = 0;
    }
    if (results.value == picture || results.value == 0x54 || results.value == 0x2102 || results.value == 0x836 || results.value == 0x36) {
      motor2.start(63);
      runMode2 = 1;
    }
    if (results.value == trei || results.value == 0x803 || results.value == 0x2051) {
      motor3.start(63);
      runMode3 = 0;
    }
    if (results.value == sapte || results.value == 0x807 || results.value == 0x2055) {
      motor3.start(-63);
      runMode3 = 0;
    }
    if (results.value == e || results.value == 0x60 || results.value == 0x2108 || results.value == 0x83C || results.value == 0x3C) {
      motor3.coast();
      runMode3 = 0;
    }
   if(results.value==feature||results.value==0x50||results.value==0x2098||results.value==0x832||results.value==0x32){
      motor3.start(63);
      runMode3 = 1;
    }
    if (results.value == patru || results.value == 0x804 || results.value == 0x2052) {
      motor4.start(63);
      runMode4 = 0;
    }
    if (results.value == opt || results.value == 0x808 || results.value == 0x2056) {
      motor4.start(-63);
      runMode4 = 0;
    }
    if (results.value == eo || results.value == 0x42 || results.value == 0x2090 || results.value == 0x82A || results.value == 0x2A) {
      motor4.coast();
      runMode4 = 0;
    }
    if (results.value == install || results.value == 0x52 || results.value == 0x2100 || results.value == 0x834 || results.value == 0x34) {
      motor4.start(63);
      runMode4 = 1;
    }
  }
  state1 = digitalRead(hallSensorPin1);


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

  if (state2 == HIGH) {
    oldState2 = 1;
  }
  if (state2 == LOW && oldState2 == 1 && runMode2 == 1) {
    motor2.coast();
    oldState2 = 0;
  }

  state3 = digitalRead(hallSensorPin3);
  
  if (state3 == HIGH) {
    oldState3 = 1;
  }
  if (state3 == LOW && oldState3 == 1 && runMode3 == 1) {
    motor3.coast();
    oldState3 = 0;
  }
  state4 = digitalRead(hallSensorPin4);
  
  if (state4 == HIGH) {
    oldState4 = 1;
  }
  if (state4 == LOW && oldState4 == 1 && runMode4 == 1) {
    motor4.coast();
    oldState4 = 0;
  }
}
2
  • what problem are you having? ... unable to detect 9 being pressed? ... something else? ... we are not going to spend time guessing what you are observing
    – jsotola
    Jul 17, 2020 at 19:25
  • Timed things are normally done with the millis() or micros() function, like in the BlinkWithoutDelay example. There are tons of tutorials out there about that. For writing code for multiple modes, you should look into the structure of an FSM (Finite State Machine). That is a simple but powerful concept, also often described on the web. I have also once written a longer answer about FSMs here, with diagrams and all. Understanding that concept will really help you
    – chrisl
    Jul 17, 2020 at 19:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.