go_away

Author Topic: programming arduino, does not read the input  (Read 2096 times)

0 Members and 1 Guest are viewing this topic.

Offline offyTopic starter

  • Supreme Robot
  • *****
  • Posts: 340
  • Helpful? 1
programming arduino, does not read the input
« on: May 18, 2009, 02:05:22 PM »
I have the following code, and the servos wont move

Code: [Select]
#include <Servo.h>
Servo upservo;
Servo sideservo;
int uppos = 0;
int sidepos = 0;
char val;
void setup() {
upservo.attach(10);
sideservo.attach(9);
  Serial.begin(115200);
}
void loop() {
 if( Serial.available() )       // if data is available to read
  {
    val = Serial.read();         // read it and store it in 'val'
  }
  if( val == 's' )
  {
    uppos = uppos - 5;
    upservo.write(uppos);
    delay(15);
  }
  if( val == 'w' )
  {
    uppos = uppos + 5;
    upservo.write(uppos);
  }
    if( val == 'a' )
  {
    sidepos = sidepos - 5;
    sideservo.write(sidepos);
  }
  if( val == 'd' )
  {
    sidepos = sidepos + 5;
    sideservo.write(sidepos);
  }
  delay(1);
  }

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
Re: programming arduino, does not read the input
« Reply #1 on: May 18, 2009, 03:11:47 PM »
Try printing out all the values you are using to debug your code.

Offline offyTopic starter

  • Supreme Robot
  • *****
  • Posts: 340
  • Helpful? 1
Re: programming arduino, does not read the input
« Reply #2 on: May 18, 2009, 06:08:34 PM »
it prints what I send it.

Offline sonictj

  • Supreme Robot
  • *****
  • Posts: 416
  • Helpful? 11
Re: programming arduino, does not read the input
« Reply #3 on: May 18, 2009, 10:50:11 PM »
I see a few problems.

1) You should initialize val to something.  I would suggest something other than w, s, d, or a.

2) You did not put in a delay for movements associated with a w, a, or d.

3) Once your arduino is sent "val" the servos will continuously move in the direction specified.  The reason is that once "val" is received "val" will not change until a new "val" is sent.  This means that the servo will move an additional 5 degrees every 16ms (specified by your delays).  The way I would solve this would be

Code: [Select]
//create an endless loop so that nothing happens until serial data is sent
while (true)
{
//this if statement will only be executed when data is sent to the arduino
      if (Serial.available() > 0)
      {                     
        val = Serial.read();
//after val is set get out of the loop       
        break;                       
      }
}
//example case
if (val == 's' && uppos > 0)
{
  uppos = uppos - 5;//could also be written uppos-=5 ;)
  upservo.write(uppos);
  delay(15); 
  //this is where you should change val to something other than w, s, a, or d
  //however this is not nessicary with the above while loop.     
}

4) A servo will only rotate a fixed amount of degrees.  Your program will attempt to move the servo beyond those limits. 
example  for when val = 's':

instead of the if statement  if( val == 's' ) use if( val == 's' && uppos > 0 ).  This prevents uppos from becoming a negative number. There should also be an upper limit that prevents uppos from becoming larger than the amount of degrees your servo can move.
« Last Edit: May 18, 2009, 10:55:04 PM by sonictj »

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
Re: programming arduino, does not read the input
« Reply #4 on: May 19, 2009, 03:27:47 AM »
Well for number 4 the Servo library in Arduino will automatically compensate for negative numbers or numbers above 180. But it is still good programming practice to not let the value get under or over.

Offline sonictj

  • Supreme Robot
  • *****
  • Posts: 416
  • Helpful? 11
Re: programming arduino, does not read the input
« Reply #5 on: May 19, 2009, 09:27:31 AM »
Quote
Well for number 4 the Servo library in Arduino will automatically compensate for negative numbers or numbers above 180. But it is still good programming practice to not let the value get under or over.

Thats not the problem.  The variable will go negative. 

example:  if you press s when uppos is 0 nothing happens as far as the servo is concerned, but the variable uppos is now -5.  This causes a prblem when you press w.  W will have to be pressed enough times to make uppos 0 before the servo will move.  That is a problem.

 


Get Your Ad Here

data_list