I am working on a program to control a servo that controls a speed control to a motor. I have been told on the Arduino forum that my code is no good “You do not want to have any calls to loop() in your code,”
my program works as written and I just dont understand what this meens or how to fix it. I am not denying that I need cleaner code but dont know enough to figure out another way to write it.
What I want to do is control a servo with a pot that controls a manual speed controler for a motor and be able to push one of 2 buttons to hold the servo at a given pot position and then be able to move the servo via a push of the buttons by 2* either way. Two other buttons are set to release the servo back to the pot control. this all works the way I have written the code.
the last thing I need is to use a reed switch to measure RPM of the motor and to feed that info back to the arduino to automatically move the servo up or down to maintain the rpm of the motor that the servo is running.
Without “else serloop1(); //returns to SerLoop1 to run again” at the bottom, it will not work.
Here is my code so far.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0;
int val;
// set pin numbers:
const int buttonPin = 10; // the number of the pushbutton pin
const int buttonPin2 = 8;
const int buttonPin3 = 4;
const int buttonPin4 = 2;
void setup() {
{
myservo.attach(12); // attaches the servo on pin 12 to the servo object
}
{
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
}
}
void loop() {
val = analogRead(potpin); //takes reading from pot current position
val = map(val, 0, 1083, 0, 180); //maps pot inputs to servo outputmyservo.write(val); //writes current position to servo to move it
if (digitalRead(buttonPin) == HIGH)
serloop1(); //sends command to SerLoop1
if (digitalRead(buttonPin2) == HIGH)
serloop1(); //sends command to SerLoop1
}
void serloop1(){
int val1 = myservo.read(); //reads current servo location
if (digitalRead(buttonPin) == HIGH)
myservo.write(val1 + 2); //SUBTRACT 2 degrees to servo position for increased motor rpm
delay(100); //allows time for switch ro reset
if (digitalRead(buttonPin2) == HIGH)
myservo.write(val1 - 2); //ADDS 2 degrees to servo position for decreased motor rpm
delay(100); //allows time for switch ro reset
if (digitalRead(buttonPin3) == HIGH)
loop(); //returns to normal pot control of motor
if (digitalRead(buttonPin4) == HIGH)
loop(); //returns to normal pot control of motor
else serloop1(); //returns to SerLoop1 to run again