Help - move servos in 1 degree steps

Hi, i got a Problem with my code for a solar powerbank.
Servos are connected to external power supply and they change their position like i want them to do e.g. (S1.write(90))

But if Servo should move plattform ( //+ and //- ) it doesn´t move.

Serial print "move right", "move left" and "dont turn", "pressed", "notpressed" and "stand by" works fine.

I need to storage the servo position somewhere but my way doesnt work.

PS: im a relative beginner, so maybe i incorrectly used the Servo commands ?!

CODE:

#include <Servo.h>


Servo S1;
int servoR = 90;

Servo S2;
int servoK = 0;


int left_sensor;
int right_sensor;
int toleranz = 30;



void setup()
{
  Serial.begin(9600);
  pinMode(5,INPUT_PULLUP); //push button pin
  
}



void loop()
{
 left_sensor = analogRead (A1);
 right_sensor = analogRead (A2);
 
 Serial.print("Left = ");
 Serial.print(left_sensor);
 Serial.print(", Right = "); 
 Serial.println(right_sensor);



  S1.attach(9);
  S1.read();
  S1.write(90);
  delay (500);
  
  S2.attach(10);
  S2.read();
  S2.write(0);
  delay (500);
  
  servoR = S1.read();
  servoK = S2.read();
  
  left_sensor = analogRead(A1); //pin A1
  right_sensor = analogRead(A2); //pin A2

  
 int SWITCH = digitalRead(5);
 
if (SWITCH == LOW)
{
Serial.println("pressed");
}
if (SWITCH == HIGH)
{
Serial.println("notpressed");
}

   
  
  while (SWITCH == LOW )   //while the button is pressed
  {

    Serial.println ("pressed");                        
    S2.write(150);
    delay(1000);

     
     if (left_sensor > right_sensor + toleranz)
  {
    if (servoR < 180) 
    servoR = servoR+=1;
    S1.write(servoR);      //+
    delay (15);
    Serial.println ("Turn left");
    delay (200); 
    }
  else if (right_sensor > left_sensor + toleranz)
  {
    if (servoR > 0) 
    servoR = servoR-=1;
    S1.write(servoR);        //-
    delay (15);
    Serial.println ("Turn right");
    delay (200); 
  }
    else Serial.println ("Dont turn");
    Serial.println(" ");
    delay(500);
    
 
    delay(500);
    SWITCH = digitalRead(5); //refresh value of variable
  }

  
  while (SWITCH == HIGH)
  {
    Serial.println ("Stand by");
    Serial.println(" ");
   
    S2.write(0);
    S1.write(90);
    
    delay(500);
   
    
    SWITCH = digitalRead(5);
  }
  
}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use [color = red]code tags[/color] (the </> icon above the compose window) to make it easier to read and copy for examination

If neither of those servos ever move then they are not connected correctly or they don't have enough power. At the very least the first write(90) and write(0) should work.

BTW I'm not sure if you realise that servo.read() just returns the last position that was written to the servo. That may or may not be the actual current servo position. Also it is pointless attaching the servos every time round loop. The attach() should be done once in setup().

Steve

1 Like

i´m sorry i was not percise enough.

S1. moves as i want him to move when i push the switch. but he doesnt moves further. (to track the light -> Code: //+ and //- )
S2. just puts the plattform up. this also works

As you have edited your original post so it says something completely different and my comments no longer make any sense I've lost interest. Good luck,

Steve

I didn´t said something complete different!
A not moving Servo was the Problem. I think you didnt read the Post properly.
I explicitly wrote, that the Plattform is not moving with a hint to the right place in the Code...

the problem was the "servoR = servoR-=1;"
I changed it to " --servoR" and added an "analogRead" for both sensors in every condition.

works fine now.

This was my first Post in this Forum and I´m a little disappointed.

The problem is, that you should not change your first post if you found a solution. If you do so, all answers, that refer to your post don't make sense anymore. You should edit your first post only for formal changes. E.g. adding code tags if you forgot to do so.
If you found a solution to your problem and changed your code accordingly show that in a new post.

As always when people communicate with each other, there are some rules that need to be followed.

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