I have used a microswitch SPDT to position my motor to home. And I have connected the output of this microswitch ( two wires) through a 2m, 12 core sensor actuator cable which leads into the micro controller (Mega256).
By doing so the signals that i receive in my serial monitor is erratic (ON and OFF). The sensor turns on high even when the circuit is not closed (Switch not pressed).
BUT when I directly connect the switch to the microcontroller, I don't face this issue. Is this an EMI or voltage drop concern? How do I get rid of this issue and route the connection through the 12 core cable without having an erratic jumping of signals?
I have not included the program on because I believe it to be a hardware issue. Do let me know if i would need to? Thanks.
Are your switches wired to +5V, or GND? Are your inputs "INPUT", or "INPUT_PULLUP"?
If your switches are wired to ground, and your inputs are "INPUT", your input signals will be very very noise sensitive. If "INPUT_PULLUP", less so, but you may still need to add a small capacitor across the input. I think LarryD is about to ask this as well..
C
With 2 meters of cable that isn't twisted pair and also using a high power rated micro switch, I wouldn't rely on using the internal pullup of the MCU. There would be not near enough wetting current and high susceptibility to noise.
I would recommend using an external 4.7K pullup resistor and 1μF capacitor connected from the input to GND. This will provide adequate wetting current and about 5 ms of hardware debounce, which would be adequate for a micro switch.
#include <Stepper.h>
int dirPin2 =3; // Direction pin feeding motor
int pulPin2 = 2; // Pulse pin feeding motor
int dirPin1 = 5; // Direction pin bending motor
int pulPin1 = 4; // Pulse pin bending motor
int limit1 = 25; // Limit switch for bending motor // Limit switch for secondary motor
int stswitch = 6; // Start switch after homing
int LED_stswitch = 7; // Relay for LED Start switch // Relay for solenoid 2- solenoid for DCV for air nipper
int solrel3 = 12; // relay for solenoid 3- solenoid for DCV for air nipper
int solrel4 = 8; // relay for solenoid 3- solenoid for DCV for air nipper actuating cylinder
int val = limit1;
int oscsteps1 = 4500;
int oscsteps2 = 1200;
int wirefeed = 18600;
Stepper motor2 (200,3,2);
Stepper motor1 (200,5,4);
void homing()
{
//***************************************** Moving bending motor to find home sensor****************************************************//
digitalWrite(dirPin1, HIGH); // Setup bending motor to turn left
do
{
digitalWrite(pulPin1 , HIGH);
delayMicroseconds(1000);
digitalWrite(pulPin1 , LOW);
delayMicroseconds(1000);
}
while (digitalRead(val) == HIGH); // Motor turns left until touches limit 1
Serial.println("triggered-moving to home position");
delay(500);
motor1.step(-900); // Positioning to start of forming
//*************************************************************************************************************************************//
}
//**************************************** Forming and cutting the wire***************************************************************//
void form_cut()
{
int i=10;
motor2.setSpeed(5000);
motor2.step(5*(-wirefeed));
motor2.step(5*(-wirefeed)); // Feeding wire first half
delay (2000);
do
{
motor1.setSpeed(5000); // Winding steps in a loop
motor1.step(-oscsteps1);
delayMicroseconds(50);
motor2.step(-oscsteps2);
delayMicroseconds(50);
motor1.step(oscsteps1);
delayMicroseconds(50);
motor2.step(-oscsteps2);
delayMicroseconds(50);
i=i-1;
}
while(i>0);
delay (2000);
motor2.setSpeed(5000); // Feeding wire second half
motor2.step(5*(-wirefeed));
motor2.step(5*(-wirefeed));
delay(500); // Delay to feed wire with secondary motor-second length
digitalWrite(solrel4,HIGH); // Actuate the cylinder to move the nipper to position
delay (2000);
digitalWrite(solrel4,LOW);
delay (2000);
digitalWrite(solrel3,HIGH); // Actuate the air nipper acutator.
delay (2000);
digitalWrite(solrel3,LOW);
delay (2000);
}
//*************************************************************************************************************************************//
void setup()
{ // Set up stepper motor pins
Serial.begin(9600);
pinMode(dirPin1, OUTPUT);
pinMode(pulPin1, OUTPUT);
pinMode(dirPin2, OUTPUT);
pinMode(pulPin2, OUTPUT);
pinMode(limit1 , INPUT_PULLUP); // Set up the limit switch
pinMode(stswitch, INPUT_PULLUP); // Set up start switch
pinMode(LED_stswitch, OUTPUT);
pinMode(solrel3, OUTPUT);
pinMode(solrel4, OUTPUT);
homing(); // Call homing function
digitalWrite(LED_stswitch,HIGH); // Once homing of both the wires are done, the "ready to start" LED comes on.
}
void loop()
{
while(digitalRead(stswitch) == HIGH); // Condition for after homing- start switch
{ digitalWrite(LED_stswitch,LOW); // Once switch is pressed light goes off.
delay(100); // Delay after homing + time to feed wire with secondary motor- first length
for(int n=5;n>0;n--)
{
form_cut(); // Call forming & cutting function
}
}
while(1);
}
Both slide switches default to having no bounce (clean signal transitions).
You'll need to work on replacing the while loops and delays with non-blocking code. I believe these multiple delays are actually what is causing your erratic operation (this is what the simulator shows).
Check simulator wiring to switches (may need to change default state).