Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

robotics_arduino

Sample Robotics Code
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

robotics_arduino

Sample Robotics Code
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

unsigned long previousMillis = 0;

const long moveDuration = 1000; // duration of servo movement in milliseconds


const long stopDuration = 5000; // duration of servo stop in milliseconds
bool servoMoving = true; // flag to indicate if the servo is currently
moving#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#include <Servo.h>

const int servoPin = 3;


Servo servoMotor;

unsigned long previousMillis = 0;


const long moveDuration = 1000; // duration of servo movement in milliseconds
const long stopDuration = 5000; // duration of servo stop in milliseconds
bool servoMoving = true; // flag to indicate if the servo is currently
moving

Adafruit_PWMServoDriver grptufortu = Adafruit_PWMServoDriver();

#define servo1 0
#define servo2 1
#define servo3 2
#define servo4 3

void screw();
void nail();

void setup() {
Serial.begin(9600);
grptufortu.begin();
grptufortu.setPWMFreq(60);
grptufortu.setPWM(servo1, 0, 330);
grptufortu.setPWM(servo2, 0, 150);
grptufortu.setPWM(servo3, 0, 150);
grptufortu.setPWM(servo4, 0, 550);
delay(3000);

servoMotor.attach(servoPin);
}

void loop() {
if (Serial.available() > 0) {
String command = Serial.readStringUntil('\n'); // Read until newline character)

if (command == "Screw") {
screw();
}

if (command == "Nail") {
nail();
}

unsigned long currentMillis = millis();


// Check if it's time to change the state of the servo
if (servoMoving) {
// Move the servo for moveDuration milliseconds
servoMotor.write(180); // Move to 180-degree position
if (currentMillis - previousMillis >= moveDuration) {
// Save the last time the servo started moving
previousMillis = currentMillis;
// Change the state to stopped
servoMoving = false;
}
} else {
// Stop the servo for stopDuration milliseconds
servoMotor.write(90); // Stop the servo at 90-degree position
if (currentMillis - previousMillis >= stopDuration) {
// Save the last time the servo stopped
previousMillis = currentMillis;
// Change the state to moving
servoMoving = true;
}
}

}
void screw(){
// puntang left muna
for (int S1value = 330; S1value >= 250; S1value--) {
grptufortu.setPWM(servo1, 0, S1value);
delay(10);
}

for (int S2value = 150; S2value <= 250; S2value++) {


grptufortu.setPWM(servo2, 0, S2value);
delay(10);
}

for (int S3value = 150; S3value <= 250; S3value++) {


grptufortu.setPWM(servo3, 0, S3value);
delay(10);

delay(2000);
for (int S4value = 550; S4value < 600; S4value++) {
grptufortu.setPWM(servo4, 0, S4value);
delay(10);
// tapos magcclose
}

for (int S3value = 250; S3value > 150; S3value--) {


grptufortu.setPWM(servo3, 0, S3value);
delay(10);
}

for (int S2value = 250; S2value > 150; S2value--) {


grptufortu.setPWM(servo2, 0, S2value);
delay(10);
}

for (int S1value = 250; S1value < 330; S1value++) {


grptufortu.setPWM(servo1, 0, S1value);
delay(10);
}

for (int S4value = 600; S4value > 550; S4value--) {


grptufortu.setPWM(servo4, 0, S4value);
delay(10);

}
delay (2000);
}

void nail(){
// kakaliwa muna
for (int S1value = 330; S1value >= 250; S1value--) {
grptufortu.setPWM(servo1, 0, S1value);
delay(10);
}

for (int S2value = 150; S2value <= 250; S2value++) {


grptufortu.setPWM(servo2, 0, S2value);
delay(10);
}

for (int S3value = 150; S3value <= 250; S3value++) {


grptufortu.setPWM(servo3, 0, S3value);
delay(10);

delay(2000);
for (int S4value = 550; S4value < 600; S4value++) {
grptufortu.setPWM(servo4, 0, S4value);
delay(10);
// tapos magcclose
}
//pupunta ng kanan
for (int S3value = 250; S3value > 150; S3value--) {
grptufortu.setPWM(servo3, 0, S3value);
delay(10);
}

for (int S2value = 250; S2value > 150; S2value--) {


grptufortu.setPWM(servo2, 0, S2value);
delay(10);
}

for (int S1value = 250; S1value < 575; S1value++) {


grptufortu.setPWM(servo1, 0, S1value);
delay(10);
}

for (int S4value = 600; S4value > 550; S4value--) {


grptufortu.setPWM(servo4, 0, S4value);
delay(10);

}
delay (2000);
for (int S1value = 575; S1value > 330; S1value--) {
grptufortu.setPWM(servo1, 0, S1value);
delay(10);

You might also like