Servo +10

Hi I'm struggling with some code:

#include <Servo.h> 
int vals = 0;
int down = 0;
int up = 0;
Servo servox;
Servo servoy;
void setup() {
  servox.attach(2);
  servoy.attach(3);
  
      Serial.begin(9600);
        Serial.println("Servo Serial Control");      
}

void loop() 
{
      if (Serial.available()) 
{
   vals = Serial.read();
   

 if(vals == 115&& down <=140) 
 { 
 down = down + 10;   
  servox.write(down);    
 }
    
  
   if(vals == 119&& up <=140) 
 { 
 up = up + 10;   
  servox.write(up);    
 }
 }
}

I just want to move the servo up or down when w (up) or s (down)
is pressed,
so, servo start at 90 degrees
when w is pressed +10 and when s is pressed -10
and repeat that.

Not everyone remembers the ASCII code table:

 if(vals == 's' && down <=140)

What are you using to send the key-presses?

Com Terminal.

I do not understand why you are using 2 variables, up and down.
If I understand what you are trying to do (and correct me if I am wrong) is to use the two keys to move up and down.

Use a single variable, and increment it or decrement it based on which keys are pressed.

untested code

#include <Servo.h>
int vals = 0;
int angle = 90;

snippage

void loop()
{
      if (Serial.available())
{
   vals = Serial.read();
  
 if(vals == 'w' && angle <=140) 
 {
 angle = angle + 10;  
  servox.write(angle);    
 }
    
   if(vals == 's' && angle >=50)
 {
 angle = angle - 10;  
  servox.write(angle);    
 }
 }
}

Hi I made a new sketch and it works good.

#include <Servo.h>
  int vals = 0;
  int anglex = 85;
  int angley = 83;
  Servo servox;
  Servo servoy;
void setup() {
  servox.attach(2);
  servoy.attach(3);

  
      Serial.begin(9600);
        Serial.println("Servo Serial Control");      
        Serial.println("Press w,a,s,d for directions.");      
        Serial.println("Press c to center.");
        servox.write(85);
        servoy.write(83);
}

void loop() 

{
      if (Serial.available()) 
{

   vals = Serial.read();
   if(vals == 99)
   {
        servox.write(85);
        servoy.write(83);
   }
   else
   { 
   }     
 if(vals == 115 && anglex <=130)
 {
  anglex = anglex + 10;  
  servox.write(anglex);    
 }  
 if(vals == 119 && anglex >=14)
 {
  anglex = anglex - 10;  
  servox.write(anglex);    
 }
 if(vals == 100 && angley <=160)
 {
  angley = angley + 10;  
  servoy.write(angley);    
 }  
 if(vals == 97 && angley >=20)
 {
  angley = angley - 10;  
  servoy.write(angley);    
 }
 }
}

But there is one thing,
when I upload the code and open my COM Terminal it works great for about a minute. Then it freezes and resets.
Sometimes it does so randomly.
So what is the solution? Does this concern the Baud rate?

ibster

Terminal it works great for about a minute. Then it freezes and resets.

Do you have a separate supply for the servos?
You really should do.

if(vals == 99)

I don't remember the ASCII code table either.

Why should I use separate power?

Because servos can draw more current than the Arduino alone can supply, leading to erratic behaviour and brown-outs.