2 DC motors with Arduino shield

I need help with code that keeps track of a switch and have a servo that turns it off. After 10 times of being switched off it would start a program with the 2 motors. It would go forward for a short duration then stop. Go in half circles going forward and backwards making like a 4 point rounded off star pattern. After that another servo would activate once.

You need to post your code. We can't help if we can't see it.

Thanks but I'm still working on it. Do I use a "if else" or "do while" statement for controlling after 10 times of being turned on it activates the motor program. Also I can't find how to do a count program to keep track of the number of switches.

Do I use a "if else" or "do while" statement for controlling after 10 times of being turned on it activates the motor program. A

It depends on how your code is structured, which is why it is so important to post it, otherwise, it's just an online guessing game.

This code still needs a counter added in and the code for the motors will change since I just got a motor shield instead of transistors.

#include <Servo.h> 
 
Servo servoarm9;  // create servo object to control a servo 
Servo servoswitch10;

int pos = 0;    // variable to store the servo position 

// this constant won't change:
const int transistorPin0 = 0;    // 
const int transistorPin1 = 1;    //
const int transistorPin2 = 2;    //
const int transistorPin3 = 3;    //
const int transistorPin4 = 4;    //
const int transistorPin5 = 5;    //
const int transistorPin6 = 6;    //
const int transistorPin7 = 7;    //
const int switchpin8 = 8;

// Variables will change:
int switchCounter = 0;   // counter for the number of button presses
int switchState = 0;         // current state of the button
int lastswitchState = 0;     // previous state of the button

 void setup() 
 {
   pinMode(transistorPin0, OUTPUT);// set  the transistor pin as output
   pinMode(transistorPin1, OUTPUT);
   pinMode(transistorPin2, OUTPUT);
   pinMode(transistorPin3, OUTPUT);
   pinMode(transistorPin4, OUTPUT);
   pinMode(transistorPin5, OUTPUT);
   pinMode(transistorPin6, OUTPUT);
   pinMode(transistorPin7, OUTPUT);
   pinMode(switchpin8, INPUT);     // set the switch pin to be an input
   servoarm9.attach(9);   // attaches the servo on pin 9 to the servo object
   servoswitch10.attach(10);
 }
void loop() 
{
   // read the switch input:
   if (digitalRead(2) == HIGH) 
   {
     // if the switch is closed:
     for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 180 degrees 
                                    // in steps of 1 degree 
    servoarm9.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  }  
   else 
   {
     // if the switch is open:
     for(pos = 90; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
                                  
    servoarm9.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
   digitalWrite(transistorPin5, HIGH);
   digitalWrite(transistorPin6, HIGH);
   digitalWrite(transistorPin0, HIGH);
   digitalWrite(transistorPin2, HIGH);
   delay(3000);
   digitalWrite(transistorPin3, HIGH);
   digitalWrite(transistorPin7, HIGH);
   delay(3000);
   digitalWrite(transistorPin5, HIGH);
   digitalWrite(transistorPin6, HIGH);
   digitalWrite(transistorPin0, HIGH);
   digitalWrite(transistorPin2, HIGH);
   delay(3000);
   digitalWrite(transistorPin3, HIGH);
   digitalWrite(transistorPin7, HIGH);
   delay(3000);

Neither of your for loops does what the comments say they do.

I recommend that you get in the habit of using a compound statement (i.e. a block of code enclosed between { and } ) for every conditional piece of code. For example the bodies of your for loops, which are probably intended to move the servo and wait for each iteration of the loop, but in fact will move the servo through all positions and then delay once at the end, because you have not used a compound statement; only the statement immediately after the for() clause is controlled by the for clause.

Stick to using compound statements for every 'if', 'else', 'for', 'do', 'while' (even if you know they are redundant) and you will avoid a lot of potentially nasty snags like the ones here.

I'm not understanding completely, I am pretty new to programming. I got most of this code from examples and then just modified it.

I’m not understanding completely

for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 180 degrees

Here, the comment doesn’t agree with the code; pos doesn’t even go from 0 to 90 degrees.

Leave out the comment if you don’t know what they mean, the compiler ignores them anyway.

I got the servo code straight from an Arduino example. All I did was change 180 to 90.

I got the motors setup with the shield. I’m still not sure about the brackets and if everything else is correct.

#include <Servo.h> 
 
Servo servoarm9;  // create servo object to control a servo 
Servo servoswitch10;

int pos = 0;    // variable to store the servo position 

// this constant won't change:
const int switchpin8 = 8;
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;

// Variables will change:
int switchCounter = 0;   // counter for the number of button presses
int switchState = 0;         // current state of the button
int lastswitchState = 0;     // previous state of the button

 void setup() 
 {
   pinMode(switchpin8, INPUT);     // set the switch pin to be an input
   servoarm9.attach(9);   // attaches the servo on pin 9 to the servo object
   servoswitch10.attach(10);
 }
void loop() 
{
   // read the switch input:
   if (digitalRead(2) == HIGH) 
   {
     // if the switch is closed:
     for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 90 degrees 
                                    // in steps of 1 degree 
    servoarm9.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  }  
   else 
   {
     // if the switch is open:
     for(pos = 90; pos>=1; pos-=1)     // goes from 90 degrees to 0 degrees 
                                  
    servoarm9.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  
}
  void setup() 
{
//Setup Channel A
pinMode(dirA, OUTPUT); //Initiates Motor Channel A pin
pinMode(brakeA, OUTPUT); //Initiates Brake Channel A pin

//Setup Channel B
pinMode(dirB, OUTPUT); //Initiates Motor Channel A pin
pinMode(brakeB, OUTPUT); //Initiates Brake Channel A pin
}

void loop(){

//Motor A forward @ full speed
digitalWrite(dirA, HIGH); //Establishes forward direction of Channel A
digitalWrite(brakeA, LOW); //Disengage the Brake for Channel A
analogWrite(pwmA, 255); //Spins the motor on channel A at full speed

//Motor B forward @ full speed
digitalWrite(dirB, HIGH); //Establishes backward direction of Channel B
digitalWrite(brakeB, LOW); //Disengage the Brake for Channel B
analogWrite(pwmB, 255);

delay(3000);

digitalWrite(brakeA, HIGH); //Engage the Brake for Channel A
digitalWrite(brakeB, HIGH); //Engage the Brake for Channel B

delay(1000);

}

Here’s the revised code, can anyone help with any mistakes.

#include <Servo.h> 
 
Servo servoarm9;  // create servo object to control a servo 
Servo servoswitch10;

int pos = 0;    // variable to store the servo position 

// this constant won't change:
const int switchPin8 = 8;
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;

// Variables will change:
int switchCounter = 0;   // counter for the number of button presses
int switchState = 0;         // current state of the button
int lastSwitchState = 0;     // previous state of the button

 void setup() 
 {
   pinMode(switchPin8, INPUT);     // set the switch pin to be an input
   servoarm9.attach(9);   // attaches the servo on pin 9 to the servo object
   servoswitch10.attach(10);
   //Setup Channel A
   pinMode(dirA, OUTPUT); //Initiates Motor Channel A pin
   pinMode(brakeA, OUTPUT); //Initiates Brake Channel A pin

   //Setup Channel B
   pinMode(dirB, OUTPUT); //Initiates Motor Channel A pin
   pinMode(brakeB, OUTPUT); //Initiates Brake Channel A pin
   Serial.begin(9600);
 }
void loop() 
{
   // read the switch input pin:
  switchState = digitalRead(switchPin8);
  
  // compare the switchState to its previous state
   if (switchState != lastSwitchState) {
    // if the state has changed, increment the counter
    if (switchState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      switchCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(switchCounter);
  }  
   else 
   {
   // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
  } 
   }
   // save the current state as the last state, 
  //for next time through the loop
  lastSwitchState = switchState;

    // turns on the servo every 10 pushes by 
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of 
  // the division of two numbers:
  if (switchCounter % 10 == 0) {
     //Motor A forward @ full speed
digitalWrite(dirA, HIGH); //Establishes forward direction of Channel A
digitalWrite(brakeA, LOW); //Disengage the Brake for Channel A
analogWrite(pwmA, 255); //Spins the motor on channel A at full speed

//Motor B forward @ full speed
digitalWrite(dirB, HIGH); //Establishes backward direction of Channel B
digitalWrite(brakeB, LOW); //Disengage the Brake for Channel B
analogWrite(pwmB, 255);

delay(3000);

digitalWrite(brakeA, HIGH); //Engage the Brake for Channel A
digitalWrite(brakeB, HIGH); //Engage the Brake for Channel B

delay(1000);

  } else {
   // if the switch is closed:
     for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 90 degrees 
                                    // in steps of 1 degree 
    servoarm9.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15); 
    // if the switch is open:
     for(pos = 90; pos>=1; pos-=1)     // goes from 90 degrees to 0 degrees 
                                  
    servoarm9.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15); 
  }

I got the servo code straight from an Arduino example. All I did was change 180 to 90.

Even then, the comment and code don't match. "pos" goes from 0 to 89 (or 179). Close, but as they say, no cigar.