Controlling the loop with an If statement - problems

Hey Forum,

I'm trying to control two servos in my loop. One of the servos may be better suited as a stepper motor, though the trouble i'm having is to do with the 'if' statement not appearing to work as I expected.

Basically, I don't want the servo to go through it's movements until a switch it flicked bringing a pin (pin10) to either HIGH or LOW (doesn't matter to me - just a change in state). The speed is controlled by a pot - I didnt think this had anything to do with the problem so I've omitted it from the drawing.

The problem I'm having is that the servo just goes through its motions regardless if the pin is HIGH or LOW value. I added the pull up resistor to ensure that i was getting a known voltage, though it hasn't changed the process.

Any tips or advice?

Attached are the drawings and code

Thank you

#include <Servo.h>

Servo feedRoller; //Feed Roller servo, continuous rotation
Servo ticker; //Servo to control the mecahnical arm

int feedRollerPin = 10;
int feedRollerPos = 0;
int speedControl = A0;
int servoSpeed = 0;

int tickerPin = 3;
int tickerPos = 0;

int startTicker = A4;



void setup() {
  
  

feedRoller.attach(feedRollerPin);
ticker.attach(tickerPin);

//Step1 take value from potentiometer and map to speed control values
servoSpeed = map(analogRead(speedControl), 0, 1023, 0, 6);

pinMode(speedControl, INPUT);
pinMode(feedRollerPin, OUTPUT);
pinMode(tickerPin, OUTPUT);
pinMode(startTicker, INPUT);
}

void loop() 
{
 
if (startTicker == LOW) {
  
  for (feedRollerPos = 0; feedRollerPos <= 180; feedRollerPos += servoSpeed) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    feedRoller.write(feedRollerPos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
   for (feedRollerPos = 180; feedRollerPos >= 0; feedRollerPos -= servoSpeed) { // goes from 180 degrees to 0 degrees
   feedRoller.write(feedRollerPos);              // tell servo to go to position in variable 'pos'
   }


}



}

pinMode(startTicker, INPUT);

digitalWrite(startTicker, LOW); // turn internal pull up resistor off (which wasn't on)

if (startTicker == HIGH) { // is always true if you use an external pull up resistor

Leo..

Yeh, just realised I posted the wrong code. Those were part of my attempts at trying to activate the IF function as I wanted.

Looks more like this.

pinMode(startTicker, INPUT);
if (startTicker == LOW)

Anything else that might be wrong?

So what is connected to pin10. I assume the servo, not a switch.
And what is connected to A4. I assume a switch.

It's always best to add a hand-drawn diagram, to stop confusion (see the "how to post" sticky)

An easy way to add a switch is to connect the switch between pin and ground.
With internal pull up enabled in setup() with pinMode. NO external resistor.

pinMode (switchPin, INPUT_PULLUP);

The pin is now 'normally high' from the internal pull up, and becomes LOW when the switch/button is pressed.
Leo..

Wawa,

Thanks for the advice, I wasn't aware of that function. Theres an image uploaded on the original post showing the basic layout.

You're correct about the servo attached to pin10. I'll try what you've suggested and see how it goes.

Cheers

The second for loop also needs a delay(15);
And you might want to add a longer delay() between each for() loop (before reversing the servo).
Leo..

asclives:

pinMode(startTicker, INPUT);

if (startTicker == LOW)

Still wrong. startTicker has the value A4. It won't change, so it will never be equal to LOW.

I suspect you meant digitalRead(startTicker)==LOW

OP's diagram:

MorganS:
Still wrong. startTicker has the value A4. It won't change, so it will never be equal to LOW.

I suspect you meant digitalRead(startTicker)==LOW

Oh, thank you. That seems to have done it.

Really appreciate the help!