Go Down

Topic: help for if statement (Read 1 time) previous topic - next topic

ksshhs

I wanna write code like below.

when you press a push button, a stepper motor is moving 1000 steps

and if you press the button after motor moves 1000steps, it back to -1000steps.

but it you press the button when motor is performing the 1000 steps it stops.


and this is code I've done so far.




Code: [Select]
/*
Stepper Motor Controller
language: Wiring/Arduino

This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 8 and 9 of the Arduino.

The motor moves 100 steps in one direction, then 100 in the other.

Created 11 Mar. 2007
Modified 7 Apr. 2007
by Tom Igoe

*/

// define the pins that the motor is attached to. You can use
// any digital I/O pins.

#include <Stepper.h>

#define motorSteps 200     // change this depending on the number of steps
// per revolution of your motor
#define motorPin1 6
#define motorPin2 5
#define ledPin 13

int switchPin = 2;              // switch is connected to pin 2
int led1Pin = 13;

int val;                        // variable for reading the pin status
int val2;                       // variable for reading the delayed status
int buttonState;                // variable to hold the button state

int lightMode = 0; 






// initialize of the Stepper library:
Stepper myStepper(motorSteps, motorPin1,motorPin2);



void setup() {
  // set the motor speed at 60 RPMS:
  myStepper.setSpeed(60);

  // Initialize the Serial port:
  Serial.begin(9600);

  // set up the LED pin:
  pinMode(ledPin, OUTPUT);
  // blink the LED:
}

void loop(){
  val = digitalRead(switchPin);      // read input value and store it in val
  delay(10);                         // 10 milliseconds is a good amount of time
  val2 = digitalRead(switchPin);     // read the input again to check for bounces
  if (val == val2) {                 // make sure we got 2 consistant readings!
    if (val != buttonState) {          // the button state has changed!
      if (val == LOW) {                // check if the button is pressed
        if (lightMode == 0) {          // if its off
          lightMode = 1;               // turn lights on!
        } else {
          if (lightMode == 1) {        // if its all-on
            lightMode = 2;             // make it blink!
          } else {
            if (lightMode == 2) {      // if its blinking
              lightMode = 3;           // make it wave!
            } else {
  if (lightMode == 3) { //  if its waving,
                lightMode = 0;           // turn light off!
              }
}
          }
        }
      }
    }
    buttonState = val;                 // save the new state in our variable
  }



  // Now do whatever the lightMode indicates
  if (lightMode == 0) { // all-off
    digitalWrite(led1Pin, LOW);
  }

  if (lightMode == 1) { // all-on
    digitalWrite(led1Pin, HIGH);
  Serial.println("Forward");
  myStepper.step(1000);
  delay(50);

  }

  if (lightMode == 2) { // blinking
    digitalWrite(led1Pin, LOW);

   
  }
  if (lightMode == 3)  { // "wave"

    digitalWrite(led1Pin, HIGH);
Serial.println("Backward");
  myStepper.step(-1000);
  delay(50);

  }   
}

PaulS

Code: [Select]
  myStepper.step(1000);
This is a blocking function. Nothing will happen while the stepper moves 1000 steps. You need to create a for loop, and in that loop step once and check the "Whoa! Stop!!!" switch after each step.
The art of getting good answers lies in asking good questions.

ksshhs

Thanks you for your reply.
I am struggling to write the loop.
so I thinking of using "Digitalwrite" instead of "steps".
so defined steps before setup.
and when the button is pressed when Digitalwrite(motor) is HIGH the motor is stops
and when the button is pressed when   Digitalwrite is LOW the motor is revered the step to go back to original place.
but I am so new to this. so i am not sure i am thinking in a right way

would you be give me a little tip or help?

PaulS

Quote
I am struggling to write the loop.

Why? for loops are simple:
Code: [Select]
for(int i=0; i<1000; i++)
{
   myStepper.step(1);
}

This accomplishes the same thing as myStepper.step(1000) does, BUT, you can add additional code in the for loop. Like, maybe:

Code: [Select]
for(int i=0; i<1000; i++)
{
   myStepper.step(1);
   if(digitalRead(whoaPin) == HIGH)
      break;
}

so that when the whoa pin goes HIGH, the loop is (prematurely) terminated.

You can record the value of i at which that happened, if you want to reposition the stepper at the point it was when the loop started.
The art of getting good answers lies in asking good questions.

ksshhs

#4
May 08, 2013, 04:42 pm Last Edit: May 08, 2013, 04:45 pm by ksshhs Reason: 1
thank you for quick reply again.

so now my code is like this. I just add the code you wrote in the beginning of loop.
but what it does now is just move 1 step in the middle of 1000 steps and it carries on the 1000 stepes in stead of just stop.
what did i wrong?

Code: [Select]
/*
Stepper Motor Controller
language: Wiring/Arduino

This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 8 and 9 of the Arduino.

The motor moves 100 steps in one direction, then 100 in the other.

Created 11 Mar. 2007
Modified 7 Apr. 2007
by Tom Igoe

*/

// define the pins that the motor is attached to. You can use
// any digital I/O pins.

#include <Stepper.h>

#define motorSteps 200     // change this depending on the number of steps
// per revolution of your motor
#define motorPin1 6
#define motorPin2 5
#define ledPin 13

int switchPin = 2;              // switch is connected to pin 2
int led1Pin = 13;

int val;                        // variable for reading the pin status
int val2;                       // variable for reading the delayed status
int buttonState;                // variable to hold the button state

int lightMode = 0;  






// initialize of the Stepper library:
Stepper myStepper(motorSteps, motorPin1,motorPin2);



void setup() {
 // set the motor speed at 60 RPMS:
 myStepper.setSpeed(60);

 // Initialize the Serial port:
 Serial.begin(9600);

 // set up the LED pin:
 pinMode(ledPin, OUTPUT);

 // blink the LED:
 
 

}




void loop(){
 
  for(int i=0; i<1000; i++)
{
  myStepper.step(1);
  if(digitalRead(switchPin) == HIGH)
     break;
}
 
 val = digitalRead(switchPin);      // read input value and store it in val
 delay(10);                         // 10 milliseconds is a good amount of time
 val2 = digitalRead(switchPin);     // read the input again to check for bounces
 if (val == val2) {                 // make sure we got 2 consistant readings!
   if (val != buttonState) {          // the button state has changed!
     if (val == LOW) {                // check if the button is pressed
       if (lightMode == 0) {          // if its off
         lightMode = 1;               // turn lights on!
       } else {
         if (lightMode == 1) {        // if its all-on
           lightMode = 2;             // make it blink!
         } else {
           if (lightMode == 2) {      // if its blinking
             lightMode = 3;           // make it wave!
           } else {
 if (lightMode == 3) { //  if its waving,
               lightMode = 0;           // turn light off!
             }
}
         }
       }
     }
   }
   buttonState = val;                 // save the new state in our variable
 }

 // Now do whatever the lightMode indicates
 if (lightMode == 0) { // all-off
   digitalWrite(led1Pin, LOW);
 }

 if (lightMode == 1) { // all-on
   digitalWrite(led1Pin, HIGH);
 myStepper.step(1000);
 }

 if (lightMode == 2) { // blinking
   digitalWrite(led1Pin, LOW);
 myStepper.step(0);
   
 }
 if (lightMode == 3)  { // "wave"

   digitalWrite(led1Pin, HIGH);
  myStepper.step(1000);

 }    
}



PaulS

Code: [Select]
  if (lightMode == 1) { // all-on
    digitalWrite(led1Pin, HIGH);
  myStepper.step(1000); // <=== You need to replace this line with the code below
  }


Code: [Select]
   for(int i=0; i<1000; i++)
   {
      myStepper.step(1);
      if(digitalRead(switchPin) == HIGH)
         break;
   }


And, do yourself a favor. Learn what Tools + Auto Format does, and become a regular user.
The art of getting good answers lies in asking good questions.

Go Up