Loading...
Pages: [1]   Go Down
Author Topic: Servo +10  (Read 309 times)
0 Members and 1 Guest are viewing this topic.
Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 26
bla-bla-bla
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi I'm struggling with some code:

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.
« Last Edit: June 23, 2010, 01:26:58 pm by ibster » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not everyone remembers the ASCII code table:
Code:
if(vals == 's' && down <=140)

What are you using to send the key-presses?
« Last Edit: June 23, 2010, 01:54:02 pm by AWOL » Logged

Pete, it's a fool looks for logic in the chambers of the human heart.

Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 26
bla-bla-bla
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Com Terminal.
Logged

Ohio
Offline Offline
Full Member
***
Karma: 0
Posts: 227
Arduino Hexapod
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
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);    
 }
 }
}
« Last Edit: June 23, 2010, 02:50:16 pm by vinceherman » Logged

Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 26
bla-bla-bla
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi I made a new sketch and it works good.
Code:
#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
Logged

UK
Offline Offline
Faraday Member
**
Karma: 15
Posts: 2852
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Code:
if(vals == 99)
I don't remember the ASCII code table either.
« Last Edit: June 24, 2010, 04:24:45 am by GrooveFlotilla » Logged

Per Arduino ad Astra

Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 26
bla-bla-bla
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why should I use separate power?
Logged

UK
Offline Offline
Faraday Member
**
Karma: 15
Posts: 2852
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Per Arduino ad Astra

Pages: [1]   Go Up
Print
 
Jump to: