Switch problem

I have been piecing together borrowed code, hoping to lace enough together for a hacked scanner stepper that pushes/pulls the light from one end of the (scanner) bed to the other. A switch is necessary at either end of the bed because our students will be changing the code and will certainly cause a collision. The switch insures such an even will be harmless.

The sketch compiles. The back and forth procedure isn’t a problem, but the switch has no affect whatsoever, even though the LED’s flash green to red on the driver board when the switch is closed and the stepper is not running. A simple tactile switch is on pin 3 and neg to the stepper driver board gnd. Is this a software or hardware problem?

#include <Stepper.h>

#define motorStep 400
#define motorPin1 8
#define motorPin2 9
#define motorPin3 10
#define motorPin4 11
#define switchPin 3

int count;
int switchState = HIGH;
int reading;
int previous = LOW;

long time = 0;
long debounce = 200;

Stepper myStepper(motorStep, motorPin1,motorPin2,motorPin3,motorPin4);

void setup() {
myStepper.setSpeed(40);
Serial.begin(9600);
count = 1;

pinMode (switchPin, INPUT);
pinMode (motorPin1, OUTPUT);
pinMode (motorPin2, OUTPUT);
pinMode (motorPin3, OUTPUT);
pinMode (motorPin4, OUTPUT);

}

void loop () {
reading = digitalRead(switchPin);
if (reading ==HIGH && previous == LOW && millis() - time > debounce) {
if (switchState == HIGH)
switchState = LOW;
else
switchState = HIGH;
time = millis();
}
digitalWrite (motorPin1, switchState);
digitalWrite (motorPin2, switchState);
digitalWrite (motorPin3, switchState);
digitalWrite (motorPin4, switchState);

previous = reading;

if (count) {
count–;
Serial.println (“Forward”);
myStepper.step(500);
delay(500);

Serial.println (“Backward”);
myStepper.step(-500);
delay(500);

}
}

I suggest you liberally add more parentheses () and braces {} to your code.

There is at least one issue in your code that will solve.

--Phil.

I am working with several Processing intro books but have a long way to master syntax problems. Would you please see http://pastie.org/437407 and advise which lines need additional brackets? Thanks.

Keith

everywhere you have an if/else statement would be the surest way of preventing errors, but this one looks like it needs braces: else switchState = HIGH; time = millis();

Also, although they are similar in many ways, there are many differences between the syntax used in Arduino and Processing. Arduino uses C++, Processing uses Java

I have tried to simplify the sketch but, still, the switch is not stopping the stepper. Do you see the problem?

#include <Stepper.h>

#define motorStep 400
#define motorPin1 8
#define motorPin2 9
#define motorPin3 10
#define motorPin4 11

int count;
int switchPin = 3;
int val;

Stepper myStepper(motorStep, motorPin1,motorPin2,motorPin3,motorPin4);

void setup() {
myStepper.setSpeed(40);
Serial.begin(9600);
count = 1;
pinMode (switchPin, INPUT);
pinMode (motorPin1, OUTPUT);
pinMode (motorPin2, OUTPUT);
pinMode (motorPin3, OUTPUT);
pinMode (motorPin4, OUTPUT);

}

void loop () {
val = digitalRead (switchPin);
if (val == LOW) {
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, HIGH);
}

if (val == HIGH) {
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);

}

if (count) {
count–;
Serial.println (“Forward”);
myStepper.step(500);
delay(500);

Serial.println (“Backward”);
myStepper.step(-500);
delay(500);

}
}

#include <Stepper.h>

#define motorStep 400
#define motorPin1 8
#define motorPin2 9
#define motorPin3 10
#define motorPin4 11


int count;
int switchPin = 3;
int val;

Stepper myStepper(motorStep, motorPin1,motorPin2,motorPin3,motorPin4);

void setup() {
 myStepper.setSpeed(40);
 Serial.begin(9600);
 count = 1;
 pinMode (switchPin, INPUT);
 pinMode (motorPin1, OUTPUT);
 pinMode (motorPin2, OUTPUT);
 pinMode (motorPin3, OUTPUT);
 pinMode (motorPin4, OUTPUT);
 Serial.println ("Setup Complete");
}

void loop () {
 val = digitalRead (switchPin);
 if (val == LOW) {
   Serial.println ("Switch: LOW");
   digitalWrite(motorPin1, HIGH);
   digitalWrite(motorPin2, HIGH);
   digitalWrite(motorPin3, HIGH);
   digitalWrite(motorPin4, HIGH);
 }

 if (val == HIGH) {
   Serial.println ("Switch: HIGH");
   digitalWrite(motorPin1, LOW);
   digitalWrite(motorPin2, LOW);
   digitalWrite(motorPin3, LOW);
   digitalWrite(motorPin4, LOW);


 }

 if (count) {
   count--;
   Serial.println ("Forward");
   myStepper.step(500);
   delay(500);

   Serial.println ("Backward");
   myStepper.step(-500);
   delay(500);

 }
}

Notice the messages that I’ve added for debugging purposes. Test it now and watch for the messages.

Do you have a pull-up or pull-down resistor for your switch(es)? If not, you will need to add one per switch, about 10k Ohms.

I have a 1k resistor as well as the jumper from pin 3 on gnd. 5v is on the other pole of the switch.

The switch message bounces wildly while forward/backward is stable.

The motor appears to be unaffected but is actually attempting to also jump wildly back and forth between multiple states, just like the switches.

You need to debounce the switch.

http://www.arduino.cc/en/Tutorial/Debounce and then try this http://www.arduino.cc/playground/Code/Debounce