fuzzy logic

the motors do not work, what I am doing wrong?

#include <Servo.h>
  
  Servo myservo;  // create servo object to control a servo
                  // a maximum of eight servo objects can be created
   
  int pos = 0;    // variable to store the servo position
  
int pingPin = 12;
  
  int EN1 = 6; 
int EN2 = 5;  //Roboduino Motor shield uses Pin 9 
int IN1 = 7; 
int IN2 = 4; //Latest version use pin 4 instead of pin 8 

  void Motor1(int pwm, boolean reverse) 
    { 
    analogWrite(EN1,pwm); //set pwm control, 0 for stop, and 255 for maximum speed 
    if(reverse) 
    { 
    digitalWrite(IN1,HIGH);
    } 
    else 
    { 
    digitalWrite(IN1,LOW); 
    } 
    } 


  void Motor2(int pwm, boolean reverse) 
    { 
    analogWrite(EN2,pwm); 
    if(reverse) 
      {
      digitalWrite(IN2,HIGH); 
      } 
    else 
      { 
      digitalWrite(IN2,LOW); 
      } 
      } 

  void Forward(int speed)
    {
      Motor1(speed,true); //You can change the speed, such as Motor(50,true) 
      Motor2(speed,true); 
    }

  void Backward(int speed)
    {
      Motor1(speed,false); //You can change the speed, such as Motor(50,true) 
      Motor2(speed,false); 
    }

  void Right(int speed)
    {
      Motor1(speed,false); 
      Motor2(speed,true); 
    }

  void Left(int speed)
    {
      Motor1(speed,true); 
      Motor2(speed,false); 
    }

  void Stop()
    {
      Motor1(0,false);
      Motor2(0,false);
    }
  
  
  void setup() {
    // initialize serial communication:
    Serial.begin(115200);
    myservo.attach(11);  // attaches the servo on pin 9 to the servo object
  }
  
  void loop()
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance

  cm = microsecondsToCentimeters(duration);
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
 
  delay(100);
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;


/*    if ((microseconds>=100) && (microseconds<=220) )
    {   //left 

      Motor1(250,true); // M1 forward when accelerometer is set to 0 -10...0 0
      Motor2(250,true); // M2 forward when accelerometer is set to 0 10 0
         
  }
  
   else if ((microseconds>10) && (microseconds<15) )

    {   //left 

      Motor1(250,true); // M1 forward when accelerometer is set to 0 -10...0 0
      Motor2(250,true); // M2 forward when accelerometer is set to 0 10 0
         
  }
}
*/

if ((microseconds>=100) && (microseconds<=220) )
    {   //left 

      Motor1(250,true); // M1 forward when accelerometer is set to 0 -10...0 0
      Motor2(250,true); // M2 forward when accelerometer is set to 0 10 0
         delay(2000);  
  }
  
   else if ((microseconds>10) && (microseconds<15) )

    {   //left 

      Motor1(250,true); // M1 forward when accelerometer is set to 0 -10...0 0
      Motor2(250,true); // M2 forward when accelerometer is set to 0 10 0
        delay(2000); 
  }
}

Moderator: Code tags added.

Your indentation is crazy, but I don’t see where you’re driving the motors.

myservo.attach(11); // attaches the servo on pin 9 to the servo object ... int EN2 = 5; //Roboduino Motor shield uses Pin 9

The comment and the code mismatch ==> The code wins !! ==> check your wiring

Furthermore the calls to the motor commands are never reached in your code.

Advice: follow your code with pen and paper...

valim: what I am doing wrong?

The only calls to your movement functions are in your 'microsecondsToCentimeters()' function. That doesn't seem like a meaningful place to call them. But in any case, the calls are after a return statement so will never execute.

If you want your motors to operate you need to actually call the methods that control them.

I suggest you start with a sketch that simply runs the motors through a set sequence to make sure that side of the software and hardware is working correctly before you try to add in your pinger and steering logic.

how can I add the ping)))
here I have a code that works like a charm :

int EN1  = 6;   
int IN1  = 7;
int EN2  = 5;                         
int IN2  = 4;                           

void Motor1(int pwm, boolean reverse) 
{
  analogWrite(EN1,pwm); //set pwm control, 0 for stop, and 255 for maximum speed 
  if(reverse) 
  {
    digitalWrite(IN1,HIGH);  
  }
  else
  {
    digitalWrite(IN1,LOW); 
  }
}

void Motor2(int pwm, boolean reverse)
{
  analogWrite(EN2,pwm);
  if(reverse)
  {
    digitalWrite(IN2,HIGH); 
  }
  else
  {
    digitalWrite(IN2,LOW); 
  }
}

void setup() 
{ 
    int i; 
    for(i=5;i<=8;i++) 
    pinMode(i, OUTPUT);
    
    //Serial.begin(19200);  
} 

void loop() 
{ 
  int x,delay_en; 
        Motor1(250,true);  //You can change the speed, such as Motor(50,true) 
        Motor2(250,true); 
        delay(10000);
        Motor1(200,false); 
        Motor2(200,true); 
        delay(1000);
        Motor1(50,true); 
        Motor2(50,true);  
        delay(1000);   
        Motor1(250,true);  //You can change the speed, such as Motor(50,true) 
        Motor2(250,true);  
        delay(10000);
        Motor1(200,true); 
        Motor2(200,false); 
        delay(1000);
        Motor1(50,true); 
        Motor2(50,true);
        delay(1000);   
        Motor1(250,true);  //You can change the speed, such as Motor(50,true) 
        Motor2(250,true);  
        delay(10000);        
               
        
  /*
  char val; 
  while(1) 
  {
    val = Serial.read(); 
    if(val!=-1) 
    {
      switch(val) 
      {
        case 'w'://Move ahead 
        Motor1(250,true);  //You can change the speed, such as Motor(50,true) 
        Motor2(250,true); 
        break;
        
        case 'x'://move back 
        Motor1(250,false); 
        Motor2(250,false); 
        break;    
        
        case 'l'://turn left 
        Motor1(200,false); 
        Motor2(200,true); 
        delay(1000);
        Motor1(50,false); 
        Motor2(50,false);                 
        break;
        
        case 'r'://turn right 
        Motor1(200,true); 
        Motor2(200,false); 
        delay(1000);
        Motor1(50,false); 
        Motor2(50,false);         
        break;
        
        case 's'://stop 
        Motor1(0,false); 
        Motor2(0,false); 
        break;
      }
    }    
  }
  */
}

so I have both the ping and the motors... how can I link the ping distance to motors ?

int EN1 = 6;  
int EN2 = 5;  //Roboduino Motor shield uses Pin 9
int IN1 = 7;
int IN2 = 4; //Latest version use pin 4 instead of pin 8



void Motor1(int pwm, boolean reverse)
        {
          analogWrite(EN1,pwm); //set pwm control, 0 for stop, and 255 for maximum speed
         if(reverse)
         { 
          digitalWrite(IN1,HIGH);    
         }
        else
        {
          digitalWrite(IN1,LOW);    
         }
        }  
        
void Motor2(int pwm, boolean reverse)
        {
          analogWrite(EN2,pwm);
         if(reverse)
         { 
          digitalWrite(IN2,HIGH);    
         }
        else
        {
          digitalWrite(IN2,LOW);    
         }
        }
const int pingPin = 12;

 void setup()
 {
  // initialize serial communication:
  Serial.begin(115200);
}


 void loop()

 {
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(100);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

“something” here in my code is the value of the ping))) in cm and I do not know what to write…

if ( something < 5)

   {
    Motor1(0,false);
    Motor2(0,false);
  }
else ( something > 200)
{
     Motor1(250,true);
     Motor2(250,true);
}

basic simple and stupid…

but I did some interesting projects until now :

4W Evilos V4 robot platform + Bluetooth + Arduino + Amarino + Android + HTC Wildfire

I twisted a code : http://robotx.ro/download/amarino_android_htc.pde

try this…

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position

int pingPin = 12;

int EN1 = 6; 
int EN2 = 5;  //Roboduino Motor shield uses Pin 9 
int IN1 = 7; 
int IN2 = 4; //Latest version use pin 4 instead of pin 8 



void setup() 
{
  // initialize serial communication:
  Serial.begin(115200);
  myservo.attach(11);  // attaches the servo on pin 9 to the servo object
}

void loop()
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance

  cm = microsecondsToCentimeters(duration);
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  if (cm < 10)
  {
    Backwards(100);
  } 
  else if (cm < 20)
  {
    Left(50);
  }
  else if (cm < 100)
  {
    Forward(100);
  }
  else
  {
    Right(255);
  }
  delay(100);
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}


void Motor1(int pwm, boolean reverse) 
{ 
  analogWrite(EN1,pwm); //set pwm control, 0 for stop, and 255 for maximum speed 
  if(reverse) 
  { 
    digitalWrite(IN1,HIGH);
  } 
  else 
  { 
    digitalWrite(IN1,LOW); 
  } 
} 


void Motor2(int pwm, boolean reverse) 
{ 
  analogWrite(EN2,pwm); 
  if(reverse) 
  {
    digitalWrite(IN2,HIGH); 
  } 
  else 
  { 
    digitalWrite(IN2,LOW); 
  } 
} 

void Forward(int speed)
{
  Motor1(speed, true); //You can change the speed, such as Motor(50,true) 
  Motor2(speed, true); 
}

void Backward(int speed)
{
  Motor1(speed, false); //You can change the speed, such as Motor(50,true) 
  Motor2(speed, false); 
}

void Right(int speed)
{
  Motor1(speed, false); 
  Motor2(speed, true); 
}

void Left(int speed)
{
  Motor1(speed, true); 
  Motor2(speed, false); 
}

void Stop()
{
  Motor1(0,false);
  Motor2(0,false);
}

Thanks Rob

here is a result

http://youtu.be/pbzm1d0xjvE

welcome,

vid stops @ 0.44 but it is clear the motors run. Now it is time for you to get some real navigation logic in it :)

advice:

put all the distance stuf in a separate function: cm = getDistance(PIN1);

that keeps your loop() clean and you could create a left and right eye or a rotating ping dev.

If you have more questions or a new vid, just post it to this thread