Hi! This is the code I'm using:
#include <Potentiometer.h>
#include <Servo.h>
Servo servo1;
int analogPin = 5;
int pos = 0;
int togglePin = 6;
int val = 0;
Potentiometer potentiometer = Potentiometer(0); //a Potentiometer at analog in 0
#define stepPin 4
#define dirPin 3
void setup()
{
pinMode(analogPin, OUTPUT); // sets pin as output (full rotation servo)
servo1.attach(6);
pinMode(stepPin, OUTPUT); //stepper pin
pinMode(dirPin, OUTPUT); //direction pin
digitalWrite(dirPin, HIGH); //sets the direction one way
digitalWrite(stepPin, LOW); //starts the steppin'
pinMode(togglePin, INPUT);
}
void loop()
{
if (togglePin < 0)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(10 + potentiometer.getValue());
digitalWrite(stepPin, LOW);
delayMicroseconds(10 + potentiometer.getValue());
}
else
{
for ( int i=1; i<30; i++){ // Rotation clockwise in small steps
analogWrite(analogPin, 125); //rotate
delay(300); // rotation duration
analogWrite(analogPin, 0); // stop rotation
delay(1500); //vibration stop
for(pos = 0; pos < 100; pos += 1)
{
servo1.write(pos);
delay(5);
}
delay(3000); //camera shooting time
servo1.write(0);
delay(500); // delay for camera
}
delay(2000);
analogWrite(analogPin, 200); // rotation back to start
delay(1200);
analogWrite(analogPin, 0); // Moving Counter Clockwise
delay(3000);
}
}
And this is the setup I'm using:
I'm trying to use the toggle switch attached to pin 6 to switch between two functions.
The first one is controlling a stepper motor, changing the speed with a potentiometer. When the switch is toggled, it should stop the stepper moving and start moving the servo motors. ( The servo attached to pin 3 is a continuous rotation servo, and the one attached to pin 2 is a micro servo.)
Currently, the led lights up however nothing else happens. Previously, I could control the servos if I only programmed the arduino to do that. I could also control the stepper if I just programmed the arduino to do this. So I can run each function individually. I don't run into any errors verifying and uploading the code.
I'm new to this, and any help/advice would be appreciated ![]()