Home \ Help \ Search \ Login RSS

Welcome, Guest. Please Login

This forum is now READ-ONLY! Please visit http://forum.wiring.co/ for the new forum.

Wiring ForumProgramming Questions & HelpPrograms › Servo Controls

Page Index Toggle Pages: 1
Servo Controls (Read 3501 times)
04/17/09 at 15:36:51

rer   Offline
YaBB Newbies

Posts: 3
*
 
I have created a program, seen below, to control a device which can load one of 5 ping pong balls, rotate to one of three basketball net targets and launch the ball off the glass or put it straight through the net.

It works great for the first net where it does not have to rotate and actuate use of the grounds2 and grounds2_bk array (both of which control a stepper motor).

The device employs a servo motor to brake a swinging arm (that launches the ball). The servo works perfectly and repeatedly, but if the platter rotates (triggers the spin function), then the servo seems to lose its mind, goes to 0 but also become limp and ineffective.

It is a most frustrating problem that I have spent hours trying to resolve. One of the steppers works fine and has no affect on the servo behavior. The other (which uses the same function) makes the servo go all weird.

All help is appreciated.

// Basketball Shooter
// Wiring Program
#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int orange = 2;    // G2
int yellow = 3;    // G1
int brown = 4;     // G3
int black = 5;     // G4
int org2 = 6;      // g2
int yel2 = 7;      // g1
int brn2 = 8;      // g3
int blk2 = 9;      // g4
int backpin = 10;
int frontpin = 11;
int magnet = 12;     // control of trans for magnet's ground leg
int target_leds []={13,14,15};
int enter = 16;
int switch1 = 24; // inputs
int switch2 = 25;
int servopin = 26;
int net=0; // 0=net, 1=glass
int basket=0; // default is first basket
int check_for_ball = 0;
int nuthinbutnet = 18;
int glass = 19;
// G1 & G3, G1 & G4, G2 & G4, G2 & G3

int grounds []={yellow,brown,orange,black,yellow,black,orange,brown,orange,black,yellow,brown,
orange,brown,yellow,black};
int grounds_bk []={orange,black,yellow,brown,yellow,black,orange,brown,yellow,brown,orange,black,
orange,brown,yellow,black};
int grounds2 []={yel2,brn2,org2,blk2,yel2,blk2,org2,brn2,org2,blk2,yel2,brn2,org2,brn2,yel2,blk
2};
int grounds2_bk []={org2,blk2,yel2,brn2,yel2,blk2,org2,brn2,yel2,brn2,org2,blk2,org2,brn2,yel2,blk
2};

void dispense () {
  digitalWrite(backpin,HIGH); // back up
  digitalWrite(frontpin,LOW); //front down..ball out
  delay(1200);
  digitalWrite(frontpin,HIGH); // front up
  delay(200);
  digitalWrite(backpin,LOW); // back down..load front
  delay(1200); // let balls roll forward
}

void serveme (int rotate, int duration) {
  myservo.write(rotate);              // The 322 servo needs 5-10 ms to make the 1 degree of rotation
  delay(duration);
}

void spin (int pins [], int times, int duration, int lasthold) {
  for (int n=1; n <=times; n++) {
    for (int i=0;i<15;i+=4) {
     digitalWrite(pins[i], HIGH); 
     digitalWrite(pins[i+1], HIGH);
     digitalWrite(pins[i+2], LOW); 
     digitalWrite(pins[i+3], LOW);
     if ((n==times) && (i >= 12)) 
       delay (lasthold);
     else
       delay (duration);
    }
  }
}

void setup() {
  myservo.attach(servopin);
  for (int posn=2;posn<17;posn++)
     pinMode(posn, OUTPUT);
  pinMode(servopin, OUTPUT);
  pinMode(switch1,INPUT);
  pinMode(switch2,INPUT); // setup inputs
}

void loop() {
  serveme (10,100);
  digitalWrite(frontpin,HIGH);
  digitalWrite(backpin,LOW);
  net=0;
  basket=0;

  for (int i=0;i<3;i++) {
    digitalWrite (target_leds[i], LOW); // Load balls - time up - all green LEDs off
  }
  delay (50);
  digitalWrite(target_leds[basket],HIGH); // turn off enter key to start
  // select the basket
  while (digitalRead(switch2)==LOW) {
    if (digitalRead(switch1)==HIGH) {
     delay (200); // wait for user to release button
     digitalWrite(target_leds[basket],LOW);
     basket++;
     if (basket>2) // warp to 0 if over 3
       basket=0;
     digitalWrite(target_leds[basket],HIGH);
    }
  }
  digitalWrite(enter,HIGH); // turn enter ON
  delay (700);
  digitalWrite(enter,LOW); // turn enter OFF
  digitalWrite(target_leds[basket],LOW); // turn basket choice LED off
  // select net or glass
  digitalWrite(target_leds[net],HIGH);
  while (digitalRead(switch2)==LOW) {
    if (digitalRead(switch1)==HIGH) {
     delay (200); // wait for user to release button
     digitalWrite(target_leds[net],LOW);
     net++;
     if (net>1)
       net=0;
     digitalWrite(target_leds[net],HIGH);
    }
  }
  digitalWrite(enter,HIGH); // turn enter ON
  delay (700);
  digitalWrite(enter,LOW); // turn enter OFF
  digitalWrite(target_leds[basket],LOW); // turn basket choice LED off
  //rotate base of machine to correct location
  if (basket>0) {
    if (basket==2) {
     spin (grounds2,14,70,1000); // basket 3
    } else {
     spin (grounds2,7,70,1000); // basket 2
    }
  }
  serveme (10,100); // draw brake away
  dispense();
 
  check_for_ball = analogRead(0);
  while (check_for_ball>200) { // ball is missing if this is true
    digitalWrite(enter,HIGH);
    delay (100);
    digitalWrite(enter,LOW);
    delay (500);
    dispense();
    check_for_ball = analogRead(0);
  }
  //engage magnet
  digitalWrite(magnet,HIGH);

  if (net==0)
    spin (grounds,nuthinbutnet,70,500); // raise arm
  else
    spin (grounds,glass,70,500); // raise arm
  digitalWrite(magnet,LOW); // release arm
 
  delay (1000); //wait for arm to slow
  for (int i=0;i<3;i++) { //swingarm brake
    serveme (0,100); // rub against rotor arm
    delay (500);
    serveme (5,100); // release rotor arm
  }
  serveme (10,200);
  if (net==0)
    spin (grounds_bk,nuthinbutnet,70,1000); // lower arm
  else
    spin (grounds_bk,glass,70,1000); //etc..
 
IP Logged
 
Page Index Toggle Pages: 1