Dear Arduino Community,
I want to add limiting switches (like this one) to my project.
The problem is, I don't know how to connect them to my Arduino Mega, and I don't know how to programe everything up.
Here's what I've done for the moment (please see the picture in the attachments for a better understanding): I connected pin 22 to a 10K ohms resistor (which is in serial with the limiting switch, and this one is connected to the GND of arduino), and to the 5V output from Arduino.
What I want to do is to find a way that everytime the Limiting Switch is pressed, one of the digital pins changes its value to LOW or HIGH, so that I can use this to stop the stepper motors.
To see if I could make the limiting switch work, I wrote a code. This code works with a LCD Shield that I bought. In this code, my goal is simple: every time that I press the limiting switch, I want to print something in the LCD Screen, saying "the PIN is HIGH", or "the PIN is LOW".
Here is the code that I wrote for this:
void menuItem8() { // Function executes when you select the 8th item from main menu
int activeButton = 0;
int val = 0;
digitalWrite (22, LOW);
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Test");
while (activeButton == 0) {
int button;
readKey = analogRead(0);
if (readKey < 790) {
delay(100);
readKey = analogRead(0);
}
val = digitalRead(22);
if (val == HIGH) {
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("HIGH");
delay (1000);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("STILL");
delay (1000);
}
if (val == LOW) {
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("BUTTON");
delay (1000);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("PRESSED");
delay (1000);
}
button = evaluateButton(readKey);
switch (button) {
case 4: // This case will execute if the "back" button is pressed
button = 0;
activeButton = 1;
break;
}
}
}
Anyone have any idea on how I should properly wire the Limiting Switch and/or Program the Arduino so I can change the value of a digital pin and use it to stop some stepper motors?
Thank you, and if I didn't make myself clear, please let me know and I'll try to explain it better.
Joao