Limit switch coding

Hi there.

I’m a CNC programmer by trade. I’m pretty new to coding the Arduino.

I am writing a prog to run two DC motors as a simple power feed set up for my manual milling machine at home. Yes I could use steppers but I love the challenge/learning the hard way. I also have some big DC motors on hand and understand PWM and H Bridge Drivers. Ultimately I will add encoders and go full CNC.

In my setup I have the main loop as Joy Stick Jog. Interrupt Pin 1 is the Power Feed Mode and Interrupt Pin 2 will be Incremental Jog but I haven’t cracked multi button Debounce/State Change yet but I will soon.

The problem I am having is I am trying to add limit switches the the Joy Stick Mode initially. I have the switch set up to Pin 23 on the Mega and is giving me the correct output in the Serial Monitor…when pressed Pin 23 goes LOW.

When I include this as an && in my IF Statement and I press it it shows LOW as expected but When I move the Joy Stick the motor still turns and the PIN 23 is driven HIGH in the serial. What is causing it to go HIGH when I command the motor to turn?

int LimitX = digitalRead(LimitXpos);

if ((inputXValue < 470) && (LimitX = HIGH)) {
digitalWrite(directionX, HIGH);
outputXValue = map(inputXValue, 470, 0, 0, 5);

I hope this makes some kind of sense?

This is my first post ever BTW

Many thanks if you can help?


  if ((inputXValue < 470) && (LimitX = HIGH)) {

You are setting LimitX to HIGH instead of testing its value

== is a comparison, = is an assignment.




What plonker