Pages: [1]   Go Down
Author Topic: Does this look good?  (Read 473 times)
0 Members and 1 Guest are viewing this topic.
Austin, TX
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

#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);
  }
}
« Last Edit: June 07, 2012, 07:42:56 pm by weiss27md » Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Does it work?  I have no idea whatsoever smiley-razz
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Austin, TX
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-wink  I am too tired right now to actually read the code and see what it does.  What does it do, by the way?
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Austin, TX
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Austin, TX
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Austin, TX
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: June 07, 2012, 07:37:16 pm by weiss27md » Logged

Austin, TX
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I updated the code with what I have right now.
Logged

Santa Fe
Offline Offline
Full Member
***
Karma: 1
Posts: 201
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Austin, TX
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: