1

This is the program I have used. It's working fine. But I don't known why sometimes it operates on its own. It is used to control a welding machine (Arduino is isolated from earthing). When there is a little vibration the code executes a command. This is the code:

int a = 9;  //right actuator forward relay 1
int b = 12; //right actuator reverse relay 2 (orange 10)
int c = 10; //left actuator forward relay 4
int d = 11; //left actuator reverse relay 3
int x = 0;
int y = 0;
const int stepPin = 8; // HIGH =DOWN; LOW == UP
const int dirPin = 7;

void setup() {
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(4, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  Serial.begin(9600);
  digitalWrite(b, HIGH);
  digitalWrite(d, HIGH);
  delay(2000);
  digitalWrite(b, LOW);
  digitalWrite(d, LOW);
  delay(1000);
  Serial.println("press the foot swtich"); // Serial Monitor for Debugging
}

void loop() {
  int Val = digitalRead(5); //   Actuator 1 Clamping
  int ValR = digitalRead(6);//   Actuator 2 Clamping
  int ValS = digitalRead(4);//Stepper Motor anticlockwise
  int ValM = digitalRead(2);//Stepper Motor Clockwise
  if (Val == LOW && x == 0  ) {
    Serial.print("Val000=");
    Serial.println(Val);
    digitalWrite(c, HIGH);
    delay(1750);
    digitalWrite(c, LOW);
    x = 1;
    Val = digitalRead(5);
    Serial.println("fwd");
    Serial.print("Val=");
    Serial.println(Val);
    delay(500);
  }
  if (Val == LOW && x == 1  ) {
    digitalWrite(d, HIGH);
    digitalWrite(b, HIGH);
    delay(2000);
    digitalWrite(d, LOW);
    digitalWrite(b, LOW);
    x = 0;
    Val = digitalRead(5);
    Serial.println("fwd");
    y = 0;
    Serial.print("Val=");
    Serial.println(Val);
    delay(500);
  }
  if (ValR == LOW && y == 0 ) {
    digitalWrite(a, HIGH);
    delay(1350);
    digitalWrite(a, LOW);
    y = 1;
    ValR = digitalRead(6);
    Serial.println("fwd");
    Serial.print("Val=");
    Serial.println(Val);
    delay(500);
  }
  if (ValR == LOW && y == 1 ) {
    digitalWrite(b, HIGH);
    delay(1200);
    digitalWrite(b, LOW);
    y = 0;
    ValR = digitalRead(6);
    Serial.println("fwd");
    Serial.print("Val=");
    Serial.println(Val);
    delay(500);
  }
  if (ValS == HIGH) {
    Serial.println(Val);
    Serial.println("up");
    digitalWrite(dirPin, HIGH);
    // Enables the motor to move in a particular direction
    // Makes 100 pulses for making half cycle rotation
    for (int x = 0; x < 100; x++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(1000);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(1000);
    }
    // Enables the motor to move in a particular direction
    // Makes 200 pulses for making one full cycle rotation
  }
  if (ValM == LOW) {
    Serial.println("down");
    Serial.println(ValR);
    digitalWrite(dirPin, LOW);
    // Enables the motor to move in a particular direction
    // Makes 200 pulses for making one full cycle rotation
    for (int x = 0; x < 100; x++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(1000);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(1000);
    }
    // Enables the motor to move in a particular direction
    // Makes 200 pulses for making one full cycle rotation
  }
}
  • 1
    1. By “it operates on its own” do you mean it acts as if the buttons are pressed, including printing the debugging output? 2. The internal pullups enabled by INPUT_PULLUP are quite weak (around 30 kΩ). In a noisy environment, you way want to use stronger external pullups (i.e. lower resistance). – Edgar Bonet Jun 21 '18 at 17:32
  • yes its act like the button is pressed, on serial debugging also and the machine runs too. Can debouncing solve if yes how can I add it? – Karan Shete Jun 21 '18 at 17:37
  • Debouncing may help if it does also deglitching. But it is probably safer to have a clean signal to start with. Thus I would try the weak pullup first. How much distance is there between the buttons and the Arduino board? – Edgar Bonet Jun 21 '18 at 18:59
  • What exactly is happening? What messages are you seeing on the serial monitor? Could it be loose wires or wires touching? More information will help us help you. – sa_leinad Jun 22 '18 at 3:57
  • 1
    I would go straight to 1 kΩ. Also, if your buttons are more than, say, 10 cm from the Arduino board, I would wire them with a twisted pair (signal and ground on the same pair) in order to prevent inductive pick-up. – Edgar Bonet Jun 22 '18 at 7:27
1

Trying to write a proper answer...

According to your description, it looks like you are victim of inductive noise pickup. The circuit [MCU → Arduino ground pin → foot pedal → Arduino digital input pin → MCU] is a loop that can pick up magnetic flux. If this flux varies, either because the magnetic field varies (ambient electromagnetic noise) or because the geometry of the circuit changes (you move the wires), this will induce an electromagnetic force that can make your Arduino read LOW even when the button is not pressed.

There are two things you can try to overcome this effect:

  1. Replace the weak internal pullup of the Arduino by a much stronger external pullup. Something around 1 kΩ should make it quite hard for the noise to pull the signal low.
  2. Connect the pedal to the Arduino board using a twisted pair of wires, like the ones found in telephone or network cables. This kind of cable does a good job at minimizing inductive pickup.
  • I will try with 1k resistor. The Button is actually a foot switch at a distance of 10 feet from arduino – Karan Shete Jun 22 '18 at 7:54
  • The problem is same with 1K resistor too.. the wires are shielded properly – Karan Shete Jun 24 '18 at 12:51
  • It only happens when welding gun relay is activated – Karan Shete Jun 24 '18 at 12:52
  • The welding gun may be a source of huge electromagnetic noise. – Edgar Bonet Jun 24 '18 at 13:19
  • what can i do now? – Karan Shete Jun 24 '18 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.