...
Code Block |
---|
language | c# |
---|
title | Robot Gripper Code |
---|
linenumbers | true |
---|
|
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int val = 0; // variable to store the servo position
int openPos=180;
int closePos=0;
char reader=0;
const int hallPin = 2;
const int servoPin = 10;
const int ledPin = 13;
const int closedVal=120; // define servo open position in degrees
const int openVal=20; //define servo closed position in degrees
int hallState=0;
int lastState=1;
bool servoPosition=LOW;
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
// initialize the LED pin as an output:
pinMode(hallPin, INPUT);
pinMode(servoPin, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
hallState = digitalRead(hallPin);
if (hallState == LOW && lastState == HIGH){
if (servoPosition == HIGH){
servoPosition = LOW;
digitalWrite(servoPin, HIGH);
digitalWrite(ledPin, HIGH);
myservo.write(closedVal);
delay(1000);
digitalWrite(servoPin, LOW);
digitalWrite(ledPin, LOW);
}
else{
servoPosition = HIGH;
digitalWrite(servoPin, HIGH);
digitalWrite(ledPin, HIGH);
myservo.write(openVal);
delay(1000);
digitalWrite(servoPin, LOW);
digitalWrite(ledPin, LOW);
}
}
else{}
lastState = hallState;
}
|
Next Page (Manufacturing Considerations) ->