Skip to content

Instantly share code, notes, and snippets.

@moomdate
Created June 28, 2019 08:46
Show Gist options
  • Select an option

  • Save moomdate/eded13542ef148d69a4c20679f13eb36 to your computer and use it in GitHub Desktop.

Select an option

Save moomdate/eded13542ef148d69a4c20679f13eb36 to your computer and use it in GitHub Desktop.
// Example By ArduinoAll
// more info https://www.arduinoall.com/b/141
#include <SoftwareSerial.h>
#include <Servo.h>
byte countByte = 0;
byte byteTemp[3];
byte tt;
int serAng = 90;
int serAngy = 90;
int ppx = 0;
Servo myservo;
Servo myservo2;
SoftwareSerial mySerial(10, 11); // TX, RX
unsigned long previousMillis = 0;
long interval = 1000;
unsigned long previousMillis2 = 0;
long interval2 = 1000;
void setup()
{
Serial.begin(115200);
myservo.attach(9);
myservo2.attach(3);
myservo.write(serAng);
myservo2.write(serAngy);
while (!Serial);
mySerial.begin(115200);
byteTemp[0] = 100;
byteTemp[1] = 100;
byteTemp[2] = 0;
}
void loop()
{
unsigned long currentMillis = millis();
unsigned long currentMillis2 = millis();
if (mySerial.available()) {
tt = mySerial.read();
if (tt != 255) {
byteTemp[countByte] = tt;
countByte++;
if (countByte > 3)
countByte = 0;
} else if (tt == 255) {
countByte = 0;
}
}
//////////////////// x
if (byteTemp[0] > 100) { // plus x
if (serAng > 40) {
interval = abs(200 - byteTemp[0]) ;
//Serial.println(interval);
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
//Serial.println("AB");
serAng--;
}
//myservo.write(serAng);
// Serial.println(byteTemp[0]);
}
}
else if (byteTemp[0] < 100 && byteTemp[0] >= 0) {
if (serAng < 145) {
interval = (abs(byteTemp[0]) + 0) ;
//Serial.println(interval);
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
//Serial.println("AB");
serAng++;
}
//myservo.write(serAng);
// Serial.println(byteTemp[0]);
}
}
/////////////////////// y
if (byteTemp[1] > 100) { // plus x
if (serAngy < 140) {
interval2 = abs(200 - byteTemp[1]) ;
//Serial.println(interval2);
if (currentMillis2 - previousMillis2 >= interval2) {
previousMillis2 = currentMillis2;
//Serial.println("AB");
serAngy++;
}
//myservo.write(serAng);
// Serial.println(byteTemp[0]);
}
}
else if (byteTemp[1] < 100 && byteTemp[1] >= 0) {
if (serAngy > 50) {
interval2 = (abs(byteTemp[1]) + 0) ;
//Serial.println(interval2);
if (currentMillis2 - previousMillis2 >= interval2) {
previousMillis2 = currentMillis2;
//Serial.println("AB");
serAngy--;
}
//myservo.write(serAng);
// Serial.println(byteTemp[0]);
}
}
myservo.write(serAng); //x
myservo2.write(serAngy);//y
//Serial.println(serAngy);
/*
Serial.print("ang : ");
Serial.print(serAng);
Serial.print(" x: ");
Serial.print(byteTemp[0]);
Serial.print(" y: ");
Serial.print(byteTemp[1]);
Serial.print(" speed: ");
Serial.println(byteTemp[2]);*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment