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.