Question about my Sketch.

johncc:
OmegaRA,

Have you been able to get your motor to even turn on, using the code I posted in reply #11 or UKHeliBobs in #20?

This would confirm whether your shield is working and your wiring is valid...

John

okay, yes, your sketch gets the one motor spinning :slight_smile:

That's great-- your shield and wiring is working!

void leftMotor( int direction, int speed)
{
    digitalWrite( LEFT_MOTOR, direction); //Establishes direction fwd/back
    digitalWrite( LEFT_BRAKE , LOW);      //Disengage the Brake
    digitalWrite( LEFT_SPINNER, speed);   //Spins the motor
}

Can you see how you would add a rightMotor() ?

Cheers,
John

johncc:
That's great-- your shield and wiring is working!

void leftMotor( int direction, int speed)

{
   digitalWrite( LEFT_MOTOR, direction); //Establishes direction fwd/back
   digitalWrite( LEFT_BRAKE , LOW);      //Disengage the Brake
   digitalWrite( LEFT_SPINNER, speed);   //Spins the motor
}
void rightMotor( int direction, int speed)
{
   digitalWrite( RIGHT_MOTOR, direction); //Establishes direction fwd/back
   digitalWrite( RIGHT_BRAKE , LOW);      //Disengage the Brake
   digitalWrite( RIGHT_SPINNER, speed);   //Spins the motor
}




Can you see how you would add a rightMotor() ?

Cheers,
John

seem right for this section of code?
Here is a quick attempt at adding the Right pins and void setup, a little confused as to where to put stuff in the loop...

/*
   Mowbot Test Sketch 1 -- Motor
 */
const int RIGHT_MOTOR=13, RIGHT_BRAKE=8, RIGHT_SPINNER=11;
const int LEFT_MOTOR=12, LEFT_BRAKE=9, LEFT_SPINNER=3;
const int FORWARD=HIGH, REVERSE=LOW;
const int FULLSPEED=255;

void leftMotor( int direction, int speed)
{
  digitalWrite( LEFT_MOTOR, direction); //Establishes direction of Channel A
  digitalWrite( LEFT_BRAKE , LOW);      //Disengage the Brake
  digitalWrite( LEFT_SPINNER, speed);   //Spins the motor
}
void rightMotor( int direction, int speed)
{
  digitalWrite( RIGHT_MOTOR, direction); //Establishes direction of Channel B
  digitalWrite( RIGHT_BRAKE , LOW);      //Disengage the Brake
  digitalWrite( RIGHT_SPINNER, speed);   //Spins the motor
}
void setup() 
{ 
  pinMode(LEFT_MOTOR, OUTPUT);   
  pinMode(LEFT_BRAKE, OUTPUT);
  leftMotor( FORWARD, FULLSPEED);
  pinMode(RIGHT_MOTOR, OUTPUT);
  pinMode(RIGHT_BRAKE, OUTPUT);
  rightMotor( FORWARD, FULLSPEED);
  
void loop() 
{
  leftMotor( FORWARD, FULLSPEED);
  delay( 4000); 	   // on and off every 4 seconds
  leftMotor( FORWARD, 0 );
  delay( 4000);
}

OmegaRa:
seem right for this section of code?
Here is a quick attempt at adding the Right pins and void setup, a little confused as to where to put stuff in the loop...

void setup()   // This runs once when you power up or reset 

{
 pinMode(LEFT_MOTOR, OUTPUT);  
 pinMode(LEFT_BRAKE, OUTPUT);
 leftMotor( FORWARD, FULLSPEED);   // *** don't need this here
 pinMode(RIGHT_MOTOR, OUTPUT);
 pinMode(RIGHT_BRAKE, OUTPUT);
 rightMotor( FORWARD, FULLSPEED);  // *** don't need this here
 
void loop()   // This one loops over and over
{
 leftMotor( FORWARD, FULLSPEED);
 delay( 4000);   // on and off every 4 seconds
 leftMotor( FORWARD, 0 );
 delay( 4000);
}  // it goes from here back to the start of loop instantly

I would recommend removing the indicated *** lines in setup, Compile and run, the motor should go on 4 seconds and then off 4 seconds, on 4, off 4. Then in loop() just change the leftMotors()s to rightMotor(). You're just making sure the motor works here.
Also see if you can add the lines in the loop, to test the reverse:
right fwd full
delay
right fwd 0 (stop)
delay
right reverse full
delay

Modify for both motors fwd, 1 fwd 1 reverse, half speed (128), etc.

Make sure you understand the concept that setup runs once, first-- and loop then runs forever, over and over.

Cheers,
John

Okay, I will test that out when I get home from work.

OmegaRa:
I am making a Robot Lawnmower and it is very basic.

Does any of this seem wrong?

Besides learning with a first sketch that runs something that could seriously hurt someone?
What could seem wrong?

I built it with a blade cover, and it is low to the ground, small chance of it hurting someone, but I hear you.

johncc:
I would recommend removing the indicated *** lines in setup, Compile and run, the motor should go on 4 seconds and then off 4 seconds, on 4, off 4. Then in loop() just change the leftMotors()s to rightMotor(). You're just making sure the motor works here.
Also see if you can add the lines in the loop, to test the reverse:
right fwd full
delay
right fwd 0 (stop)
delay
right reverse full
delay

Modify for both motors fwd, 1 fwd 1 reverse, half speed (128), etc.

Make sure you understand the concept that setup runs once, first-- and loop then runs forever, over and over.

Cheers,
John

Okay, I did all that and got the motors working, not both at the same time, but I got each motor to go forward and backward, and switching back and forth.

You could bury a powered cable under the lawn and use a magnetic field sensor or two on the mower to follow that like a toy line-follower robot. You'd get a perfect path every time regardless of wheel slip, etc. No map or mapping required, just one long piece of single conductor wire and some time making and filling very thin, shallow trenches.

Could probably run something like that with a Tiny.

GoForSmoke:
You could bury a powered cable under the lawn and use a magnetic field sensor or two on the mower to follow that like a toy line-follower robot. You'd get a perfect path every time regardless of wheel slip, etc. No map or mapping required, just one long piece of single conductor wire and some time making and filling very thin, shallow trenches.

Could probably run something like that with a Tiny.

This is more or less the plan eventually. Right now I am just going to use it in my backyard, which is fully fenced in, so I don't need sensors yet, just bumpers to make it turn around should it run into anything.

here are links to a couple pictures of what it looks like so far

http://sdrv.ms/14EWuxa

http://sdrv.ms/14EWqxI

http://sdrv.ms/14EWyNz

I have to properly mount the Board and give it some paint...but overall that is it, minus it's covers

OmegaRa:
Okay, I did all that and got the motors working, not both at the same time, but I got each motor to go forward and backward, and switching back and forth.

Did they fail to run at the same time or was that how you programmed it ?

UKHeliBob:

OmegaRa:
Okay, I did all that and got the motors working, not both at the same time, but I got each motor to go forward and backward, and switching back and forth.

Did they fail to run at the same time or was that how you programmed it ?

That was just how I programmed it to test. I tried adding the other motor in after and got both working.

Now that you know both motors are working you could do something like this to test the bumper switches:

/* Bumpers */
const int LEFT_BUMP = A2, RIGHT_BUMP = A3;
boolean bump(int pin) {
	// there's fancier ways but for now you will understand this ...
	if (digitalRead(pin) == LOW)    // switches are wired to ground
		return true;
	else
		return false;
}

and in loop do something like

// test the bumper switches
void loop() {
	if (bump(LEFT_BUMP)){
		leftMotor(FORWARD, FULLSPEED);  // left motor on
		rightMotor(FORWARD, 0);         // right off
	}
	else if (bump( RIGHT_BUMP)){
		// right motor on, left off
		...
	}
}

Remember to set INPUT_PULLUP in setup()...

Cheers,
John

Okay guys here is my final sketch :slight_smile: I have a video of it working too, but it is still uploading. I will link to it later when it is done. I need to head to work soon.

EDIT: here is a link to the video on my Skydrive. http://sdrv.ms/VQMzDs
and on youtube My Robo Mower - YouTube

/*
   Mowbot Sketch 
 */
const int RIGHT_MOTOR=13, RIGHT_BRAKE=8, RIGHT_SPINNER=11;
const int LEFT_MOTOR=12, LEFT_BRAKE=9, LEFT_SPINNER=3;
const int FORWARD=HIGH, REVERSE=LOW;
const int FULLSPEED=255;

void leftMotor( int direction, int speed)
{
  digitalWrite( LEFT_MOTOR, direction); //Establishes direction of Channel A
  digitalWrite( LEFT_BRAKE , LOW);      //Disengage the Brake
  digitalWrite( LEFT_SPINNER, speed);   //Spins the motor
}
void rightMotor( int direction, int speed)
{
  digitalWrite( RIGHT_MOTOR, direction); //Establishes direction of Channel B
  digitalWrite( RIGHT_BRAKE , LOW);      //Disengage the Brake
  digitalWrite( RIGHT_SPINNER, speed);   //Spins the motor
}
/* Bumpers */
const int LEFT_BUMP = A2;
const int RIGHT_BUMP = A3;

boolean bump(int pin) {
  // there's fancier ways but for now ...
  if (digitalRead(pin) == LOW)
    return true;
  else
    return false;
}

void setup() 
{ 
  pinMode(LEFT_MOTOR, OUTPUT);   
  pinMode(LEFT_BRAKE, OUTPUT);
  pinMode(RIGHT_MOTOR, OUTPUT);
  pinMode(RIGHT_BRAKE, OUTPUT);
  pinMode(LEFT_BUMP, INPUT_PULLUP);
  pinMode(RIGHT_BUMP, INPUT_PULLUP);

}
void loop()
{
  int randNumber = random(900, 2500);

  if (bump(LEFT_BUMP))            // left bumper hit
  {
    leftMotor(REVERSE, FULLSPEED);  // backup
    rightMotor(REVERSE, FULLSPEED);
    delay(1500);
    leftMotor(REVERSE, FULLSPEED);  // turn
    rightMotor(FORWARD, FULLSPEED);
    delay(randNumber);       // a random amount
  }
  else if (bump(RIGHT_BUMP))
  {
    leftMotor(REVERSE, FULLSPEED);
    rightMotor(REVERSE, FULLSPEED);
    delay(1500);
    rightMotor(REVERSE, FULLSPEED);
    leftMotor(FORWARD, FULLSPEED);
    delay(randNumber);
  }
  else
  {
    leftMotor(FORWARD, FULLSPEED);
    rightMotor(FORWARD, FULLSPEED);
  }
}  // then back to loop(), over and over

Congratulations on getting it working. Your code is fairly easy to read but I would suggest making each of the sets of motor controls into a function, each with a meaningful name. Some of the functions, such as the ones for forward and reverse would be called more than once and using functions would make it easier to understand the flow of the code. A function to make the mower spin in a random direction could also help to mix up the movement a little too.

Another point, it is not very kind to the motors to go from full speed forward to full speed backwards without a period of time when they are stopped. It would be even kinder to the motors to bring them up to full speed over a period of time rather than instantaneously, but that can be added to your present code. You could do this inside the functions that I have suggested using as there would then only be one place to implement the changes.

Have you tried the mower on grass yet ? The motor driving the blade looks too small to deliver much power, but maybe not.

as it is winter, there is no grass to cut sadly. But once the snow melts and it warms up a bit I will try it out on the grass. The blade I made is pretty lite and it spins very quickly on the motor, we'll see what happens when there is a little resistance with grass. As for your other points, they make sense, but I don't really know anything about coding to be able to properly implement them. It is something I can work on in the future, but for now I Am just glad I got it working.

You are right to be pleased that it works but I am fairly sure that it won't cut the grass, but we shall see.

As to my suggestion to use functions, here is a modified portion of your code that illustrates the idea

  else if (bump(RIGHT_BUMP))
  {
    backup(FULLSPEED);
    delay(1500);
    spinRight(FULLSPEED);
    delay(randNumber);
  }
  else
  {
    forward(FULLSPEED);
  }
}  // then back to loop(), over and over

void forward(int speed)
{
  leftMotor(FORWARD, speed);
  rightMotor(FORWARD, speed);
}

void backup(int speed)
{
  leftMotor(REVERSE, speed);
  rightMotor(REVERSE, speed);
}

void spinRight(int speed)
{
  rightMotor(REVERSE, speed);
  leftMotor(FORWARD, speed);
}

To my mind it is easier to understand what is going and make the loop() function less cluttered. You could take the idea even further and combine these lines

    backup(FULLSPEED);
    delay(1500);
    spinRight(FULLSPEED);
    delay(randNumber);

into a function called, perhaps avoidRight(), and call it when you want to back up and spin right. How far you take it is up to you.
Creating functions like these also allows you to copy them to other programs without the need to write the code again.

What next ?
A robot vacuum cleaner perhaps. Yes, I do know that they are already available !

I Can give that a try at some point, as for the robot vacuum, I already own a roomba 550 and an older 400 series. The 400 series has non-functional brush motor, but other than that it works, was thinking of converting it into something.