Pages: [1]   Go Down
Author Topic: claw controlled by 1 button  (Read 324 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i have have been building a robotic arm with a 1:1 mirroring controller. i need help with how to get my button hooked up so that with one press the servo goes one way (makeing the claw open) then releace the button and make the servo turn the other way. i have tryed many different setups but i cant get this working.
Code:
// Remote Sketch
// by david21n

// Simple sketch for one to one robot arm project

#include <Servo.h>
 
Servo servo1;    // create servo object to control a servo
Servo servo2;    // a maximum of eight servo objects can be created
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
Servo servo7;

// States of the claw
const int OPEN = 0;
const int CLOSED = 1;
const int OPENING = 2;
const int CLOSING = 3;

// claw close and open positions
const int closeClaw1 = 160;
const int openClaw1 = 105;
const int closeClaw2 = 15;
const int openClaw2 = 70;

// intial values of servos
int val1 = 90;        // Joint 1 (Base)
int val2 = 90;        // Joint 2 (Close to base)
int val3 = 90;        // Joint 3 (Middle)
int val4 = 90;        // Joint 4 (End)
int val5 = 90;        // Joint 5 (Rotate the claw)
int valClaw1 = 160;   // claw 1
int valClaw2 = 15;    // claw 2

// Movement of the Servos
// - each servo moves depending how it is mounted
// Joint 1 (Base)           --- right = 0, left = 180
// Joint 2 (Close to base)  --- back or up = 0, forward or down = 180
// Joint 3 (Middle)         --- back or up = 0, forward or down = 180
// Joint 3 (End)            --- back or up = 180, forward or down = 0

// analog pins
const int input1 = 3;    // Base
const int input2 = 2;    // Joint 1 (Close to base)
const int input3 = 1;    // Joint 2 (Middle)
const int input4 = 0;    // Joint 3 (End)

// The potentiometers would be read and adjusted to
// a value between 0 and 180 that the servo function
// can use. But, the potentiometers goes father than
// 180 degrees, so the program need parameters.
// To get these parameters run the "Sense" sketch
// and write down the values of the joint when the
// potentiometer is at 0 and 180 degrees.

// IMPORTANT: make sure the 0 degree position of the
// potentiometer is the same as the 0 degree position
// of the servo. If the 0 degree positon of potentiometer
// is the 180 degree position of the servo, the servo
// would go the reverse direction of the potentiomter

// Joint read values at 0 and 180 degrees
const int deg1[] = {205,935};
const int deg2[] = {95,830};
const int deg3[] = {210,915};
const int deg4[] = {130,830};

// scales used to scale input value to 0 to 180
// scale = (deg180 - deg0) / 180;
const float scale1 = 3.91;
const float scale2 = 3.91;
const float scale3 = 4.08;
const float scale4 = 3.89;

// Joint offset
// The offset is when the servo is not mounted
// correctly by a few degrees, so an offset is applied
// to correct it.
const int offset1 = 0;
const int offset2 = 5;
const int offset3 = -5;
const int offset4 = 5;

// Claw increment
const int incClaw1 = -5;
const int incClaw2 = 5;

// Led pin to show arduino is on
int ledPin = 13;

// Claw variables
int buttonPin = 12;
int buttonState = 0;
int clawState = CLOSED;

void setup()
{
  // attach servos
  servo1.attach(0);  // BASE
  servo2.attach(1);  // Joint 1
  servo3.attach(2);  // Joint 2
  servo4.attach(3);  // Joint 3
  servo5.attach(5);  // Joint 4 rotate
  servo6.attach(6);  // claw 1
  servo7.attach(7);  // claw 2
 
  // led pin
  pinMode(ledPin, OUTPUT);
}
 
 
void loop()
{
  // Turn on led
  digitalWrite(ledPin, HIGH);
 
  // Read the potentiometers
  val1  = analogRead(input1);
  val2  = analogRead(input2);
  val3  = analogRead(input3);
  val4  = analogRead(input4);
 
  // Adjust the read values
  val1 = adj(val1, deg1, scale1, offset1);
  val2 = adj(val2, deg2, scale2, offset2);
  val3 = adj(val3, deg3, scale3, offset3);
  val4 = adj(val4, deg4, scale4, offset4);
 
  // Send out signal to servos
  servo1.write(val1);             
  servo2.write(val2);             
  servo3.write(val3);           
  servo4.write(val4);             
  servo5.write(val5);
  servo6.write(valClaw1);
  servo7.write(valClaw2);
 
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // Check if the pushbutton is pressed.
 
  // if it is, the buttonState is LOW,
  // changes the claw state
  if ((buttonState == LOW))
  {       
    if (clawState == CLOSED)
      clawState = OPENING;
    else if (clawState == OPEN)
      clawState = CLOSING;
  }
 
  // if claw is opening or closing, increases
  // or decreases the servo value
  if (clawState == OPENING)
  {
    if(valClaw1 > openClaw1 && clawState == OPENING)
    {
      valClaw1 += incClaw1;
      valClaw2 += incClaw2;
      servo6.write(valClaw1);
      servo7.write(valClaw2);
    }
    else
      clawState = OPEN;      // claw is open
  }
  else if (clawState == CLOSING)
  {
    if(valClaw1 < closeClaw1 && clawState == CLOSING)
    {
      valClaw1 -= incClaw1;
      valClaw2 -= incClaw2;
      servo6.write(valClaw1);
      servo7.write(valClaw2);
    }
    else
      clawState = CLOSED;    // claw is closed
  }
 
  // delay for the servo to
  // move to the new position
  delay(15);
}

// The function adjusts the analog read value to degrees
int adj(int input, const int deg[], const float scale, const int offset)
{
  float val = 0;
 
  // scales the value to 0 to 180
  if ( input < deg[0])
    val = 0;
  else if ( (input >= deg[0]) && (input <= deg[1]) )
    val = (input - deg[0])/scale;
  else if (input > deg[1])
    val = 180;
 
  // add offset
  val = val + offset;
  // fix max and min
  if (val > 180)
    val = 180;
  else if (val < 0)
    val = 0;
 
  return val;
}
maybe if someone could help me figure this out. or mabey change the code so that one button opens the claw and the other would close it. that would be extremely help full. i am new to writing code and need all the help i can get. please get back to me.  if you need you you could email me at (erik102696@gmail.com). also this is the website that i got the idea from and the code http://letsmakerobots.com/node/22346 
Logged

NE PA
Offline Offline
Full Member
***
Karma: 5
Posts: 156
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So you want the servo to go one way with the press of the button and the other way when released? That seems pretty simple but maybe I'm not understanding how you need it done. It would be something like this:

Code:
If (digitalRead(buttonpin) == HIGH)
{
servo.write(180);
}
else
{
servo.write(0);
}

Also I noticed you are trying to adjust the analog read values from a pot to the output of a servo. Why not use the map function for that?
Logged


Queens, New York
Offline Offline
Faraday Member
**
Karma: 65
Posts: 2925
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You should clarify that you are using analog pins and not digital pins.
Code:
const int input1 = 3;    // Base
const int input2 = 2;    // Joint 1 (Close to base)
const int input3 = 1;    // Joint 2 (Middle)
const int input4 = 0;    // Joint 3 (End)

to.
Code:
const int input1 = A3;    // Base
const int input2 = A2;    // Joint 1 (Close to base)
const int input3 = A1;    // Joint 2 (Middle)
const int input4 = A0;    // Joint 3 (End)
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

0
Offline Offline
Tesla Member
***
Karma: 114
Posts: 8905
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I like making a pin high then pulling it low to ground. Easy to test just using a piece of wire for a switch.

Code:
//zoomkat servo button test 12-29-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }   
 
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
}
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I like making a pin high then pulling it low to ground. Easy to test just using a piece of wire for a switch.

Code:
//zoomkat servo button test 12-29-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }   
 
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
}

is this something that could be added to my pre existing code. and how i am new to this i tryed to set it up as being controlled by many different ways this is my last hope. i need to get this arm working for this weekend the only thing left to get working is the claw.
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 65
Posts: 2925
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well the part you have to open and close your claw, you just take it out and insert this in its place. You will need to add the global variables for the buttons and change servo1.Write(170) "open" and servo1.Write(10) "close", to your claw servos.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Pages: [1]   Go Up
Jump to: