Hi ,
I need help with the coding as I cannot get this to work with the code I have.
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.
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...
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".
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....
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.
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;
}
I have no idea. It was in your original code. I was hoping you would know
My guess was that it is used to change direction of the targets just like hitting a limit switch