Pages: 1 2 [3] 4   Go Down
Author Topic: i dont get it  (Read 4067 times)
0 Members and 1 Guest are viewing this topic.
ottawa, canada
Offline Offline
God Member
*****
Karma: 6
Posts: 993
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

at the beginning of loop
Code:
switch(state)
  {
    state = 1 ;
    Serial.print("in state 1 ");
     Serial.println();
    case 1:
    ping_read();
    drive_forward();

should probably be

 switch(state)
  {
     case 1:
    Serial.print("in state 1 ");
    Serial.println();
   ping_read();
   drive_forward();



and do you need a delay after drive_left() in state  5 to give it a chance to make a turn?

and shouldn't state 1 recenter the servo? before it reads it?
« Last Edit: December 02, 2010, 12:15:47 pm by bill2009 » Logged

Bill Rowe
Olduino - An Arduino for the First of Us
www.olduino.wordpress.com

Hyderabad , India
Offline Offline
God Member
*****
Karma: 6
Posts: 621
can't help not to think arduinaizing something !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
and do you need a delay after drive_left() in state  5 to give it a chance to make a turn?

and shouldn't state 1 recenter the servo? before it reads it?
fixed it !
But still cant get it to go forward  :-?
Logged

Hyderabad , India
Offline Offline
God Member
*****
Karma: 6
Posts: 621
can't help not to think arduinaizing something !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

and another problem !  :smiley
as soon as the osbtacle is detected the servo is set to 10 degrees and the leftwheel starts rotating without looking to thte right
Logged

ottawa, canada
Offline Offline
God Member
*****
Karma: 6
Posts: 993
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
fixed it !
But still cant get it to go forward

can you describe the behaviour now?
Logged

Bill Rowe
Olduino - An Arduino for the First of Us
www.olduino.wordpress.com

ottawa, canada
Offline Offline
God Member
*****
Karma: 6
Posts: 993
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
and another problem !  
as soon as the osbtacle is detected the servo is set to 10 degrees and the leftwheel starts rotating without looking to thte right

do you need a delay after commanding the servo to move to give it a chance to get there?  Looking at this: http://www.arduino.cc/playground/ComponentLib/Servo

they have a delay of 15-20ms.
Logged

Bill Rowe
Olduino - An Arduino for the First of Us
www.olduino.wordpress.com

Hyderabad , India
Offline Offline
God Member
*****
Karma: 6
Posts: 621
can't help not to think arduinaizing something !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

the behaviour didnt change a bit !
 by fixed it i eamnt thatr i set the code accordingly !
it dosent move forward !
only when a obstacle is detected it moves the servo and even be fore it looks to the right it starts rotaitng the left wheel !  >smiley-sad
and i havew checked all the drive functiois they run as they sould when tried out individually
Logged

ottawa, canada
Offline Offline
God Member
*****
Karma: 6
Posts: 993
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

At the beginning of loop, before the cases, please print millis(), the current state, and the fwd, left, and right distances.
Logged

Bill Rowe
Olduino - An Arduino for the First of Us
www.olduino.wordpress.com

Hyderabad , India
Offline Offline
God Member
*****
Karma: 6
Posts: 621
can't help not to think arduinaizing something !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
At the beginning of loop, before the cases, please print millis(), the current state, and the fwd, left, and right distances.
i'm prteey much confused a example ? :smiley
Logged

ottawa, canada
Offline Offline
God Member
*****
Karma: 6
Posts: 993
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Serial.println(" ");Serial.print(millis()); Serial.print(":"); Serial.print(state); Serial.print(" "); Serial.print(cm);  and so on


did you put a delay after the servo commands and do you now see it moving?
Logged

Bill Rowe
Olduino - An Arduino for the First of Us
www.olduino.wordpress.com

Hyderabad , India
Offline Offline
God Member
*****
Karma: 6
Posts: 621
can't help not to think arduinaizing something !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
did you put a delay after the servo commands and do you now see it moving?
Yes the servo is working as it should  smiley
ill add those
my problem is that
only when a obstacle is detected it moves the servo and even be fore it looks to the right it starts rotaitng the left wheel !
Logged

ottawa, canada
Offline Offline
God Member
*****
Karma: 6
Posts: 993
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok.  put in the prints and re-post your code and the output.
Logged

Bill Rowe
Olduino - An Arduino for the First of Us
www.olduino.wordpress.com

Hyderabad , India
Offline Offline
God Member
*****
Karma: 6
Posts: 621
can't help not to think arduinaizing something !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

CODE
Code:
///setting up pin no's ////


#include <Servo.h>            
#define ledpin_right 13                  
#define ledpin_left 12
#define motor_ONE_pin_ONE 3          
#define motor_ONE_pin_TWO 4    
#define motor_TWO_pin_ONE 5      
#define motor_TWO_pin_TWO 6
const int pingPin = 10;
int pos = 0 ;
Servo myservo;
int state = 1;
long duration,cm;
long distance_right = 0 ;
long distance_left = 0 ;
 
 
/////setting pinmode//////


void setup()
{
  myservo.attach(9);
  Serial.begin(9600);                            
  pinMode(motor_ONE_pin_ONE,OUTPUT);    
  pinMode(motor_ONE_pin_TWO,OUTPUT);    
  pinMode(motor_TWO_pin_ONE,OUTPUT);    
  pinMode(motor_TWO_pin_TWO,OUTPUT);    
}


//////looping funtoinn//////


void loop()
{
  switch(state)
  {
    Serial.print(cm);
    Serial.print("dist forward");
    Serial.print(distance_right);
    Serial.print("dist_right");
    Serial.print(distance_left);
    Serial.print("dist_left");
    Serial.print(state);
    Serial.print ("state");
    Serial.print(millis());
    state = 1 ;
    Serial.print("in state 1 ");
    Serial.println();
    case 1:
    ping_read();
    drive_forward();
    myservo.write(80);
    if(cm < 10 )
    {
      state = 2;
      Serial.print("switching to state 2");

      Serial.println();
      
    }
    break;
    case 2:
    ping_read_left_right();
    state =3;
    Serial.print("in state 4");
    Serial.println();
    break;
    case 3:
    if(distance_right > distance_left )
    {
      state =4;
      Serial.print("switching to state 4 ");  
    }
    else if (distance_right < distance_left )
    {
      state =5;
      Serial.print("switching to state 5 ");
      Serial.println();
    }
    else if ( (distance_right<10) && (distance_left<10) )
    {
      state =6;
      Serial.print("switching to state 6 ");
      Serial.println();
    }
    
    else
    {
      state = 1 ;
      Serial.print("switching to state 1 ");
      Serial.println();
    }
    case 4:
    drive_right();
    state = 1 ;
    break;
    case 5:  
    drive_left();
    state = 1;
    break;
    case 6:
    drive_360();
    state = 1;
    break;
    default: ;
  }
}
    



//////ping functions///////

void ping_read()
{
  pinMode(pingPin, OUTPUT);            
  digitalWrite(pingPin, LOW);          
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);          
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);          
  pinMode(pingPin, INPUT);                    
  duration = pulseIn(pingPin, HIGH);          
  cm = microsecondsToCentimeters(duration);
  Serial.print(cm);        
  Serial.print("cm");      
  Serial.println();
  return;
}
void ping_read_left_right()
{
  myservo.write(10);
  delay(350);
  pinMode(pingPin, OUTPUT);            
  digitalWrite(pingPin, LOW);          
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);          
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);          
  pinMode(pingPin, INPUT);                    
  duration = pulseIn(pingPin, HIGH);          
  distance_left = microsecondsToCentimeters(duration);
  Serial.print( distance_left);      
  Serial.print("cm");    
  Serial.println();
  myservo.write(180);
  delay(350);
  pinMode(pingPin, OUTPUT);            
  digitalWrite(pingPin, LOW);          
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);          
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);          
  pinMode(pingPin, INPUT);                    
  duration = pulseIn(pingPin, HIGH);          
  distance_right = microsecondsToCentimeters(duration);
  Serial.print( distance_left);;      
  Serial.print("cm");    
  Serial.println();
  return;

}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

///////drive functions//////


void drive_forward()
{
  digitalWrite(motor_ONE_pin_ONE,HIGH);
  digitalWrite(motor_TWO_pin_ONE,LOW);
  digitalWrite(motor_ONE_pin_TWO,HIGH);
  digitalWrite(motor_TWO_pin_TWO,LOW);
  return;
}


void drive_stop()
{
  digitalWrite(motor_ONE_pin_ONE,LOW);
  digitalWrite(motor_TWO_pin_ONE,LOW);
  digitalWrite(motor_ONE_pin_TWO,LOW);
  digitalWrite(motor_TWO_pin_TWO,LOW);
  return;
}

void drive_right()
{
  digitalWrite(motor_ONE_pin_ONE,HIGH);
  digitalWrite(motor_TWO_pin_ONE,LOW);
  digitalWrite(motor_ONE_pin_TWO,LOW);
  digitalWrite(motor_TWO_pin_TWO,LOW);
  delay(1000);
  return;
}

void drive_left()
{
  digitalWrite(motor_ONE_pin_ONE,LOW);
  digitalWrite(motor_TWO_pin_ONE,LOW);
  digitalWrite(motor_ONE_pin_TWO,HIGH);
  digitalWrite(motor_TWO_pin_TWO,LOW);
  delay(1000);
  return;
  
}

void drive_360()
{
  digitalWrite(motor_ONE_pin_ONE,HIGH);
  digitalWrite(motor_TWO_pin_ONE,LOW);
  digitalWrite(motor_ONE_pin_TWO,LOW);
  digitalWrite(motor_TWO_pin_TWO,LOW);
  return;
}

but i dont see nay vaules other than the value of cm coming up and when obstructed something like this

[glow]................................................[/glow]4cm
in state 4
switching to state 4 8cm
switching to state 2
4cm
4cm
in state 4
switching to state 4 7cm
switching to state 2
3cm
3cm
in state 4
switching to state 4 6cm
switching to state 2
3cm
3cm
in state 4
switching to state 4 26cm
26cm
26cm
Logged

ottawa, canada
Offline Offline
God Member
*****
Karma: 6
Posts: 993
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

don't put stuff inside switch before the first case

Code:
void loop()
{
  switch(state)
  {
    Serial.print(cm);
    Serial.print("dist forward");
    Serial.print(distance_right);
    Serial.print("dist_right");
    Serial.print(distance_left);
    Serial.print("dist_left");
    Serial.print(state);
    Serial.print ("state");
    Serial.print(millis());
    state = 1 ;
    Serial.print("in state 1 ");
    Serial.println();

should be

void loop()
{
    Serial.print(cm);
    Serial.print("dist forward");
    Serial.print(distance_right);
    Serial.print("dist_right");
    Serial.print(distance_left);
    Serial.print("dist_left");
    Serial.print(state);
    Serial.print ("state");
    Serial.print(millis());
    Serial.println();

  switch(state)
  {



notice that i removed a couple of lines as well which were inappropriate.
Logged

Bill Rowe
Olduino - An Arduino for the First of Us
www.olduino.wordpress.com

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 40
Posts: 5590
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Just for everyone's edification on the subject, I am involved in the following thread as well on "state machines":

http://www.electro-tech-online.com/general-electronics-chat/113718-state-machine-mcu.html

Thought I'd pass it along, it may help somebody out there...?

 smiley

Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Hyderabad , India
Offline Offline
God Member
*****
Karma: 6
Posts: 621
can't help not to think arduinaizing something !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

i added the srial prints the result is same!
Quote
99dist forward93dist_right20dist_left1state3017498cm
98dist forward93dist_right20dist_left1state3023598cm
98dist forward93dist_right20dist_left1state3029698cm
98dist forward93dist_right20dist_left1state3035698cm
98dist forward93dist_right20dist_left1state3041698cm
98dist forward93dist_right20dist_left1state3047798cm
98dist forward93dist_right20dist_left1state3053798cm
98dist forward93dist_right20dist_left1state3059998cm
98dist forward93dist_right20dist_left1state3065998cm
98dist forward93dist_right20dist_left1state3072098cm
98dist forward93dist_right20dist_left1state3078098cm
98dist forward93dist_right20dist_left1state3084098cm
98dist forward93dist_right20dist_left1state3090298cm
98dist forward93dist_right20dist_left1state3096298cm
98dist forward93dist_right20dist_left1state3102398cm
98dist forward93dist_right20dist_left1state3108398cm
98dist forward93dist_right20dist_left1state3114398cm
98dist forward93dist_right20dist_left1state3120498cm
98dist forward93dist_right20dist_left1state3126598cm
98dist forward93dist_right20dist_left1state3132698cm
98dist forward93dist_right20dist_left1state3138698cm
98dist forward93dist_right20dist_left1state3144798cm
98dist forward93dist_right20dist_left1state3150797cm
97dist forward93dist_right20dist_left1state3156797cm
97dist forward93dist_right20dist_left1state3162997cm
97dist forward93dist_right20dist_left1state3168997cm
97dist forward93dist_right20dist_left1state3175097cm
97dist forward93dist_right20dist_left1state3181097cm
97dist forward93dist_right20dist_left1state3187097cm
97dist forward93dist_right20dist_left1state31931132cm
132dist forward93dist_right20dist_left1state3199698cm
98dist forward93dist_right20dist_left1state32057132cm
132dist forward93dist_right20dist_left1state3212198cm
98dist forward93dist_right20dist_left1state3218298cm
98dist forward93dist_right20dist_left1state3224298cm
98dist forward93dist_right20dist_left1state3230498cm
98dist forward93dist_right20dist_left1state3236498cm
98dist forward93dist_right20dist_left1state3242498cm
98dist forward93dist_right20dist_left1state3248598cm
98dist forward93dist_right20dist_left1state3254598cm
98dist forward93dist_right20dist_left1state3260610cm
10dist forward93dist_right20dist_left1state326626cm
switching to state 2
6dist forward93dist_right20dist_left2state3273915cm
15cm
in state 4
6dist forward93dist_right15dist_left3state33517switching to state 4 6dist forward93dist_right15dist_left1state3458664cm
64dist forward93dist_right15dist_left1state3464454cm
54dist forward93dist_right15dist_left1state3470394cm
94dist forward93dist_right15dist_left1state3476394cm
94dist forward93dist_right15dist_left1state3482393cm
93dist forward93dist_right15dist_left1state34883132cm
132dist forward93dist_right15dist_left1state34949132cm
132dist forward93dist_right15dist_left1state3501398cm
98dist forward93dist_right15dist_left1state3507498cm
98dist forward93dist_right15dist_left1state3513498cm
98dist forward93dist_right15dist_left1state3519498cm
98dist forward93dist_right15dist_left1state3525698cm
98dist forward93dist_right15dist_left1state3531698cm
98dist forward93dist_right15dist_left1state3537798cm
98dist forward93dist_right15dist_left1state3543799cm
99dist forward93dist_right15dist_left1state3549798cm
98dist forward93dist_right15dist_left1state3555898cm
98dist forward93dist_right15dist_left1state3561998cm
98dist forward93dist_right15dist_left1state3568098cm
98dist forward93dist_right15dist_left1state3574098cm
98dist forward93dist_right15dist_left1state3580198cm
98dist forward93dist_right15dist_left1state3586198cm
98dist forward93dist_right15dist_left1state3592198cm
98dist forward93dist_right15dist_left1state3598398cm
98dist forward93dist_right15dist_left1state3604398cm
98dist forward93dist_right15dist_left1state3610498cm
98dist forward93dist_right15dist_left1state3616498cm
98dist forward93dist_right15dist_left1state3622598cm
98dist forward93dist_right15dist_left1state3628698cm
98dist forward93dist_right15dist_left1state3634698cm
98dist forward93dist_right15dist_left1state3640798cm
98dist forward93dist_right15dist_left1state3646798cm
98dist forward93dist_right15dist_left


the problem is
only when a obstacle is detected it moves the servo and even be fore it looks to the right it starts rotaitng the left wheel  no drive forwaerd occurs
Logged

Pages: 1 2 [3] 4   Go Up
Jump to: