SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By Plasticserpent
#193833
I have only 2 months experience with anything Arduino, and have become stumped by what is probably very simple to the more experienced.
The following sketch is an example of what I'm trying to accomplish. It runs with no problems controlling a DC motors behaviors using an Arduino Uno, and an Adafruit DRV8871 motor driver breakout board. The hardware includes a potentiometer which will take manual control of the motor whenever it is not turned to zero. The void loop begins with the "if" statement regarding the position of the pot. If it turns out to be at the minimum position, the "else" statement gives control of the motor to the program which is made up of sections. Each section represents the complete instructions for a particular motor behavior. The example sketch has 4 separate behaviors, Rumble, 40 dual speed pulse at 4 dif RPMs, Simi fast ramp up 50 to 255, and 30 mixed pulses at medium speeds. These 4 behaviors run continuously, in sequence without any problem.

Here is where I got stumped. I need to add a momentary, pushbutton switch to the setup that causes each behavior to loop continuously until it is pushed once, to cause the program to move ahead to the next behavior, and then continue that one until the button is pushed again. I need that button to be in control as soon as the "else" says the pot is not in control. I have read, and watched tutorials until I feel like my eyes are going to bleed, and I still have two problems. I can't figure out where to hook up that switch, even though I am pretty sure it will be one of the analog inputs on the Arduino.
(I use A2 for the potentiometer) but I could sure use a little coaching on the physical wiring for that switch. My huge problem though, is that I am completely stumped on the code to work that button into my already working sketch.
Here's the example sketch.

int motor= 9; // OK to leave as 9 on pro trinket
int (speedInput)= 10; // OK to leave as 10 on pro trinket
int x;
Void setup() {
pinMode( 9 , OUTPUT); // Must be a PWM pin
pinMode( 10 , OUTPUT); //pot value /4
}
void loop() {
int speedInput = analogRead( 2 );
if (speedInput > 5) // pot is not at minimum position
{
analogWrite( 10 , speedInput/4);
}
else // allow the program to control the motor
{
// Rumble ======================================

analogWrite( 9 ,65); // 25% duty cycle
delay(10000); // play for 10 sec

// 40 two speed pulse at four dif RPMs ================
int maxCount = 10; // loop 10 times
for (int count = 0; count < maxCount ; count++) // count
{
analogWrite( 9 ,115 ); // 45% duty cycle
delay(250); // play for 0.25s

analogWrite( 9 , 45 ); // 18% duty cycle
delay(1500); // play for 1.5 sec
}

for (int count = 0; count < maxCount ; count++) // count
{
analogWrite( 9 ,170 ); // 67% duty cycle
delay(250); // play for 0.25s

analogWrite( 9 , 45 ); // 18% duty cycle
delay(1500); // play for 1.5 sec
}

for (int count = 0; count < maxCount ; count++) // count
{
analogWrite( 9 ,215 ); // 84% duty cycle
delay(250); // play for 0.25s

analogWrite( 9 , 45 ); // 18% duty cycle
delay(1500); // wait for 1.5 sec
}

for (int count = 0; count < maxCount ; count++) // count
{
analogWrite( 9 ,255 ); // 100% duty cycle
delay(250); // play for 0.25s

analogWrite( 9 , 45 ); // 18% duty cycle
delay(1500); // wait for 1.5 sec
}
//Simi fast Ramp up 20 to 255 ========================

for( x = 50 ; x < 255 ; x++ )
{
analogWrite( 9 , x );
delay (70);
}
analogWrite( 9 , 0 );
delay(600);

// 30 mixed pulses at medium speeds =============

for (int count = 0; count < maxCount ; count++) // count
{
analogWrite( 9 ,90 ); // 35% duty cycle
delay(100); // play for 0.1s

analogWrite( 9 , 0 ); // 0% duty cycle (off)
delay(70); // wait .07 seconds

analogWrite( 9 , 220 ); // 86% duty cycle (off)
delay(40); // play for .4 sec

analogWrite( 9 , 0 ); // 0% duty cycle (off)
delay(70); // wait .07 seconds
}
for (int count = 0; count < maxCount ; count++) // count
{
analogWrite( 9 ,90 ); // 35% duty cycle
delay(100); // play for 0.1s

analogWrite( 9 , 0 ); // 0% duty cycle (off)
delay(70); // wait .07 seconds

analogWrite( 9 , 220 ); // 86% duty cycle
delay(40); // play for .04 sec

analogWrite( 9 , 0 ); // 0% duty cycle (off)
delay(70); // wait .07 seconds
}
for (int count = 0; count < maxCount ; count++) // count
{
analogWrite( 9 ,90 ); // 35% duty cycle
delay(100); // play for 0.1s

analogWrite( 9 , 0 ); // 0% duty cycle (off)
delay(70); // wait .07 seconds

analogWrite( 9 , 220 ); //86% duty cycle
delay(40); // play for .04 sec

analogWrite( 9 , 0 ); // 0% duty cycle (off)
delay(70); // wait .07 seconds
}
}

Any help with this will be greatly appreciated.
#193932
No need to use and analog pin for a button. It will only have 2 states pressed or not. So a digital pin is fine. You'll have to decide if you want the button to be "active high" or "active low". That will determine how you wire it up. For example, for "active high" you would pull down the pin to ground say with a 10K resistor and drive the pin high when the button is pressed. Also, look at using an interrupt to sense the button press. The ISR would simply note the pin value change to HIGH ("RISING") and set a state variable indicating the button was pressed. In your loop you would then check for the state change and take appropriate action then clear the state variable.

With respect to your motor control, depending in the behavior you want there, that will determine how and when you check for the button press by looking at the state variable. If you are delaying and want to move on immediately, you'll have to intersperse checks of the button state with the delay.

So here are some things / examples to look for:
1. Attaching interrupts to pin (plus detaching interrupts)
2. Examples of connecting buttons to digital pins
3. Learn about active high and active low
4. Look for examples of debouncing buttons (both HW and SW)

Hope this "coaching" helps move you forward. Other may have better ideas but this is at least one approach you can look at.