I am making progress with my motor control project, it will spin up to 2K rpm :). This is while using the delay command. I am trying to use milli or even micros. In the sketch below there are two triggers, they both should do same thing, but the trig1/drive1 will turn motor slower then trig2/drive2. It has got to be the way I am using milli? Any help would be greatly appreciated. Rawbush
// constants won't change. They're used here to set pin numbers
#include <Button.h> // http://www.arduino.cc/playground/Code/Button
Button Trig1 = Button (15,PULLUP); // sets analog pin 0 as Trig-1 input, also pulled up
Button Trig2 = Button (16, PULLUP); // sets analog pin 1 as Trig-2 input, also pulled up
const int Drive1 = 17; // sets analog pin 2 as Drive-1 output
const int Drive2 = 18; // sets analog pin 3 as Drive-2 output
const int ONTIME = 2;
// Variables will change:
int Drive1State = 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 = 2; // interval at which to blink (milliseconds)
void setup(){
pinMode (Drive1, OUTPUT); // this is output for Drive-1
pinMode (Drive2, OUTPUT); // this is output for Drive-2
}
void loop()
{
if (millis() - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = millis();
if(Trig1.isPressed()){ // reads the Trigger 1 input and switches Drive-1
if (Drive1State == LOW) {
Drive1State = HIGH;
interval = ONTIME;
}else{
Drive1State = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(Drive1, Drive1State);
}
if (Trig2.isPressed()){ // reads the Trigger 2 input and switches Drive-2
digitalWrite (Drive2, HIGH);
delay (2); // this is the amount of time the Drive-2 pulse is ON
digitalWrite (Drive1, LOW); // forces Drive-2 low...no motor pulse
}else{
digitalWrite (Drive2, LOW); // keeps Drive-2 LOW
}
}
}
Yes the comments may be wrong,still work in progress. But pin assignment and usage works fine. The problem I am having is that the section of code that uses milli doesn't work as well as the one with delay. Could it be that 2ms is to small a time?
I have to admit the code is very confusing. It looks like you are trying to do some simple pulse-width modulation to control the speed of the motors. Why not just use analogWrite() which is meant for this purpose?
The delay(2) function pretty much guarantees that "millis() - previousMillis > interval" is true, so the second button trigger will affect the first motor. Furthermore, delay(2) will delay anywhere from 0 to 2 ms depending upon how far along time is from one millisecond to another.
I would begin by trying to restructure the code to use analogWrite() and put the motors on pins that support PWM.
Here is what I have tried now. Once power is added to arduino motor stays still- this is good. Then after one of the triggers is sensed it pulses but then stays on. A little closer.
// constants won't change. They're used here to set pin numbers
#include <Button.h> // http://www.arduino.cc/playground/Code/Button
Button Trig1 = Button (15,PULLUP); // sets analog pin 1 as Trig-1 input, also pulled up
Button Trig2 = Button (16, PULLUP); // sets analog pin 2 as Trig-2 input, also pulled up
const int Drive1 = 17; // sets analog pin 3 as Drive-1 output
const int Drive2 = 18; // sets analog pin 4 as Drive-2 output
const int ONTIME = 2;
// Variables will change:
int Drive1State = LOW; // ledState used to set the LED
int Drive2State = LOW;
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 = 2; // interval at which to blink (milliseconds)
void setup(){
pinMode (Drive1, OUTPUT); // this is output for Drive-1
pinMode (Drive2, OUTPUT); // this is output for Drive-2
}
void loop()
{
if (millis() - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = millis();
if(Trig1.isPressed()){ // reads the Trigger 1 input and switches Drive-1
if (Drive1State == LOW) {
Drive1State = HIGH;
interval = ONTIME;
// set the LED with the ledState of the variable:
digitalWrite(Drive1, Drive1State);
}else{
Drive1State = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(Drive1, Drive1State);
}
if (Trig2.isPressed()){ // reads the Trigger 2 input and switches Drive-2
if (Drive2State == LOW) {
Drive2State = HIGH;
interval = ONTIME;
// set the LED with the ledState of the variable:
digitalWrite(Drive2, Drive2State);
}else{
digitalWrite (Drive2, LOW); // keeps Drive-2 LOW
}
}
}}
Once power is added to arduino motor stays still- this is good. Then after one of the triggers is sensed it pulses but then stays on.
What does "it pulses" mean?
if(Trig1.isPressed()){ // reads the Trigger 1 input and switches Drive-1
if (Drive1State == LOW) {
Drive1State = HIGH;
interval = ONTIME;
// set the LED with the ledState of the variable:
digitalWrite(Drive1, Drive1State);
}else{
Drive1State = LOW;
}
Consistent indenting would be useful. Putting the opening { on the next line would, too.
If Trig1 is pressed, and the motor was off, turn it on, and set interval. OK. This is happening. Where do you ever turn the motor off?