Pages: 1 [2] 3   Go Down
Author Topic: Question about my Sketch.  (Read 1204 times)
0 Members and 1 Guest are viewing this topic.
Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 354
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

An input pin when nothing is connected (or an open switch in your case) is indeterminate, neither low nor high (i.e. bouncing in between).  A pull-up resistor holds it gently but distinctly high, but allows a switch to ground to easily pull it distinctly low.  The INPUT_PULLUP enables a built-in pullup resistor.

A pull-down resistor addresses the same problem in the opposite way, but there is no built-in (you would have to provide externally)....

Cheers,
John
Logged

NJ, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

so my next question would be, instead of A2 and A3, should I use the current sensing pins A0 and A1? or would that not make a difference?
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3482
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The first thing that strikes me is that motor controllers usually need 3 inputs to control each motor. 
The first sketch from the OP defines 4 pins (8, 9, 10 and 11) as outputs
Code:
  pinMode(8, OUTPUT);   
  pinMode(9, OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
In the first sketch posted the 4 outputs are used as follows
Code:
  digitalWrite(8,LOW);   // motor A back
  digitalWrite(9, HIGH);   // motor A back
  digitalWrite(10, LOW);    // motor B back
  digitalWrite(11, HIGH);    // motor B back
In the next sketch 4 pins (8, 9, 12, 13) have been defined as outputs
Code:
  pinMode(12, OUTPUT);   
  pinMode(9, OUTPUT);
  pinMode(13,OUTPUT);
  pinMode(8,OUTPUT);
and used in the sketch
Code:
    digitalWrite(12, HIGH); //Establishes forward direction of Channel A
    digitalWrite(9, LOW);   //Disengage the Brake for Channel A
    digitalWrite(3, 255);   //Spins the motor on Channel A at full speed
    digitalWrite(13, HIGH); //Establishes forward direction of Channel B
    digitalWrite(8, LOW);   //Disengage the Brake for Channel B
    digitalWrite(11, 255);   //Spins the motor on Channel B at full speed
but pins 3 and 11 are not defined as outputs so will default to being inputs so will not behave as outputs

At no time are 6 pins (3 per motor) defined as outputs and used in the sketch.

One useful thing to do would be to give the pins meaningful names which would make reading the code easier.
I am in agreement with John that the full sketch be put aside for now and that a start should be made with small steps.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

NJ, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The second sketch was merely to show the sketch that I was basing mine off of. It is not actually the one I am using. The guy who did that one used a different shield,
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3482
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The fact remains that neither sketch uses 3 pins defined as outputs to control the shield.

Motor channel A uses pin 12 to control direction, pin 9 for braking and pin 3 to control speed whilst motor channel B uses pin 13 to control direction, pin 8 for braking and pin 11 to control speed.  Pins A0 and A1 are used for current sensing if required, but are not necessary unless current sensing is needed.

There are a number of things wrong with your sketch such as reading an analog value from the bump pins then testing whether the value(s) are HIGH.  This could work but it would be more normal to read the digital value and test it for HIGH/LOW.

In this snippet
Code:
  pinMode(12, OUTPUT);   
  pinMode(9, OUTPUT);
  pinMode(13,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);

}

void loop() {
  val = analogRead(bumpPin1);
  randNumber = random(600, 1500);
  if (val == HIGH) { // if no objects detected on bump sensor right
    digitalWrite(12, HIGH); //Establishes forward direction of Channel A
    digitalWrite(9, LOW);   //Disengage the Brake for Channel A
    digitalWrite(3, 255);   //Spins the motor on Channel A at full speed
    digitalWrite(13, HIGH); //Establishes forward direction of Channel B
    digitalWrite(8, LOW);   //Disengage the Brake for Channel B
    digitalWrite(11, 255);   //Spins the motor on Channel B at full speed
pins 3 and 11 are used as outputs but are not defined as such.
You need to start with simple sketches that run the motor(s) and work from there
Try this
Code:
int motorA_dir = 12;    //direction
int motorA_brake = 9;   //brake
int motorA_speed = 3;   //speed

void setup()
{
  pinMode (motorA_dir, OUTPUT);    //set 3 pins to be outputs
  pinMode (motorA_brake, OUTPUT);
  pinMode (motorA_speed, OUTPUT);

  digitalWrite(motorA_dir,HIGH);  //direction 1
  digitalWrite(motorA_brake,LOW);  //brake off

  for (int motorSpeed = 0;motorSpeed <=255 ;motorSpeed++)
  {
    analogWrite(motorA_speed, motorSpeed);  //set the motor speed
    delay(100);                             //slow down the for loop a little
  }
  analogWrite(motorA_speed, 0);  //let the motor coast to a stop
}

void loop()
{
}
One motor should run up from its slowest speed to its highest then stop.  Note how giving the Arduino pins names helps to make the code more readable.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 354
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

NJ, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I was at work all day and didn't get to tinker. Just got home and finished dinner and a grocery shop will attempt the tinker now. Have to go downstairs and load your sketch to the UNO and hook it up to see.
Logged

NJ, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The fact remains that neither sketch uses 3 pins defined as outputs to control the shield...

...You need to start with simple sketches that run the motor(s) and work from there
Try this
Code:
int motorA_dir = 12;    //direction
int motorA_brake = 9;   //brake
int motorA_speed = 3;   //speed

void setup()
{
  pinMode (motorA_dir, OUTPUT);    //set 3 pins to be outputs
  pinMode (motorA_brake, OUTPUT);
  pinMode (motorA_speed, OUTPUT);

  digitalWrite(motorA_dir,HIGH);  //direction 1
  digitalWrite(motorA_brake,LOW);  //brake off

  for (int motorSpeed = 0;motorSpeed <=255 ;motorSpeed++)
  {
    analogWrite(motorA_speed, motorSpeed);  //set the motor speed
    delay(100);                             //slow down the for loop a little
  }
  analogWrite(motorA_speed, 0);  //let the motor coast to a stop
}

void loop()
{
}
One motor should run up from its slowest speed to its highest then stop.  Note how giving the Arduino pins names helps to make the code more readable.

I see what you are saying. I will give that a go as well, so obviously the sketch you provided is for a single motor, but once I get at least one working then it is a matter of adding the second..
Logged

NJ, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 354
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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
Logged

NJ, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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...

Code:
/*
   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);
}
« Last Edit: January 31, 2013, 09:05:10 pm by OmegaRa » Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 354
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Code:
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
« Last Edit: January 31, 2013, 09:58:17 pm by johncc » Logged

NJ, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4019
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Logged

Examples can be found in your IDE.

NJ, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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