Need help with my coding

Im having difficulties with my coding, the coding is suppose to naturally suppose to move the stepper motor when you use to joystick and when you press the button the stepper motor should move. in my coding Ive wrote that when the switch = 0 then the servo motor should activate but when I do so nothing happens what am I missing in my coding

Midterm1.ino (1.96 KB)

Code should be posted using code blocks. Like this:

#include <Servo.h>

int s1 = 8;
int s2 = 9;
int s3 = 10;
int s4 = 11;
int s5 = 2;
int SW_pin = 13;
int x;
int y;
int a = 13;
Servo Servo1;

void setup() {
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
  pinMode(s4, OUTPUT);
  pinMode(s5, INPUT);
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);

  pinMode(13, INPUT);
  digitalWrite(13, HIGH);
  Serial.begin(9600);

  Servo1.attach(2);
}
void loop() {
  x = analogRead(A0);
  y = analogRead(A1);

  if (x < 500)
  {
    digitalWrite(s1, HIGH);
    digitalWrite(s2, LOW);
    digitalWrite(s3, LOW);
    digitalWrite(s4, LOW);
    delay(5);
    digitalWrite(s1, LOW);
    digitalWrite(s2, HIGH);
    digitalWrite(s3, LOW);
    digitalWrite(s4, LOW);
    delay(5);
    digitalWrite(s1, LOW);
    digitalWrite(s2, LOW);
    digitalWrite(s3, HIGH);
    digitalWrite(s4, LOW);
    delay(5);
    digitalWrite(s1, LOW);
    digitalWrite(s2, LOW);
    digitalWrite(s3, LOW);
    digitalWrite(s4, HIGH);
    delay(5);
  }
  else if (x > 550)
  {
    digitalWrite(s1, LOW);
    digitalWrite(s2, LOW);
    digitalWrite(s3, LOW);
    digitalWrite(s4, HIGH);
    delay(5);
    digitalWrite(s1, LOW);
    digitalWrite(s2, LOW);
    digitalWrite(s3, HIGH);
    digitalWrite(s4, LOW);
    delay(5);
    digitalWrite(s1, LOW);
    digitalWrite(s2, HIGH);
    digitalWrite(s3, LOW);
    digitalWrite(s4, LOW);
    delay(5);
    digitalWrite(s1, HIGH);
    digitalWrite(s2, LOW);
    digitalWrite(s3, LOW);
    digitalWrite(s4, LOW);
    delay(5);
  }
  else if (x > 500 && x < 550)
  {
    digitalWrite(s1, LOW);
    digitalWrite(s2, LOW);
    digitalWrite(s3, LOW);
    digitalWrite(s4, LOW);
  }
  if (a = 1)
  {
    Servo1.write(0);
    delay(100);
  }
  else {
    // Make servo go to 0 degrees
    Servo1.write(0);
    delay(100);
    // Make servo go to 90 degrees
    Servo1.write(90);
    delay(100);
    // Make servo go to 180 degrees
    Servo1.write(180);
    delay(100);

  }
  Serial.print("Switch:  ");
  Serial.print(digitalRead(SW_pin));
  Serial.print("\n");
}
  if (a = 1)
  {
    Servo1.write(0);
    delay(100);
  }

Should be:

  if (a == 1)
  {
    Servo1.write(0);
    delay(100);
  }

However, a is initialized to 13 but is never modified after that

The only time you are reading from the switch is to print the value

Also, you are attaching servo to pin 2 but you have that programmed to an input previously, although the library probably reconfigures it.

And you are only coding for a servo. There is no stepper motor anywhere in your code. And unless you have a very special servo your delays are too short to give it time to move.

Oh and could you give us a hint. Which pin is the "button" connected to?

Steve

slipstick:
And you are only coding for a servo. There is no stepper motor anywhere in your code. And unless you have a very special servo your delays are too short to give it time to move.

True. Steppers and servos...BIG difference.

"And unless you have a very special servo your delays are too short to give it time to move."

That may not be valid. I don't think there is any requirement for adding delays to allow the servo to move. Servos just follow the commands being supplied. It seems to be an artifact from the "knob" example.

100 ms is about five RC frame times.
A decent servo should manage about 60 degrees in that time.

"100 ms is about five RC frame times.
A decent servo should manage about 60 degrees in that time."

And... :sunglasses:

TheMemberFormerlyKnownAsAWOL:
100 ms is about five RC frame times.
A decent servo should manage about 60 degrees in that time.

So trying to move it 90 degrees in that time may cause disappointment.

Steve

zoomkat:
"And unless you have a very special servo your delays are too short to give it time to move."

That may not be valid. I don't think there is any requirement for adding delays to allow the servo to move. Servos just follow the commands being supplied. It seems to be an artifact from the "knob" example.

A typical servo can do about 50-60 degrees in 100ms. So if you command it to go to 0 (from who knows where) then before it can get there ask it to go 90 and 100mS later to 180 it is going to be very confused.

It's simply bad practice to jerk servos around never giving them time to reach target.

Steve

"It's simply bad practice to jerk servos around never giving them time to reach target."

I don't think hobby grade servos will be much worse for the wear (opinion). If there is a serious concern when reversing the servo, then much more complex code and wiring would be needed to stop the servo softly before starting it in the other direction. If the servo is to continue in the same direction, the delay is just burning daylight. Remove the delay in the "knob" example and see if it makes a difference.

Not a good test because Knob only moves the servo when the pot is moved. If you really think no delays are needed to allow the servo time to move try:

void loop()
{
servo.write(0);
servo.write(180);
}

The servo will just twitch. Then put something like delay(500) between the writes and at the end. Any change in behaviour?

Steve

I currently can't test with my Arduino and servos as they have been in box somewhere for several years. The original delay appeared in the servo "sweep" example to make it move about slowly, which is a simple method to do that. It appears that the delay was then incorporated into the "sweep" as a cut and paste which makes the servo slow to move. The "time to get there" really doesn't pass the snicker test in the pot instance. Servos can jitter/hunt for various reasons, with an analog pot input, noise appears it can be a factor. Servos can also jitter due to worn internal pots. If jitter/hunting is an issue, a dead band can be added in the code similar to the below. Suggesting increasing the delay to something like 100ms just seems a little bit "out there". Just an opinion.

//zoomkat multi pot/servo test 3-23-13
//includes dead band for testing and limit servo hunting
//view output using the serial monitor

#include <Servo.h> 
Servo myservo1;  //declare servos
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

int potpin1 = 0;  //analog input pin A0
int potpin2 = 1;
int potpin3 = 2;
int potpin4 = 3;
int potpin5 = 4;

int newval1, oldval1;  //pot input values
int newval2, oldval2;
int newval3, oldval3;
int newval4, oldval4;
int newval5, oldval5;

void setup() 
{
  Serial.begin(9600);  
  myservo1.attach(2);  
  myservo2.attach(3);
  myservo3.attach(4);
  myservo4.attach(5);
  myservo5.attach(6);
  Serial.println("testing multi pot servo");  
}

void loop()
{ 
  newval1 = analogRead(potpin1);           
  newval1 = map(newval1, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){ //dead band 
    myservo1.write(newval1); //position the servo
    Serial.print("1- ");
    Serial.println(newval1); //print the new value for testing 
    oldval1=newval1; //set the current old value
  }

  newval2 = analogRead(potpin2);
  newval2 = map(newval2, 0, 1023, 0, 179);
  if (newval2 < (oldval2-2) || newval2 > (oldval2+2)){  
    myservo2.write(newval2);
    Serial.print("2- ");    
    Serial.println(newval2);
    oldval2=newval2;
  }

  newval3 = analogRead(potpin3);           
  newval3 = map(newval3, 0, 1023, 0, 179); 
  if (newval3 < (oldval3-2) || newval3 > (oldval3+2)){  
    myservo3.write(newval3);
    Serial.print("3- ");
    Serial.println(newval3);
    oldval3=newval3;
  }

  newval4 = analogRead(potpin4);           
  newval4 = map(newval4, 0, 1023, 0, 179); 
  if (newval4 < (oldval4-2) || newval4 > (oldval4+2)){  
    myservo4.write(newval4);
    Serial.print("4- ");
    Serial.println(newval4);
    oldval4=newval4;
  }

  newval5 = analogRead(potpin5);           
  newval5 = map(newval5, 0, 1023, 0, 179); 
  if (newval5 < (oldval5-2) || newval5 > (oldval5+2)){  
    myservo5.write(newval5);
    Serial.print("5- ");
    Serial.println(newval5);
    oldval5=newval5;
  } 
  delay(50);  //to slow loop for testing
}

I did the test. It seemed obvious to me that it is necessary to allow a servo time to reach a commanded position before you give it a different command and then keep on commanding it to different positions with little or no time between commands but I thought I'd check just in case the obvious is wrong. It isn't.

So one of us here is just doing thought experiments i.e. guessing what he thinks "should" happen. And that ain't me.

Steve

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.... :slight_smile:

sorry I wasn't able to reply to answer most of your questions

PIN 13 is where I have the switch from the joystick

PIN 2 is connected to the servo motor

and s1-5 is what is the servo motor. now the servo motor works when I activate move the joystick in the X axis.

I'm going t o try adding the == and increasing the delay time to see if it can respond to the change.

So its works after I increased the delay time to 500 for the servo motor I can move both the stepper motor and the servo motor

the servo motor the way I programmed it moves on it own and I can control the stepper motor by moving the joystick in the x axis but how do I make it so that when I hold the button down only the servo motor will move and nothing else ?

Post your current (almost) working code and we can help from there. Did you ever change that 'if (a=1)'?

Steve

yes the servo was able to move when change it to (a == 1)

#include <Servo.h>

int s1 = 8;
int s2 = 9;
int s3 = 10;
int s4 = 11;
int s5 = 2;
int SW_pin = 13;
int x;
int y;
int a = 13;
Servo Servo1;

void setup() {
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
pinMode(s4, OUTPUT);
pinMode(s5, INPUT);
pinMode(A0, INPUT);
pinMode(A1, INPUT);

pinMode(13, INPUT);
digitalWrite(13, HIGH);
Serial.begin(9600);

Servo1.attach(2);
}
void loop() {
x = analogRead(A0);
y = analogRead(A1);

if (x < 500)
{
digitalWrite(s1, HIGH);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
digitalWrite(s4, LOW);
delay(5);
digitalWrite(s1, LOW);
digitalWrite(s2, HIGH);
digitalWrite(s3, LOW);
digitalWrite(s4, LOW);
delay(5);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, HIGH);
digitalWrite(s4, LOW);
delay(5);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
digitalWrite(s4, HIGH);
delay(5);
}
else if (x > 550)
{
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
digitalWrite(s4, HIGH);
delay(5);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, HIGH);
digitalWrite(s4, LOW);
delay(5);
digitalWrite(s1, LOW);
digitalWrite(s2, HIGH);
digitalWrite(s3, LOW);
digitalWrite(s4, LOW);
delay(5);
digitalWrite(s1, HIGH);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
digitalWrite(s4, LOW);
delay(5);
}
else if (x > 500 && x < 550)
{
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
digitalWrite(s4, LOW);
}
if (a == 0)
{
Servo1.write(0);
delay(500);
}
else {
// Make servo go to 0 degrees
Servo1.write(0);
delay(500);
// Make servo go to 90 degrees
Servo1.write(90);
delay(500);
// Make servo go to 180 degrees
Servo1.write(180);
delay(500);

}
Serial.print("Switch: ");
Serial.print(digitalRead(SW_pin));
Serial.print("\n");
}

This is the most updated code version but idk if im doing it wrong sill when the button is pressed then only the servo motor should move but its not even listening to that command. is it possible that Im confusing it from the very beginning from declaring SW_Pin is pin 13 and int a = pin 13 as well.

ive been researching on conditional statement and it seems that most use that setup