#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.