I have not received the order yet, I ask this question to have the code ready for when it arrives. I want to be sure that what I wrote will work correctly, I am new to Arduino.
1- As you can see, the sensor has 3 pins, one for 5v, another for GND and another for signals. I will connect one to PWM pin 3 and the other to 4 (Arduino Mega). That's right?
2- I have a simple code and I want to know if it is ok or there is some error. I want the actuator to stop when pressed (I take care of the actuator script):
// Endstop
int endstop_1 = 3;
int endstop_2 = 4;
void setup() {
pinMode(endstop_1, INPUT);
pinMode(endstop_2, INPUT);
Serial.begin(115200);
}
void loop() {
if (digitalRead(endstop_1) == HIGH) { // assumes HIGH = pressed
Serial.println("endstop_1 pressed");
//break; or stop function
}
if (digitalRead(endstop_2) == HIGH) { // assumes HIGH = pressed
Serial.println("endstop_2 pressed");
//break; or stop function
}
}
The wiring seems to be:
Red: +5V to Normally Closed contact and LED
Black: Ground to Normally Open contact.
Green: Signal from Common contact and +5V pull-up.
You would connect Red to +5V, Black to Ground, and Green to an INPUT pin. The board has a pull-up resistor so INPUT_PULLUP is not necessary.
When the switch is not pressed, Signal is connected to +5V through the NC contact and reads HIGH.
When the switch is pressed, Signal is connected to Ground through the NO contact and reads LOW. This also grounds the LED and causes it to light up.