changing back and forth motor rpm

Help!

I have successfully written the code to turn on and off a small computer fan with a 2 position toggle switch, 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.

I am using a redboard with arduino uno with a Rugged Motor Driver board stacked on it because the Stepper is a 24v motor and the Redboard cant handle that voltage.

Any advice would be soooooo helpful.... Code to follow in separate post...gotta figure out how to post it correctly..... still new at this....
Thank you,
J.C.

Welco_motor_2ml_4min_with_switch_2.ino (4.33 KB)

  // initialize the pushbutton pin as an input:
  pinMode(switchPin, INPUT);
  pinMode(switch2Pin, INPUT);

Some more details about this switch would be useful. Why is a toggle switch connected to two input pins? A toggle switch is a light switch. It is either on or off. That is, there is one input - HIGH or LOW. A complete schematic is in order.

  // Configure fault inputs with pullups
  pinMode(FAULT1_PIN, INPUT); digitalWrite(FAULT1_PIN, HIGH);
  pinMode(FAULT2_PIN, INPUT); digitalWrite(FAULT2_PIN, HIGH);

ONE statement per line! INPUT_PULLUP as the mode would eliminate the need for the second statement.

 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) {     
    // turn fan on:    
    digitalWrite(fanPin, HIGH); 
   PWM = 200;
  } 
  else {
    // turn LED off:
    digitalWrite(fanPin, LOW); 
  }

After determining the appropriate fan speed, you simply turn the fan on or off. Seems pointless to determine the fan speed, then.

#include <Stepper.h>
/* This Rugged Motor Shield application demonstrates keyboard control
 * of a stepper motor. Assumptions:
 *
 *   - stepper motor connected to the 2 phases
 *   - 8V-30V supply connected to Vin (optional, else Arduino Vin is used)
 *   - Vin jumper is cut (J21) (only required if Vin>15V)
 *   - FAULT1 and FAULT2 jumpers (J10/J14) installed (optional)
 *   - LED jumpers (J15, J16, J6, J7) installed (optional)
 *
 * The behavior is as follows:
 *
 *   - The 'f' key sets forward motion
 *   - The 'b' key sets backwards motion
 *   - The 'a' key accelerates rotational speed
 *   - The 'd' key decelerates rotation speed
 *   - The 'o' key increases motor power (PWM higher duty cycle)
 *   - The 'm' key decreases motor power (PWM lower duty cycle)
 * 
 * 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 potentiometer
const int fanPin = A2;      // select the pin for the fan
const 
nt 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
signed 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()
{
    // read the state of the pushbutton value:
  switchState = digitalRead(switchPin);
  switch2State = digitalRead(switch2Pin);
  // check if the pushbutton is pressed.
  
 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) {     
    // turn fan on:    
    digitalWrite(fanPin, HIGH); 
   PWM = 200;
  } 
  else {
    // turn LED off:
    digitalWrite(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));

  }

"ONE statement per line! INPUT_PULLUP as the mode would eliminate the need for the second statement."

As I said, I am NEW and am trying to learn the language as I go with experiments. I am sorry if my code modifications or use of terms is extraneous or redundant...... I am totally learning here....

As I said, I am NEW and am trying to learn the language as I go with experiments.

Fine. But, just like writing, there are conventions that should be followed, like using punctuation, one though per sentence, etc. One of the established conventions in one statement per line.

  pinMode(FAULT1_PIN, INPUT); digitalWrite(FAULT1_PIN, HIGH);

should be:

  pinMode(FAULT1_PIN, INPUT);
  digitalWrite(FAULT1_PIN, HIGH);

That you haven't encountered INPUT_PULLUP yet, or have elected not to use it is fine.

"Why is a toggle switch connected to two input pins?"

one toggle is for the fan: switchPin
one is the new one for changing the rate: switch2Pin.....
is that not the right way to define 2 separate toggles? :confused:

You need to explain the "two position toggle switch". How many legs does it have? Is it a single pole, double throw switch? A single pole, single throw switch?

Oh,
it has 3 legs and looks like this except it does not light up...2 position-----on or off

2 position-----on or off

So, current goes in one pin, and out, or not out, the other. That is not going to be connected to two input pins. I have no idea why the switch has three legs. You'll need a multi-meter to determine which leg does what (or does nothing).

Im sorry not to have a picture of the setup as I am at work right now.....

the center pole is connected to the input pin..... one of the other legs is connected to the ground.
That is the setup for the fan and it works fine with the existing code.

I took the second switch and attached the center pole to A4 and one of the other poles to ground just like the fan switch but I don't know how to make the code switch to one RPM to another.....

Don't you have to connect the switch to an input AND a ground?? What am I missing about that?

I have checked to make sure these 2 pins close a circuit when the switch is thrown...

Don't you have to connect the switch to an input AND a ground?? What am I missing about that?

I would. But, having done that, I would use the INPUT_PULLUP mode.

There are other ways to connect switches.

Define a table.
If switch one is open and switch two is open, what PWM value should be used?
If switch one is open and switch two is closed, what PWM value should be used?
If switch one is closed and switch two is open, what PWM value should be used?
If switch one is closed and switch two is closed, what PWM value should be used?

Where do you actually analogWrite() that value to the fan?

if (switchState == HIGH) {     
    // turn fan on:    
    digitalWrite(fanPin, HIGH); 
   PWM = 200;
  } 
  else {
    // turn LED off:
    digitalWrite(fanPin, LOW); 
  }

Cashdds:

if (switchState == HIGH) {     

// turn fan on:   
    digitalWrite(fanPin, HIGH);
  PWM = 200;
  }
  else {
    // turn LED off:
    digitalWrite(fanPin, LOW);
  }

Another non-sequitar. I don't know which of the questions that have been asked this is supposed to be an answer to. Try learning to quote.

If this is supposed to be related to where you analogWrite() the PWM value to the fan, it does not answer the question because it does NOT analogWrite() the PWM value to the pin that the fan is connected to.

"Small computer fan" is likely to be a brushless one with its own control circuitry inside. Just changing the input voltage or (even worse: PWM) will not change the fan speed. The internal controller will try to correct for that.

Can you confirm that it is a two-wire brushed motor?

#include <Stepper.h>
/* This Rugged Motor Shield application demonstrates keyboard control
 * of a stepper motor. Assumptions:
 *
 *   - stepper motor connected to the 2 phases
 *   - 8V-30V supply connected to Vin (optional, else Arduino Vin is used)
 *   - Vin jumper is cut (J21) (only required if Vin>15V)
 *   - FAULT1 and FAULT2 jumpers (J10/J14) installed (optional)
 *   - LED jumpers (J15, J16, J6, J7) installed (optional)
 *
 * The behavior is as follows:
 *
 *   - The 'f' key sets forward motion
 *   - The 'b' key sets backwards motion
 *   - The 'a' key accelerates rotational speed
 *   - The 'd' key decelerates rotation speed
 *   - The 'o' key increases motor power (PWM higher duty cycle)
 *   - The 'm' key decreases motor power (PWM lower duty cycle)
 * 
 * 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 potentiometer
const int fanPin = A2;      // select the 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
signed 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()
{
  // read the state of the pushbutton value:
  switchState = digitalRead(switchPin);
  switch2State = digitalRead(switch2Pin);
  // check if the pushbutton is pressed.
  
 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) {     
    // turn fan on:    
    digitalWrite(fanPin, HIGH); 
   PWM = 200;
  } 
  else {
    // turn LED off:
    digitalWrite(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));

  }

"Another non-sequitar. I don't know which of the questions that have been asked this is supposed to be an answer to.Try learning to quote."

Paul, I appreciate your help with this. I will quote you from now on so there is no confusion...
And, yes, the post was related to the analogRead response you gave. It appears that I made a simple mistake in the code and put digital instead of analogRead Apparently the fan then runs on the standard HIGH voltage going to the analog pin when it is thrown...
I will make the appropriate corrections.....

yes.... it is a mini 12v brushless fan

Again, thank you for your assistance.