Question about my Sketch.

yeah, I was just hoping, since it was a simple program, that I would be able to quickly get it going without having to get too heavily into learning C++, since right now I am also studying for my CCNA and trying to find a new job as a net admin. Learning a programming language at this point in time doesn't seem conducive. Just out of curiosity though, could you guys point me in the general direction of where I went wrong? I made one or two modifications that I think might help but I don't really know. This is just from looking at a couple on the in IDE and on the site examples.

/*
Mowbot software version 2f with random number turning
 
 
 */int bumpPin1 = A2;
int bumpPin2 = A3;
int val;
int randNumber;

void setup() {                
  // initialize the digital pins as an outputs.
  // Pins to control motor shield

  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
  }
  else {  // if objects detected on right sensor
    digitalWrite(12, LOW);  //Establishes backward direction of Channel B
    digitalWrite(9, LOW);   //Disengage the Brake for Channel B
    digitalWrite(3, 123);    //Spins the motor on Channel B at half speed
    digitalWrite(13, LOW);  //Establishes backward direction of Channel B
    digitalWrite(8, LOW);   //Disengage the Brake for Channel B
    digitalWrite(11, 123);    //Spins the motor on Channel B at half speed
    delay(1500);
    digitalWrite(13, LOW);// keep motor b running to turn
    digitalWrite(8, LOW);
    delay(randNumber);
    analogWrite(11, 123);    //Spins the motor on Channel B at half speed
  }  
  val = analogRead(bumpPin2);
  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
  }
  else {  // if objects detected on right sensor
    digitalWrite(12, LOW);  //Establishes backward direction of Channel B
    digitalWrite(9, LOW);   //Disengage the Brake for Channel B
    digitalWrite(3, 123);    //Spins the motor on Channel B at half speed
    digitalWrite(13, LOW);  //Establishes backward direction of Channel B
    digitalWrite(8, LOW);   //Disengage the Brake for Channel B
    digitalWrite(11, 123);    //Spins the motor on Channel B at half speed
    delay(1500);
    digitalWrite(13, LOW);// keep motor b running to turn
    digitalWrite(8, LOW);
    digitalWrite(11, 123);    //Spins the motor on Channel B at half speed
    delay(randNumber); 
  }
}
Just out of curiosity though, could you guys point me in the general direction of where I went wrong?

Oh I didn't realize you have already hooked it all up? What is it doing wrong?

I can't simulate all that code in my head, the way it's written...

You mention providing power to a blade, are you really planning that? (or simulated)

I don't see anything in the program that suggests turning on and off the blade mower.

(Anyway, I'd recommend simulated :slight_smile: )

Cheers,
John

it's not doing anything, the blade motor starts to spin (since it just gets direct power off the battery). The lights come on on the board and shield, but it doesn't move.

johncc:
You mention providing power to a blade, are you really planning that? (or simulated)

I don't see anything in the program that suggests turning on and off the blade mower.

(Anyway, I'd recommend simulated :slight_smile: )

Cheers,
John

the blade motor is not controlled by the board, it just comes on when I flip the switch.

edit: I actually first tried this bot with a homemade circuit using relays, I was able to get it to go forward, bump, back up, but I couldn't get it to turn (could figure out the caps). So I wanted to switch it to an Arduino.

OmegaRa:
it's not doing anything, the blade motor starts to spin (since it just gets direct power off the battery). The lights come on on the board and shield, but it doesn't move.

Can you check the voltages on the pins e.g. 12, 9, and 3?

I think the writes to pins 3 and 11 should be analogWrites. There may be other issues with that code... hard to tell

they were analog writes when I tried, nothing. So I tried switching it to digital, haven't actually tried it since changing the code. Last time I tried it it was the code from my first post.

So...

If it's just "nothing happens", we're back to what I said, test some pieces

/*
   Mowbot Test Sketch 1 -- Motor
*/
   
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
    digitalWrite( LEFT_BRAKE , LOW);      //Disengage the Brake
    digitalWrite( LEFT_SPINNER, speed);   //Spins the motor
}

void setup() 
{ 
    pinMode(LEFT_MOTOR, OUTPUT);   
    pinMode(LEFT_BRAKE, OUTPUT);
}

void loop() 
{
    leftMotor( FORWARD, FULLSPEED);
    delay( 4000); 	   // on and off every 4 seconds
    leftMotor( FORWARD, 0 );
    delay( 4000);
}

Okay, I copied that sketch, but it is getting late and I need to call it quits for the night. I will test it out tomorrow and report back. Thanks for all the help so far :slight_smile:

Ok. We will get to the switches later, but if you continue with your code you will probably need to change your setup() to:

pinMode(A2, INPUT_PULLUP);
pinMode(A3, INPUT_PULLUP);

John

johncc:
Ok. We will get to the switches later, but if you continue with your code you will probably need to change your setup() to:

pinMode(A2, INPUT_PULLUP);

pinMode(A3, INPUT_PULLUP);



John

I will do that :slight_smile: just for informational purposes what does INPUT_PULLUP do?

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

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?

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

  pinMode(8, OUTPUT);   
  pinMode(9, OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);

In the first sketch posted the 4 outputs are used as follows

  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

  pinMode(12, OUTPUT);   
  pinMode(9, OUTPUT);
  pinMode(13,OUTPUT);
  pinMode(8,OUTPUT);

and used in the sketch

    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.

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,

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

  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

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.

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

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

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.

UKHeliBob:
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

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

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: