/*
################################################################################
# By DrCrowller #
# #
# Feel free to copy, modify and do whatever you want with this project #
# Be aware that the use of many variables may consume more memory than wanted #
# #
# Created : 7 Apr. 2022 #
################################################################################
*/
#include <Servo.h>
#define LED_PIN 13 // Pin to control LED
Servo arm; // Create a "Servo" object called "arm"
float pos = 0.0; // Variable where the arm's position will be stored (in degrees)
int timesRepeat = 0;
int timesRepeatStop = 3; // Variable used for repeating the step
void setup()
{
pinMode(A0, INPUT_PULLUP); // Set the A0 pin to a switch in pullup mode
pinMode(LED_PIN, OUTPUT);
pinMode(5, INPUT_PULLUP); // Set the pin 5 to a pushbutton in pullup mode
arm.attach(11.2); // Attache the arm to the pin 11.2
arm.write(pos); // Initialize the arm's position to 0 (leftmost)
}
void loop()
{
if(!digitalRead(5)) // Slide-switch
{
digitalWrite(LED_BUILTIN, HIGH);
if (!digitalRead(A0)) // Check for the button input
{
if (pos < 180) // Check that the position won't go lower than 180°
{
for (timesRepeat = 0; timesRepeat < timesRepeatStop; timesRepeat += 1)
{
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
arm.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 5ms for the servo to reach the position
}
delay(500);
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
arm.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 5ms for the servo to reach the position
}
delay(500);
}
}
}
digitalWrite(LED_BUILTIN, LOW);
}
}