Getting RPM changed back and forth when I click a switch

Yesterday I had some assistance from someone, but it didn't address my main problem. In the code below, I have a bipolar stepper motor attached. I want to attach an ANALOG 2 position switch to my arduino. When the switch is in the off (low) I want the RPM to be 24 but when I click the switch to on (high) I want the code to substitute a different RPM ( say 100). When I turn off the switch I want it to return to the default of 24 rpms....

I cant figure out how to write the code so the RPMS change in response with the switch....Please help....
What kind of command do I need? I'm sure this could help a lot of projects since its covered under the GNU GP License

#include <Stepper.h>
/*   Assumptions:
 *
 *   - stepper motor connected to the 2 phases
 *   - 8V-30V supply connected to Vin (optional, else Arduino Vin is used)

 *
 *  
 * 
 * This software is licensed under the GNU General Public License (GPL) Version
 * 3 or later. This license is described at
 * http://www.gnu.org/licenses/gpl.html
 *
 * Application Version 1.0 -- October 2010 Rugged Circuits LLC
 * http://www.ruggedcircuits.com
 */
 
// Define how many steps there are in 1 revolution of your motor
#define STEPS_PER_REVOLUTION 200

/**********************************************************/
/* YOU SHOULDN'T HAVE TO CHANGE ANYTHING BELOW THIS POINT */
/**********************************************************/
// constants won't change. They're used here to 
// set pin numbers:
const int switchPin = A0;    // select the input pin for the fan
const int fanPin = A2;      // select the output pin for the fan
const int switch2Pin = A4;    // select the input pin for rate
 
// variables will change:
int switchState= 0;  // variable to store the value coming from the sensor
int switch2State = 0;
// 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

// General-purpose LED's, active high
#define LED0_PIN 9    // For motor A
#define LED1_PIN 10   // For motor A
#define LED2_PIN 16   // For motor B
#define LED3_PIN 17   // For motor B

Stepper stepper(STEPS_PER_REVOLUTION, DIR1_PIN, DIR2_PIN);

// Set initial default values
unsigned RPM = 22;
unsigned PWM = 25;
unsigned DIR = 1;
 
void setup()
{
    // initialize the LED pin as an output:
  pinMode(fanPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(switchPin, INPUT);
  pinMode(switch2Pin, INPUT);
  // 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);

  pinMode(LED0_PIN, OUTPUT);
  digitalWrite(LED0_PIN, LOW);
  pinMode(LED1_PIN, OUTPUT);
  digitalWrite(LED1_PIN, LOW);
  pinMode(LED2_PIN, OUTPUT);
  digitalWrite(LED2_PIN, LOW);
  pinMode(LED3_PIN, OUTPUT);
  digitalWrite(LED3_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);

  // 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);

  // Now enable PWM and start motion
  analogWrite(EN1_PIN, PWM);
  analogWrite(EN2_PIN, PWM);
  stepper.setSpeed(RPM);

}

void loop()
{
  
[color=red]  switchState = analogRead(switchPin);    // read the state of the switch value:
  switch2State = analogRead(switch2Pin);  // check if the switch is tripped.
  
 if (switch2State == HIGH)  {
  RPM = 100;  // speeds up motor rpm's
 }
  else {
    RPM = 25;      //if switch is off then goes back to unsigned rpm =25
  }
   [/color]
  if (switchState == HIGH) {     
      
    analogWrite(fanPin, HIGH);  // turn fan on
   PWM = 200;
  } 
  else {
    // turn LED off:
    analogWrite(fanPin, LOW); 
  }
 

  // stop the program for for <sensorValue> milliseconds:

  

  // This is a busy-wait loop until the inter-step time passes
  stepper.step(DIR);

  // Mirror phase outputs in LED's
  digitalWrite(LED1_PIN,
  digitalRead(DIR1_PIN));
  digitalWrite(LED3_PIN,
  digitalRead(DIR2_PIN));

  // Drive fault output LED's as mirror of fault inputs
  digitalWrite(LED0_PIN,
  !digitalRead(FAULT1_PIN));

  }

What is an analog 2 position switch? Never heard of it.

The analogWrite() function does not take HIGH and LOW as the second argument!

It is a toggle switch: see attachment

Different-Types-of-Switches-1-1335089698.jpg

Cashdds:
It is a toggle switch: see attachment

It's still a digital switch. It is either off or on. It is still read with digitalRead().

PaulS:
The analogWrite() function does not take HIGH and LOW as the second argument!

Paul, please do not respond to this topic. I don't need your sarcasm or exclamation points.... I told you I am learning this language so I don't appreciate you trying to make me feel like an idiot because I am not a "neckbeard" who lives in my mom's basement and plays with robots all day.....

A professional response is what I am looking for not:

"Try learning to quote!" or "The analogWrite() function does not take HIGH and LOW as the second argument!"

Cashdds:
Paul, please do not respond to this topic. I don't need your sarcasm or exclamation points....

Nothing sarcastic there! In your place I would have taken it as a strong hint that I was using the function wrongly and gone to look at the documentation.

"The exclamation mark (British English) or exclamation point (American English) is a punctuation mark usually used after an interjection or exclamation to indicate strong feelings or high volume (shouting), and often marks the end of a sentence."

Professional conversations are not punctuated by exclamation points and are not appreciated. The conception is such that the recipient of said remark feels like he is being yelled at like a child who doesn't understand. This code and its evolution is a leaning experience. Not a Teutonic boot camp.

Cashdds:
Yesterday I had some assistance from someone, but it didn't address my main problem.

As far as I can see this is your earlier Thread.

Why have you started a new Thread?

In that other Thread you seemed to be trying to control the speed of a fan (?) with the stepper library. Have we any reason to think this has changed?

Give me a good reason why I should not ask the Moderator to merge this Thread with the earlier one.

...R

I started a new thread because in my previous thread I posted that I needed help with the RPM & switch for a stepper motor:

".... but I cannot figure out what I am doing wrong with the 2nd switch code....
Instead of turning a pin from low to high, I simply want the code to switch the RPM of my stepper motor from X to Y when the switch is ON and from Y to X when I turn it OFF."

However, PaulS, took the thread down the road of trying to tell me how to run my fan with a switch .....that was already working..... I was asking for help with changing my RPM on my stepper pump..... not how to run my fan.

If you feel you should merge these threads, please feel free. I only started the 2nd one so people wouldn't start reading about the "fan" and again overlook the real code question of: How can I change my rpm's on the stepper motor between toggle positions?

How can I change my rpm's on the stepper motor between toggle positions?

As I suggested in the other thread, make a table. Define what the two switches (or are there now only one?) are supposed to do to define the RPM's PWM setting.

Then, use the state of the switch(es) to select the appropriate PWM value.

Then, apply that PWM value to the fan pin, using analogWrite().

PaulS:
As I suggested in the other thread, make a table. Define what the two switches (or are there now only one?) are supposed to do to define the RPM's PWM setting.

Then, use the state of the switch(es) to select the appropriate PWM value.

Then, apply that PWM value to the fan pin, using analogWrite().

Sincerest thanks...

PaulS:
As I suggested in the other thread, make a table. Define what the two switches (or are there now only one?) are supposed to do to define the RPM's PWM setting.

Then, use the state of the switch(es) to select the appropriate PWM value.

Then, apply that PWM value to the fan pin, using analogWrite().

In the code:
// Set initial default values
unsigned RPM = 22;
unsigned PWM = 25;
unsigned DIR = 1;

I have changed the "unsigned RPM" from 22 to 100, then uploaded the new sketch and it runs at 100 RPM without a problem.... without changing the "unsigned PWM = 25".....

Since I am using a Rugged Circuit Motor Driver and supplying power through that....( the arduino milks power off the Motor Driver shield)..... I don't think the PWM matters since both rpm settings run when uploaded at different times....

Sorry, but I cannot understand what you are saying in Reply #12, or elsewhere. You seem to keep mixing up different things - like stirring a fruit-cake mixture.

Can you write a very short program to make your motor run at 24 RPM.
Can you then make a small change to that program to make the motor run at 100 RPM.

If not, do nothing further until you can get those to work.

...R

Robin2:
Sorry, but I cannot understand what you are saying in Reply #12, or elsewhere. You seem to keep mixing up different things - like stirring a fruit-cake mixture.

Can you write a very short program to make your motor run at 24 RPM.
Can you then make a small change to that program to make the motor run at 100 RPM.

If not, do nothing further until you can get those to work.

...R

Robin,

The sketch I have listed above works fine to run the Stepper. I can change the {unsigned RPM = 22} to {unsigned RPM = 100} for the RPM and upload the sketch to my board and it will then run at 100 RPM.

I am only confused on how to write the code to make the switch go from one to another.... I am wondering if a switch/case command may be the easiest......

Did you fix the issue brought up in post #4.

Are you reading switch2pin with a digitalRead()?

Please post you latest code.

Cashdds:
The sketch I have listed above works fine to run the Stepper. I can change the {unsigned RPM = 22} to {unsigned RPM = 100} for the RPM and upload the sketch to my board and it will then run at 100 RPM.

I am only confused on how to write the code to make the switch go from one to another.... I am wondering if a switch/case command may be the easiest......

Presumably all you need to do is change the value of RPM depending on the value of the switch. Something like

switchVal = digitalRead(switchPin);
if (switchVal == LOW) {
   RPM = 22;
}
else {
   RPM = 100;
}

...R

"Did you fix the issue brought up in post #4.

Are you reading switch2pin with a digitalRead()?

Please post you latest code. "

#include <Stepper.h>
/*   Assumptions:
 
 * This software is licensed under the GNU General Public License (GPL) Version
 * 3 or later. This license is described at
 * http://www.gnu.org/licenses/gpl.html
 *
 
 
// Define how many steps there are in 1 revolution of your motor
#define STEPS_PER_REVOLUTION 200

 
// constants won't change. They're used here to 
// set pin numbers:
const int switchPin = A0;    // select the input pin for the fan
const int fanPin = A2;      // select the output pin for the fan
const int switch2Pin = A4;    // select the input pin for rate
 
// variables will change:
int switchState= 0;  // variable to store the value coming from the sensor
int switch2State = 0;
// 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

// General-purpose LED's, active high
#define LED0_PIN 9    // For motor A
#define LED1_PIN 10   // For motor A
#define LED2_PIN 16   // For motor B
#define LED3_PIN 17   // For motor B

Stepper stepper(STEPS_PER_REVOLUTION, DIR1_PIN, DIR2_PIN);

// Set initial default values
unsigned RPM = 22;
unsigned PWM = 25;
unsigned DIR = 1;
 
void setup()
{
    // initialize the LED pin as an output:
  pinMode(fanPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(switchPin, INPUT);
  pinMode(switch2Pin, INPUT);
  // 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);

  pinMode(LED0_PIN, OUTPUT);
  digitalWrite(LED0_PIN, LOW);
  pinMode(LED1_PIN, OUTPUT);
  digitalWrite(LED1_PIN, LOW);
  pinMode(LED2_PIN, OUTPUT);
  digitalWrite(LED2_PIN, LOW);
  pinMode(LED3_PIN, OUTPUT);
  digitalWrite(LED3_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);

  // 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);

  // Now enable PWM and start motion
  analogWrite(EN1_PIN, PWM);
  analogWrite(EN2_PIN, PWM);
  stepper.setSpeed(RPM);

}

void loop()
{
  
  switchState = digitalRead(switchPin);    // read the state of the switch value:
  switch2State = digitalRead(switch2Pin);  // check if the switch is tripped.
  
 if (switch2State == HIGH)  {
  RPM = 100;  // speeds up motor rpm's
 }
  else {
    RPM = 25;      //if switch is off then goes back to unsigned rpm =25
  }
   
  if (switchState == HIGH) {     
      
    analogWrite(fanPin, PWM = 200);  // turn fan on
   
  } 
  else {
    // turn fan off:
    analogWrite(fanPin, PWM = 0); 
  }
 

  // stop the program for for <sensorValue> milliseconds:

  

  // This is a busy-wait loop until the inter-step time passes
  stepper.step(DIR);

  // Mirror phase outputs in LED's
  digitalWrite(LED1_PIN,
  digitalRead(DIR1_PIN));
  digitalWrite(LED3_PIN,
  digitalRead(DIR2_PIN));

  // Drive fault output LED's as mirror of fault inputs
  digitalWrite(LED0_PIN,
  !digitalRead(FAULT1_PIN));

  }

Robin2:
Presumably all you need to do is change the value of RPM depending on the value of the switch. Something like

switchVal = digitalRead(switchPin);

if (switchVal == LOW) {
  RPM = 22;
}
else {
  RPM = 100;
}




...R

I will try this tonight and see what I can make of it...... Thank you, R..... p.s.: I hate fruitcake

You never call

stepper.setSpeed(RPM);

in the loop after you change the RPM with the reading of the switch.