#!/bin/bash

#SRVOC - SERVO CONTINUIOS - VARIABLE POSITION AND SPEED [DEGREES] + [DELAY]
#sudo apt install arduino-cli
#source srvo
clear
echo "SET SERVO START POSITION [DEGREES]:"
read -r spos
echo "SET SERVO END POSITION [DEGREES]:"
read -r epos
echo "SET SERVO SPEED [DELAY]:"
read -r dl
mkdir -p ./servo/
echo "
#include <Servo.h>

Servo servo0; // Create a servo object

void setup() {
    servo0.attach(9); // Attach the servo to pin 9
}

void loop() {
    for (int pos = $spos; pos <= $epos; pos += 1) { // Move from 0 to 180 degrees
        servo0.write(pos); // Tell servo to go to position in variable 'pos'
        delay($dl); // Wait for the servo to reach the position
    }
    for (int pos = $epos; pos >= $spos; pos -= 1) { // Move back from 180 to 0 degrees
        servo0.write(pos); // Tell servo to go to position in variable 'pos'
        delay($dl); // Wait for the servo to reach the position
    }
}
" | tee ./servo/servo.ino #>> ./servo/servo.ino
#arduino-cli lib install Servo
arduino-cli compile --fqbn $fqbn --port /dev/tty$port ./servo/servo.ino
arduino-cli upload --fqbn $fqbn --port /dev/tty$port ./servo/servo.ino
clear
./srvoc
