Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

stepper switch
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

stepper switch

by keithg on Sun Apr 05, 2009 9:24 pm

I have borrowed lines from your switch tutorial but the switch intended to stop the stepper has no effect even though the code downloads and the stepper runs through the procedure back and forth as it should. Would you please have a look? I have been chasing this one for a week!



Code: Select all | TOGGLE FULL SIZE
#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);

  }
}
keithg
 
Posts: 82
Joined: Thu Oct 30, 2008 8:30 pm

Re: stepper switch

by adafruit on Sun Apr 05, 2009 11:16 pm

what is

Code: Select all | TOGGLE FULL SIZE
  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);


  }


supposed to do?

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

Re: stepper switch

by keithg on Mon Apr 06, 2009 7:04 am

These lines are supposed to turn the motor off/on depending upon the state of the switch. Doesn't do a thing but I haven't discovered an alternative.
keithg
 
Posts: 82
Joined: Thu Oct 30, 2008 8:30 pm

Re: stepper switch

by adafruit_support_bill on Mon Apr 06, 2009 5:24 pm

How about the direct approach:

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

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

adafruit_support_bill
 
Posts: 77911
Joined: Sat Feb 07, 2009 10:11 am

Re: stepper switch

by keithg on Mon Apr 06, 2009 7:19 pm

When the sketch compiles, I get this error: error: a function-definition is not allowed here before '{' token.


#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)
{
if (count)
{
count--;
Serial.println ("Forward");
myStepper.step(500);
delay(500);

Serial.println ("Backward");
myStepper.step(-500);
delay(500);
}
}
keithg
 
Posts: 82
Joined: Thu Oct 30, 2008 8:30 pm

Re: stepper switch

by keithg on Mon Apr 06, 2009 7:34 pm

I resolved curly bracket problem but now the sketch runs continuously without stopping and the switch is still not functioning.

#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)
// {
if (count)

count--;
Serial.println ("Forward");
myStepper.step(500);
delay(500);
{
Serial.println ("Backward");
myStepper.step(-500);
delay(500);
}
}
keithg
 
Posts: 82
Joined: Thu Oct 30, 2008 8:30 pm

Re: stepper switch

by adafruit on Tue Apr 07, 2009 12:46 am

Code: Select all | TOGGLE FULL SIZE
#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) {
    return; // stop motor
  }

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

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

  }
}


if you use the Code button, your code will be readable and you'll receive more help

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

Please be positive and constructive with your questions and comments.