[Arduino] MeArm with ps2.
MeArm Robot Arm - Your Robot - V1.0
https://www.hackster.io/benbobgray/mearm-robot-arm-your-robot-v1-0-326702
Reference:
arduino tower
http://yehnan.blogspot.tw/2013/09/arduinotower-pro-sg90.html
Source code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Servo.h> | |
// This sketch shows the basic operation of the Thumb Joystick (COM-09032) and breakoutboard (BOB-09110). | |
// The joystick outputs two analog voltages (VERT and HORIZ), and one digital signal (SEL) forthe pushbutton. | |
// Connections to joystick (change if you use different pins): | |
Servo middle, left, right, claw ; // creates 4 "servo objects" | |
const int VERT = A1; // analog | |
const int HORIZ = A2; // analog | |
const int VERT2 = A3; // analog | |
const int HORIZ2 = A4; // analog | |
//const int SEL = 11; // digital | |
int idx_middle = 90; | |
int idx_right = 90; | |
int idx_left = 90; | |
int idx_claw = 25; | |
// Also connect the joystick VCC to Arduino 5V, and joystick GND to Arduino GND. | |
// This sketch outputs serial data at 9600 baud (open Serial Monitor to view). | |
void setup() | |
{ | |
// make the SEL line an input | |
//pinMode(SEL,INPUT); | |
// turn on the pull-up resistor for the SEL line (see http://arduino.cc/en/Tutorial/DigitalPins) | |
//digitalWrite(SEL,HIGH); | |
middle.attach(9); // Attach PIN number with Servo | |
middle.write(idx_middle); // Initial Servo of an angle 90 | |
right.attach(10); // Attach PIN number with Servo | |
right.write(idx_right); // Initial Servo of an angle 90 | |
left.attach(11); // Attach PIN number with Servo | |
left.write(idx_left); // Initial Servo of an angle 90 | |
claw.attach(8); // Attach PIN number with Servo | |
claw.write(idx_claw); // Initial Servo of an angle 25 | |
delay(3000); | |
// set up serial port for output | |
Serial.begin(9600); | |
} | |
void loop() | |
{ | |
int vertical, horizontal,vertical2, horizontal2, select; | |
// read all values from the joystick | |
vertical = analogRead(VERT); // will be 0-1023 | |
horizontal = analogRead(HORIZ); // will be 0-1023 | |
vertical2 = analogRead(VERT2); // will be 0-1023 | |
horizontal2 = analogRead(HORIZ2); // will be 0-1023 | |
//select = digitalRead(SEL); // will be HIGH (1) if not pressed, and LOW (0) if pressed | |
// print out the values | |
idx_middle = map(horizontal, 0, 1023, 150, 30); | |
middle.write(idx_middle); | |
idx_left = map(vertical, 0, 1023, 30, 150);//idx_left = map(vertical2, 100, 900, 90, 150); | |
left.write(idx_left); | |
idx_right = map(vertical2, 0, 1023, 50, 150); | |
right.write(idx_right); | |
idx_claw = map(horizontal2, 0,1023, 0, 50);//claw = map(horizontal2, 0, 1023, 0, 180); | |
claw.write(idx_claw); | |
Serial.print("vertical: "); | |
Serial.print(vertical); | |
Serial.print(" horizontal: "); | |
Serial.print(horizontal); | |
Serial.print("vertical2: "); | |
Serial.print(vertical2); | |
Serial.print(" horizontal2: "); | |
Serial.print(horizontal2); | |
Serial.print(" select: "); | |
Serial.print(" "); | |
Serial.print(idx_left); | |
Serial.print(" "); | |
Serial.print(idx_right); | |
Serial.print(" "); | |
Serial.print(idx_middle); | |
Serial.print(" "); | |
Serial.print(idx_claw); | |
Serial.print(" "); | |
if(select == HIGH) | |
Serial.println("not pressed"); | |
else | |
Serial.println("PRESSED!!!!!"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Servo.h> | |
Servo middle; | |
void setup(){ | |
// | |
//Serial.begin(9600); | |
middle.attach(9); | |
middle.write(80); | |
} | |
void loop(){ | |
//middle.write(100); | |
//delay(20); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<Servo.h> | |
Servo myservo; // create Servo instance to control Servomotor | |
void setup() { | |
// put your setup code here, to run once: | |
myservo.attach(9); // attach PIN number with Servo | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
// myservo.write(45); | |
myLoop(0,43);//claw's balance is 25 | |
} | |
void myLoop(int idx_start, int idx_end){ | |
for(int i = idx_start; i <= idx_end; i+=1){ | |
myservo.write(i); // write angle i: range from 0 ~ 180 | |
delay(5); | |
} | |
for(int i = idx_end; i >= idx_start; i-=1){ | |
myservo.write(i);// write angle i: range from 0 ~ 180 | |
delay(5); | |
} | |
} |
0 意見:
張貼留言