The sketch, Blink without delay is what I'd like to do, but it is for an LED. One pin only.
The state, int ledState = LOW; is either high or low.
I want to use this with a DC motor, but the motor has two pins. One is high and one is low; that's one direction...one is low and one is high; that's the other direction. And when they are both low, that is stopped.
So my question is, how to I refer to the state of the motor when it has two pins. I want to be able to control the time it's on in one direction, the opposite direction, and how long it's off. I want to use millis instead of delay. How do I refer to the two pins of the motor, like the one pin of the LED?
/* Blink without Delay
Turns on and off a light emitting diode(LED) connected to a digital
pin, without using the delay() function. This means that other code
can run at the same time without being interrupted by the LED code.
The circuit:
* LED attached from pin 13 to ground.
* Note: on most Arduinos, there is already an LED on the board
that's attached to pin 13, so no hardware is needed for this example.
created 2005
by David A. Mellis
modified 8 Feb 2010
by Paul Stoffregen
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/
// constants won't change. Used here to
// set pin numbers:
const int ledPin = 13; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}
void loop()
{
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
However, driving a motor directly from an Arduino pin is not advisable due to the limited current available. You would be better off using a relay or transistors.
Just out of curiosity - you are not thinking of connecting your arduino pins directly to a DC motor, I hope.
If you want a DC motor to chnge directions, the usual solution is a thing called an H-Bridge. The come as a shield which you clip onto the top of your arduino.
I needed to put the ! (NOT) on the state where needed.
I need to think more like a digital IC.
Of course I'm using an H-Bridge...it has become so routine I forgot to mention it. I am new to the Arduino and new to coding, so I'm still banging my head against the wall...but I'm slowly getting it.
This drives a DC motor CW, CCW, and stopped for "interval" length of time at random. It also has a speed control.
If you see anything odd or questionable, let me know.
const int switchPin = 7; // switch input
const int switch2Pin = 8; // switch input
const int motor1Pin = 2; // H-bridge leg 1 (pin 2)
const int motor2Pin = 4; // H-bridge leg 2 (pin 7,)
const int enablePin = 3; // H-bridge enable pin
int potPin = A0;
int motorSpeed = 0;
int motorState = LOW;
long previousMillis = 0;
long interval = 500;
void setup() {
Serial.begin(9600);
// set the switch as an input:
pinMode(switchPin, INPUT);
pinMode(switch2Pin, INPUT);
// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
// set enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
}
void loop() {
unsigned long currentMillis = millis();
int move = random(0, 3);
switch (move) {
case 0:
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
digitalWrite(motor1Pin, motorState);
digitalWrite(motor2Pin, !motorState);
motorSpeed = analogRead(potPin)/4;
analogWrite(enablePin, motorSpeed);
break;
}
case 1:
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
digitalWrite(motor1Pin, !motorState);
digitalWrite(motor2Pin, motorState);
motorSpeed = analogRead(potPin)/4;
analogWrite(enablePin, motorSpeed);
break;
}
case 2:
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
digitalWrite(motor1Pin, motorState);
digitalWrite(motor2Pin, motorState);
break;
}
}
}
If you see anything odd or questionable, let me know.
The timing variables should be unsigned long to avoid problems with millis() rollover
Many of your variables are ints when their value will never exceed 255 so they could be bytes
You do not use the switch inputs declared in the program but you could usefully set them as INPUT_PULLUP to avoid the need for external resistors if/when you do use them.