Go Down

Topic: Case switch instead of if statement (Read 834 times) previous topic - next topic

hun1

Hi again everyone! I'm think of using multiple pushbuttons for a stepper motor project.  I'm thinking like 5 or 6 pushbuttons to tell the stepper motor to go to specified place.  Anyway, i'm wondering if the case switch is better to use then a series of if/then statements for this project.  Also, i'm confused with the case switch in regards to using pushbuttons.  Does anyone know any good tutorials for this besides the provided arduino tutorial on the website? thanks! :)

johnwasser

The switch/case statement is used when you are comparing an integer value against a number of integer constants:

If your code looks something like this:
Code: [Select]

int x = f();

if (x == 1)
   do the stuff for 1;
else
if (x == 2)
   do the stuff for 2;
else
if (x == 5)
   do the stuff for 5;
else
if (x == 17)
   do the stuff for 17;


you can re-write it to be this:

Code: [Select]

int x = f();
switch (x)
    {
case 1:
   do the stuff for 1;
   break;
case 2:
   do the stuff for 2;
   break;
case 5:
   do the stuff for 5;
   break;
case 17:
   do the stuff for 17;
   break;
   }
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

liudr

I don't think you should use switch-case for what you intend to do. A switch-case is used when the input has multiple values but your button can yield only on and off. Just use if statement will do. If you want some efficiency, you can use array to store pins that are connected to buttons and distances that each button represents.

hun1

So I just bought the Rugged Motor Driver for the Arduino and it's cool! In my code I have an if within an if statement and it wont work right.  I want to push a button and have the motor move back to a limiter switch to calibrate itself and then move forward.  But Im just not getting it.  Any suggestions guys?

Code: [Select]



#include <Button.h>
#include <Stepper.h>

#define STEPS_PER_REVOLUTION 200

// Pick correct Arduino development board for Rugged Motor Driver
#define BOARD  0   /* Arduino Duemilanove/Uno (ATmega328P) */
//#define BOARD  1   /* Arduino Mega (ATmega1280) */
//#define BOARD  2   /* Rugged Circuits Gator (ATmega324P) */

#ifndef BOARD
#  if defined(__AVR_ATmega328P__)
#    define BOARD 0
#  elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#    define BOARD 1
#  elif defined(__AVR_ATmega324P__)
#    define BOARD 2
#  else
#    error You must define BOARD near the top of this file
#  endif
#endif

#if (BOARD!=0) && (BOARD!=1) && (BOARD!=2)
#error Unknown board
#endif


// Enable (PWM) outputs
#define EN1_PIN 3
#define EN2_PIN 11

// Direction outputs
#define DIR1_PIN 12
#define DIR2_PIN 13

// Fault inputs, active low
#define FAULT1_PIN 5
#define FAULT2_PIN 8

Stepper stepper(STEPS_PER_REVOLUTION, DIR1_PIN, DIR2_PIN);

// Set initial default values
unsigned RPM = 50;
unsigned PWM = 10;
unsigned DIR = -1;






//create a Button object at pin X
/*
|| Wiring:
|| GND -----/ ------ pin X
*/
Button button = Button(6,PULLUP);
Button limitsw = Button(7,PULLUP);

void setup(){
 
    // Configure all outputs off for now
  pinMode(EN1_PIN, OUTPUT); digitalWrite(EN1_PIN, LOW);
  pinMode(EN2_PIN, OUTPUT); digitalWrite(EN2_PIN, LOW);
  pinMode(DIR1_PIN, OUTPUT); digitalWrite(DIR1_PIN, LOW);
  pinMode(DIR2_PIN, OUTPUT); digitalWrite(DIR2_PIN, LOW);
 
    // Configure fault inputs with pullups
  pinMode(FAULT1_PIN, INPUT); digitalWrite(FAULT1_PIN, HIGH);
  pinMode(FAULT2_PIN, INPUT); digitalWrite(FAULT2_PIN, HIGH);
 
    Serial.begin(9600); // only necessary for debugging

  // Change from divide-by-64 prescale on Timer 2 to divide by 8 to get
  // 8-times faster PWM frequency (976 Hz --> 7.8 kHz). This should prevent
  // overcurrent conditions for steppers with high voltages and low inductance.
  TCCR2B = _BV(CS21);
}

void loop(){

 
//////////// configure button 1 /////////////

  if(button.uniquePress()){
   
  // Now enable PWM and start motion
  analogWrite(EN1_PIN, PWM);
  analogWrite(EN2_PIN, PWM);
  stepper.setSpeed(RPM);
  Serial.println("reverse");
 
 
///////// limit switch for button //////////////
    if (limitsw.uniquePress() && button.wasPressed()){
       
    analogWrite(EN1_PIN, PWM);
    analogWrite(EN2_PIN, PWM);
    stepper.setSpeed(RPM);
    Serial.println("forward");
 
    // # of steps forward//
    stepper.step(600);
 
    // turn off power to the motor //
    digitalWrite(EN1_PIN, LOW);
    digitalWrite(EN2_PIN, LOW);
      }
   }
   // This is a busy-wait loop until the inter-step time passes
   stepper.step(DIR); 

}

AWOL

Quote
I want to push a button and have the motor move back to a limiter switch to calibrate itself and then move forward.  But Im just not getting it

No, we're not getting it either.
You've not described what you observed happening.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up