Go Down

Topic: Does this look good? (Read 504 times) previous topic - next topic

weiss27md

Jun 08, 2012, 12:53 am Last Edit: Jun 08, 2012, 02:42 am by weiss27md Reason: 1
#include <Servo.h> //servo library

Servo servoarm9;  // create servo object to control a servo
Servo servoswitch10; // create servo object to control a servo

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 = 7;
const int brakeB = 4;
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); //attaches the servo on pin 10 to the servo object
  //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 B pin
  pinMode(brakeB, OUTPUT); //Initiates Brake Channel B pin
  Serial.begin(9600);
}

void loop()
{
  switchState = digitalRead(switchPin8); // read the switch input pin
  if (switchState != lastSwitchState) // compare the switchState to its previous state
  {
    if (switchState == HIGH) // if the state has changed, increment the counter
    {
      switchCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(switchCounter);
    }

    else
    {
      Serial.println("off");
    }
  }
  lastSwitchState = switchState; // save the current state as the last state, for next time through the loop
  // 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 forward 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);

    //Motor A backwards @ full speed
    digitalWrite(dirA, LOW); //Establishes backward direction of Channel A
    digitalWrite(brakeA, LOW); //Disengage the Brake for Channel A
    analogWrite(pwmA, 255); //full speed
    //Motor B backwards @ half speed
    digitalWrite(dirB, LOW); //Establishes backward direction of Channel B
    digitalWrite(brakeB, LOW); //Disengage the Brake for Channel B
    analogWrite(pwmB, 127); //half speed
    delay(3000);

    //Motor A forward @ half speed
    digitalWrite(dirA, HIGH); //Establishes forward direction of Channel A
    digitalWrite(brakeA, LOW); //Disengage the Brake for Channel A
    analogWrite(pwmA, 127); //half speed
    //Motor B forward @ full speed
    digitalWrite(dirB, HIGH); //Establishes forward direction of Channel B
    digitalWrite(brakeB, LOW); //Disengage the Brake for Channel B
    analogWrite(pwmB, 255); //full speed
    delay(3000);

    //Motor A backwards @ full speed
    digitalWrite(dirA, LOW); //Establishes backward direction of Channel A
    digitalWrite(brakeA, LOW); //Disengage the Brake for Channel A
    analogWrite(pwmA, 255); //full speed
    //Motor B backwards @ half speed
    digitalWrite(dirB, LOW); //Establishes backward direction of Channel B
    digitalWrite(brakeB, LOW); //Disengage the Brake for Channel B
    analogWrite(pwmB, 127); //half speed
    delay(3000);

    //Motor A forward @ half speed
    digitalWrite(dirA, HIGH); //Establishes forward direction of Channel A
    digitalWrite(brakeA, LOW); //Disengage the Brake for Channel A
    analogWrite(pwmA, 127); //half speed
    //Motor B forward @ full speed
    digitalWrite(dirB, HIGH); //Establishes forward direction of Channel B
    digitalWrite(brakeB, LOW); //Disengage the Brake for Channel B
    analogWrite(pwmB, 255); //full speed
    delay(3000); 

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

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

majenko

Yes, it looks good.  It's got plenty of comments, and the formatting is nice.

Does it work?  I have no idea whatsoever :P
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

weiss27md

I'm going to test it soon.  Thanks for looking over it.  I'm just wondering about if there is a conflict with pin 9?

majenko

Oh, you want to know if the code is OK?  I didn't read the code - I just gave my aesthetic judgement on how it looks ;)  I am too tired right now to actually read the code and see what it does.  What does it do, by the way?
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

weiss27md

It's the most useless machine that also drives around.

majenko

Now I have read the code I see what you mean about a conflict on pin 9.

Yes, you have both a servo and a brake on pin 9.

Now, I don't know servos, but I don't think they can do multiplexing with other functionality.

You will have to rearrange your pins somewhat to use different ones.
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

weiss27md

But aren't those the only pins available if I'm using 2 motors and 2 servos?  Which one can be moved?

majenko

What makes you think that?  I take it your dir* and brake* signals are controlling a h-bridge?  Those are just digital signals.  You can use any IO line for those - including the analog pins.
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

weiss27md

#8
Jun 08, 2012, 02:35 am Last Edit: Jun 08, 2012, 02:37 am by weiss27md Reason: 1
I changed brakeA to pin 7.  The program is not working correctly.  Can someone help? With the switch flipped one way the motors start to run. Flipped the other way the servos just oscilate.

weiss27md

I updated the code with what I have right now.

RPCoyle

The code compiles Ok and the servos are activated/deactivated so you are probably close to doing what you want , but not in the correct sequence. If you kludged the code together from someone else's, then the best bet is to maybe re-do it and start simple and get one servo working then the other. then implement each step of logic to get what you want. You may be better off doing this unless there is only one logic bomb somewhere and you can find it right away.

Some of the C++ gurus here could probably debug it for you but it might be better for you to understand everything about the underlying logic by doing it yourself.

weiss27md

When powered on it should do nothing.  when the switch is turned on the servo arm comes up and turns it off.  After 10 times the motor program should run.  Once the motor program runs the arm servo should activate.

Go Up