#include <Stepper.h>
#include <Wire.h>
#include <Servo.h>
Servo myservo1; //create servo object to control a servo1
Servo myservo2; //create servo object to control a servo2
const int trigPin = 3; const int echoPin = 2; const int stepsPerRevolution = 200; //fit the number of steps per revolution for motor stepper
int speedstep = 0;
int stepCount = 0; //number of steps the motor has taken
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); // initialize the stepper library on pins 8 through 11:
void setup()
{ Serial.begin(9600);
myservo1.attach(7);
myservo2.attach(6);
}
long microsecondsToCentimeters(long microseconds) {
return microseconds / 29 / 2;
}
void gerak120() {
myservo1.write(120); //sets the servo position according to
the scaled value delay(2000);
}
void gerak180() {
myservo1.write(90); //sets the servo position according to the scaled value
myservo2.write(180); //sets the servo position according to the scaled value
delay(4000);
}
void loop() {
speedstep = 350; //motor speed 0 - 1023 //map it to a range from 0 to 100:
int motorSpeed = map(speedstep, 0, 1023, 0, 100); //set the motor speed:
if (motorSpeed > 0) {
myStepper.setSpeed(motorSpeed);
myStepper.step(stepsPerRevolution / 100);
}
long duration, cm;
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(0);
digitalWrite(trigPin, HIGH);
delayMicroseconds(20);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
//cm = microsecondToCentimeters(duration);
if ( melon mentah ) {
gerak120();
}
else if ( melon matang ) {
gerak180();
}
else {
myservo1.write(0);
myservo2.write(0); //tidak menggerakkan servo
}