Go Down

Topic: Code support for Dc motor and Servo reacting to IR Sensor feedback (Read 103 times) previous topic - next topic

Scenes_From_Sound

Apologies in advance if this question has been answered, I could not find it on this forum (or anywhere else, for that matter).

I'm trying to develop my coding skills by weaving together two tasks together and have exhausted my ideas on solving this.

What I am working on:
A four wheeled rover with a DC motor driving two wheels, a servo turning the other set of wheels and an IR
Sensor that provides feedback on how to behave.

What I want the rover to do:

Move forward until the IR Sensor detects an obstacle.
Move backward and turn the wheels to reorient the rover.
Move forward again.

So far, I have been successful at either turning the wheels left and right or rolling forward and backward as separate sketches.
My code is missing the component that weaves these two activities together.

Here is my code so far:

Code: [Select]
#include <Servo.h>

const int EN=9;  //Half bridge 1 enable
const int MC1=3;  //MOtor control 1 on pin 3
const int MC2=2;  //MOtor control 2 on pin 2
const int IR=0;  //IR on analog pin 0
const int SERVO=10; //Half bridge 3,4 enable


Servo myServo;  //Servo object
int dist1 = 0;  //forward
int dist2 = 0;  //reverse
int dist3 = 0;  //brake

int val = 0;   //for storing the reading from the pot
int velocity = 0;  //For storing the desired velocity (from 0-255)

void setup()
{
  Serial.begin(9600);
  myServo.attach(SERVO);
  
  pinMode(EN, OUTPUT);
  pinMode(MC1, OUTPUT);
  pinMode(MC2, OUTPUT);
  
  brake();  //Initialize with motor stopped
}

void loop()
{
  Serial.println(val);
  val = analogRead(IR);    //read IR
  val = map(val, 0, 1023, 0, 179);  //scale it to servo range
  myServo.write(val);    //sets the servo
  //delay(15);           //waits for the servo

  dist1 = readDistance(0);  //measure IR distance at zero degrees
    analogWrite(SERVO, dist1);
    delay(300);             //delay before next measurement

  dist2 = readDistance(25);  //measure IR distance at zero degrees
    analogWrite(SERVO, dist2);
    delay(300);

  dist3 = readDistance(0);
    analogWrite(SERVO, dist1);
    delay(300);              //delay before next measurement    
 

  //go forward
  if (val > 108)
  {
    velocity = map(val, 108, 1023, 0, 255);
    forward(velocity);
    digitalWrite(MC2, 140);
    
    //dist1 = readDistance(0);  //measure IR distance at zero degrees
    //analogWrite(SERVO, dist1);
    //delay(30);             //delay before next measurement
  }

  //go backward
  else if (val < 107)
  {
    velocity = map(val, 107, 1023, 0, 255);
    reverse(velocity);
    digitalWrite(MC1, 190);
    dist2 = readDistance(15);  //measure IR distance at zero degrees
    
    //analogWrite(SERVO, dist2);
    //delay(30);
  }

  //brake
  else
  {
    brake();
    dist3 = readDistance(0);
    analogWrite(SERVO, dist1);
    delay(30);              //delay before next measurement
  }

 
}


//Motor goes forward at given rate (from 0-255)

void forward (int rate)
{
  digitalWrite(EN, HIGH);
  digitalWrite(MC1, HIGH);
  digitalWrite(MC2, LOW);
  analogWrite(EN, rate);
}



//Motor goes backward at given rate (from 0-255)
void reverse (int rate)

{
  digitalWrite(EN, HIGH);
  digitalWrite(MC1, LOW);
  digitalWrite(MC2, HIGH);
  analogWrite(EN, rate);
  
}



//stops motor
void brake()
{
  digitalWrite(EN, LOW);
  digitalWrite(MC1, LOW);
  digitalWrite(MC2, LOW);
  digitalWrite(EN, LOW);
}

int readDistance(int pos)
{
  
  int dist = analogRead(IR);  //read ir sensor
                
}


Attached are the circuits. IR replaces the trim pot.
Any advise is appreciated. Thank you.

slipstick

Well it doesn't compile because you have an extra }. Also readDistance() takes a parameter which is never used and is declared as an int but doesn't return anything. That's not going to help.

There may be other problems but why not at least get it compiling first then test it and let us know what it does and doesn't do.


Steve

Scenes_From_Sound


Scenes_From_Sound

Well it doesn't compile because you have an extra }. Also readDistance() takes a parameter which is never used and is declared as an int but doesn't return anything. That's not going to help.

There may be other problems but why not at least get it compiling first then test it and let us know what it does and doesn't do.


Steve
I'm not seeing an extra } (was it in the void loop() area?)

It compiles, but only the direction guiding servo works. The forward and backward servo does not.
This puts me back to square one. I can fanangle the code to perform one function and ignore the other function.

I think I am crossing the variables declared either in the void setup() or void loop() sections. Somehow, the servo and motor need to use the same IR sensor data but perform their separate tasks.

If it were pseudo code, the logic I am after would be:

move forward  //until encountering an object at 88>=i<=102
brake
move backward while turning direction for 5 seconds then straighten wheels
brake
repeat



Scenes_From_Sound

For what it's worth, here is my script as of right now.

I think I set the IR sensor twice, and it should only be set once.
Could this be the issue?

Code: [Select]
#include <Servo.h>

const int EN=9;  //Half bridge 1 enable
const int MC1=3;  //MOtor control 1 on pin 3
const int MC2=2;  //MOtor control 2 on pin 2
const int IR=0;  //IR on analog pin 0
const int SERVO=10; //Half bridge 3,4 enable


Servo myServo;  //Servo object
int dist1 = 0;  //forward
int dist2 = 0;  //reverse
int dist3 = 0;  //brake

int val = 0;   //for storing the reading from the pot
int velocity = 0;  //For storing the desired velocity (from 0-255)

void setup()
{
  Serial.begin(9600);
  myServo.attach(SERVO);
  
  pinMode(EN, OUTPUT);
  pinMode(MC1, OUTPUT);
  pinMode(MC2, OUTPUT);
  
  brake();  //Initialize with motor stopped
}

void loop()
{
  Serial.println(val);
  val = analogRead(IR);    //read IR
  val = map(val, 0, 1023, 0, 179);  //scale it to servo range
  myServo.write(val);    //sets the servo
  //delay(15);           //waits for the servo

  
    analogWrite(SERVO, dist1);
    delay(300);             //delay before next measurement

  dist2 = readDistance(25);  //measure IR distance at zero degrees
    analogWrite(SERVO, dist2);
    delay(300);

  dist3 = readDistance(0);
    analogWrite(SERVO, dist1);
    delay(300);              //delay before next measurement    


  //go forward
  if (val > 108)
  {
    velocity = map(val, 108, 1023, 0, 255);
    forward(velocity);
    digitalWrite(MC2, 140);
    
    //dist1 = readDistance(0);  //measure IR distance at zero degrees
    //analogWrite(SERVO, dist1);
    //delay(30);             //delay before next measurement
  }

  //go backward
  else if (val < 107)
  {
    velocity = map(val, 107, 1023, 0, 255);
    reverse(velocity);
    digitalWrite(MC1, 190);
    dist2 = readDistance(15);  //measure IR distance at zero degrees
    
    //analogWrite(SERVO, dist2);
    //delay(30);
  }

  //brake
  else
  {
    brake();
    dist3 = readDistance(0);
    analogWrite(SERVO, dist1);
    delay(30);              //delay before next measurement
  }
}

//Motor goes forward at given rate (from 0-255)

void forward (int rate)
{
  digitalWrite(EN, HIGH);
  digitalWrite(MC1, HIGH);
  digitalWrite(MC2, LOW);
  analogWrite(EN, rate);
}

//Motor goes backward at given rate (from 0-255)
void reverse (int rate)

{
  digitalWrite(EN, HIGH);
  digitalWrite(MC1, LOW);
  digitalWrite(MC2, HIGH);
  analogWrite(EN, rate);
  
}



//stops motor
void brake()
{
  digitalWrite(EN, LOW);
  digitalWrite(MC1, LOW);
  digitalWrite(MC2, LOW);
  digitalWrite(EN, LOW);
}

int readDistance(int pos)
{
  
  int dist = analogRead(IR);  //read ir sensor
                
}

Go Up