Help with code behaving strangely

Hi

I'm working on a project where I need to controll a stepper motor with a potentiometer. At the same time I need a servo to open a hatch when i press on a button. The problem i now have is that my code behaves strangely. Sometimes the stepper motor turns in a random direction and doesn't respond to input from a potentiometer. The servo also makes weird movements and only sometimes responds to inputs from the button. Does anyone know the reason of this strange behaviour? When I dont include the part of the stepper motor the servo works perfectly.

This is my code.

#include <Servo.h> 
#include <Stepper.h>
#define STEPS 32

const int servoPin = 13; 
const int buttonPin = 1;
int buttonState = 0;

int Pval = 0;
int potVal = 0;
Servo Servo1; 
Stepper stepper(STEPS, 8, 10, 9, 11);

void setup() { 
   Serial.begin(9600);
   stepper.setSpeed(700);

   Servo1.attach(servoPin); 
   pinMode(servoPin, OUTPUT);
   pinMode(buttonPin, INPUT);
}
void loop(){ 
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    Servo1.write(45);
  } else {
    Servo1.write(150);
  }
  potVal = map(analogRead(A0),0,1024,0,500);
   if (potVal>Pval)
  stepper.step(5);
   if (potVal<Pval)
  stepper.step(-5);

Pval = potVal;

Serial.println(Pval);
}

I have also attached my wiring diagram.

const int buttonPin = 1;

Pin 1 is the hardware serial TX pin. That is a poor choice for the switch input.

There is no pull down resistor on the switch (button) input pin. The input is floating when the switch is open (not pressed) so its state is indeterminate. Wire the switch to ground and enable the internal pullup with

pinMode(pin, INPUT_PULLUP);

The input will read HIGH when the switch is not pressed and LOW when the switch is pressed. Adjust the logic in the code accordingly.

Powering the servo and stepper from the Uno 5V is not recommended. The Uno 5V regulator is limited in the amount of current that it can, reliably, supply. Better an external supply for the motors. A 5V cell phone charger or 4 AA battery pack, for instance.

groundFungus:

const int buttonPin = 1;

Pin 1 is the hardware serial TX pin. That is a poor choice for the switch input.

There is no pull down resistor on the switch (button) input pin. The input is floating when the switch is open (not pressed) so its state is indeterminate. Wire the switch to ground and enable the internal pullup with

pinMode(pin, INPUT_PULLUP);

The input will read HIGH when the switch is not pressed and LOW when the switch is pressed. Adjust the logic in the code accordingly.

Powering the servo and stepper from the Uno 5V is not recommended. The Uno 5V regulator is limited in the amount of current that it can, reliably, supply. Better an external supply for the motors. A 5V cell phone charger or 4 AA battery pack, for instance.

Thanks for the help. Could I also use a 9V battery to power the motors?

I don't think so. The servo is probably rated for 4.8 - 6V so 9V will likely damage it.

Besides, 9V transistor (rectangular) batteries do not have much current capability so would not last very long.

This is what you wrote. It means "When I move the pot, move the stepper 5 steps in that direction and stop." Is that what you wanted to do?

void loop()
{
  potVal = map(analogRead(A0), 0, 1024, 0, 500);
  
  if (potVal > Pval)
    stepper.step(5);
    
  if (potVal < Pval)
    stepper.step(-5);


  Pval = potVal;
}

If you want the stepper to track the movement of the pot:

void loop()
{
  static int stepperPosition = 0;
  int potVal = map(analogRead(A0), 0, 1024, 0, 500);
  
  if (potVal != stepperPosition)
    stepper.step(potVal - stepperPosition);
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.