Moving target for shooting practice

Hi ,
I need help with the coding as I cannot get this to work with the code I have. :slight_smile:
And my codingskills are just not there.....
I am using a H-Bridge for the motor that runs the target, two switches, one at each end of the track.
Also using two potentiometers for regulating speed and delay.

I got it working with one switch and just need help figuring out coding the second.

Enklare__lgbana_211212.ino (1.6 KB)
Enklare__lgbana.ino (1.8 KB)

Hi @dkmart
In your code I didn't find the definitions of
"two switches, one at each end of the track."
what you refer to in the text.

1 Like

Sorry, missed to upload the one with two switches.
It is in the original post now.

why do you need two 'buttons' in code? The target can only be at one end or another, but not both, so it can;t trigger both swithces at once. So use two switches on same circuit...

Thanks for the reply @XRAD !

They are buttons at the moment on my breadboard.
But the will be limitswitches, "endswitches" on the track.
It triggers a stop and then a delay to start going "back".

Hi @dkmart
try this code:

switchLeft is left final switch connected pin 2

switchRight is right final switch connected pin 3

//https://howtomechatronics.com/tutorials/arduino/arduino-dc-motor-control-tutorial-l298n-pwm-h-bridge/

#define switchLeft 2
#define switchRight 3
#define enA 9
#define in1 6
#define in2 7
#define button 4
bool rotDirection = false;
const int delayPot = A1; // fördröjningspot
int delayValue = 0; // värde fördröjningspot
//----------------------------------------------------------------------------
void setup() {
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(button, INPUT);
  pinMode(switchLeft, INPUT);
  pinMode(switchRight, INPUT);
  // Set initial rotation direction
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  Serial.begin(9600);
}
//----------------------------------------------------------------------------
void loop()
{

  int potValue = analogRead(A0);            // Read potentiometer value
  Serial.println(potValue);
  delayValue = analogRead (delayPot);
  delayValue = map(delayValue, 0, 1023, 1000, 10000);
  int pwmOutput = map(potValue, 0, 1023, 0 , 255);       // Map the potentiometer value from 0 to 255
  analogWrite(enA, pwmOutput);                           // Send PWM signal to L298N Enable pin

  if (digitalRead(button) == LOW && digitalRead(switchLeft) == HIGH)
  {
    rotDirection = true;
  }
  if (digitalRead(button) == LOW  && digitalRead(switchRight) == HIGH)
  {
    rotDirection = false;
  }
  if (rotDirection == false && digitalRead(switchRight) == LOW)
  {
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    delay (delayValue);
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
  }
  if (rotDirection == true && digitalRead(switchRight) == LOW)
  {
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    delay (delayValue);
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
  }
}
1 Like

yw. I understand the design. You may want to use an additional millis() timer or debounce > than 20ms for limit switching as 'turn around time' (ie: motor stopping and reversing) may take longer. Once you get the basic reversing working, you can add a 'soft' start up /shut down code to motor PWM....

OR: instead of a hard limit switch at end of track, have track wheel trigger switch BEFORE end of track...then you won't have that hard slam reverse....

1 Like

Thanks @ruilviana

This does not start the motor at all. (I might have broken the H-Bridge)
If I understand correct, you are using rotDirection to run this?
My thought was to use it as an controlmethod, if I would use megnetic switch.
Then it would enable one switch both back an forth.

Hi
It may not be starting the engine because the switches are inverted.
Post a schematic of how you are connecting everything that facilitates my understanding of how the switch logic should be.

1 Like

How do you have your limit switches (or pushbuttons) wired up? They are declared as INPUT, so they require a pull-up resistor. Have you installed one? The best way to wire them up is one side to your pin, the other to ground and declare them as INPUT_PULLUP. They will ready HIGH when not pressed and LOW when pressed.

Also, based on your direction of travel, you only want to check the limit switch you are moving towards since, during a turn-around, the current limit switch can still be closed.

//https://howtomechatronics.com/tutorials/arduino/arduino-dc-motor-control-tutorial-l298n-pwm-h-bridge/

const byte switchLeft = 2;
const byte switchRight = 3;
const byte button = 4;
#define enA 9
#define in1 6
#define in2 7
bool movingLeft;
const int speedPot = A0;
const int delayPot = A1; // fördröjningspot

int delayValue = 0; // värde fördröjningspot
int previousButtonState;

//----------------------------------------------------------------------------
void setup() {
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(button, INPUT_PULLUP);
  pinMode(switchLeft, INPUT_PULLUP);
  pinMode(switchRight, INPUT_PULLUP);
  // Set initial rotation direction
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  movingLeft = true;
  Serial.begin(9600);
}
//----------------------------------------------------------------------------
void loop()
{
  int potValue = analogRead(speedPot);            // Read potentiometer value
  Serial.println(potValue);
  int pwmOutput = map(potValue, 0, 1023, 0 , 255);       // Map the potentiometer value from 0 to 255
  analogWrite(enA, pwmOutput);                           // Send PWM signal to L298N Enable pin

  delayValue = analogRead (delayPot);
  delayValue = map(delayValue, 0, 1023, 1000, 10000);

  // Read button - Debounce
  if (digitalRead(button) == LOW) {
    changeDirection();
    while (digitalRead(button) == LOW) {
      delay(20);
    }
  }

  if (movingLeft  && digitalRead(switchLeft) == LOW) {
    changeDirection();
  }
  if (!movingLeft  && digitalRead(switchRight) == LOW) {
    changeDirection();
  }
}

void changeDirection() {

  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  delay (delayValue);

  if ( movingLeft ) {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
  }
  else {
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
  }
  movingLeft = !movingLeft;
}
1 Like

Thanks @blh64 for the code.
Thats exactly what I tried to explain earlier. :slight_smile:

Changed my switches and tested, works like I want it to! Great!

One thing I didnt understand was the button, What does it do? Is it for starting?
Leftswitch and Rightswitch I get.

I have no idea. It was in your original code. I was hoping you would know :slight_smile:
My guess was that it is used to change direction of the targets just like hitting a limit switch

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.