Pages: [1] 2 3   Go Down
Author Topic: Question about my Sketch.  (Read 1797 times)
0 Members and 1 Guest are viewing this topic.
NJ, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I am making a Robot Lawnmower and it is very basic. I am using an UNO with the arduino Motor Shield. This is my first attempt at a sketch and using someone elses sketch as a guide (he used a different motor shield) along with a motor shield tutorial I found online, I came up with this.

Code:
/*
Mowbot software version 2f with random number turning
 
 
 */int bumpPin1 = 2;
int bumpPin2 = 3;
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);

}

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
    analogWrite(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
    analogWrite(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
    analogWrite(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
    analogWrite(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
    analogWrite(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
    analogWrite(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
    analogWrite(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
    analogWrite(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);
    analogWrite(11, 123);    //Spins the motor on Channel B at half speed
    delay(randNumber);
  }
}


Basically all I need it to do is go forward at full speed and if it bumps into something, back up, randomly turn and go forward again. I am using two micro switches for the bumpers. I have a 12VDC acid battery from an APC powering the Motor shield/motors, and a rechargable 9.6V (9v sized) battery powering the board. The Vin Connect pin I removed per suggestion from the Main site. The wiring goes from the battery, splits off to the blade motor and goes to the microswitches, then out of the switch to the motor shield, the other terminal on the micro switch will go to A2 and A3 but I haven't tried them connected yet. The ground connects to the ground for the blade motor, which connects to the battery.

Does any of this seem wrong? Any help would be appreciated.
« Last Edit: January 30, 2013, 07:52:18 pm by OmegaRa » Logged

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

If I were you I'd start with the two microswitches.
Get them working.... with very simple sketch ... play with it
Code:
if ( bump( LEFT_SWITCH ) )
   Serial.println("Left Bump");
if ( bump( RIGHT_SWITCH ) )
   Serial.println("Right Bump");
Then add 1 motor
Get it working, forward and reverse.  Very simple sketch... play with it
Code:
motorSpeed( LEFT_MOTOR, FORWARD, FULL);
...
motorSpeed( RIGHT_MOTOR, REVERSE, HALF);
Then the other motor

Then the "master control program"  smiley
Logged

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

I am going to have to watch some more tutorials. I was just trying to use the sketch someone else gave me

Code:
/*
Mowbot software version 2f with random number turning

 
 */ int infPin1 = 12;
    int infPin2 = 7;
    int bumpPin1 = 6;
    int bumpPin2 = 5;
    int val;
    int randNumber;

void setup() {               
  // initialize the digital pins as an outputs.
  // Pins to control motor shield
 
  pinMode(8, OUTPUT);   
  pinMode(9, OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(infPin1, INPUT);
  pinMode(infPin2, INPUT);
 
}

void loop() {
 val = digitalRead(infPin1);
  randNumber = random(600, 1500); // random number between 800 and 1500
  if (val == HIGH) { // if no objects detected on left sensor
  digitalWrite(8, HIGH);   // motor A forward
  digitalWrite(9, LOW);    // motor A forward
  digitalWrite(10, HIGH);    // motor B forward
  digitalWrite(11, LOW);    // motor B forward
  }
  else {   // if objects detected on left sensor
  delay(50); // wait for motor stop
  digitalWrite(8,LOW);   // motor A back
  digitalWrite(9, HIGH);   // motor A back
  digitalWrite(10, LOW);    // motor B back
  digitalWrite(11, HIGH);    // motor B back
  delay(1500);
  digitalWrite(8,HIGH);  // Keep motor AB running to turn
  digitalWrite(9,LOW);
  delay(randNumber); //generate random number for turn
  }
   val = digitalRead(infPin2);
   randNumber = random(600, 1500);
  if (val == HIGH) { // if no objects detected on right sensor
  digitalWrite(8, HIGH);   // motor A forward
  digitalWrite(9, LOW);    // motor A forward
  digitalWrite(10, HIGH);    // motor B forward
  digitalWrite(11, LOW);    // motor B forward
  }
  else {  // if objects detected on right sensor
  delay(50); // wait for motor stop
  digitalWrite(8,LOW);     // motor A back
  digitalWrite(9, HIGH);   // motor A back
  digitalWrite(10, LOW);    // motor B back
  digitalWrite(11, HIGH);    // motor B back
  delay(1500);
  digitalWrite(10, HIGH);// keep motor B running to turn
  digitalWrite(11,LOW);
  delay(randNumber);
  } 
  val = digitalRead(bumpPin1);
  randNumber = random(600, 1500);
  if (val == HIGH) { // if no objects detected on bump sensor right
  digitalWrite(8, HIGH);   // motor a forward
  digitalWrite(9, LOW);    // motor a forward
  digitalWrite(10, HIGH);    // motor b forward
  digitalWrite(11, LOW);    // motor b forward
  }
  else {  // if objects detected on right sensor
  digitalWrite(8,LOW);     // motor a back
  digitalWrite(9, HIGH);   // motor a back
  digitalWrite(10, LOW);    // motor b back
  digitalWrite(11, HIGH);    // motor b back
  delay(1500);
  digitalWrite(10, HIGH);// keep motor b running to turn
  digitalWrite(11,LOW);
  delay(randNumber);
}  val = digitalRead(bumpPin2);
randNumber = random(600, 1500);
  if (val == HIGH) { // if no objects detected on bump sensor left
  digitalWrite(8, HIGH);   // motor a forward
  digitalWrite(9, LOW);    // motor a forward
  digitalWrite(10, HIGH);    // motor b forward
  digitalWrite(11, LOW);    // motor b forward
  }
  else {  // if objects detected on left sensor
  digitalWrite(8,LOW);     // motor a back
  digitalWrite(9, HIGH);   // motor a back
  digitalWrite(10, LOW);    // motor b back
  digitalWrite(11, HIGH);    // motor b back
  delay(1500);
  digitalWrite(10, HIGH);// keep motor b running to turn
  digitalWrite(11,LOW);
  delay(randNumber); }
}

Which worked for him ( I saw a video of it working) but as I didn't have IR sensors I removed that part, and because he used a different motor shield the pins were different. So back to the drawing board, time to learn some coding...
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 362
Posts: 17307
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So back to the drawing board, time to learn some coding...

Arduinos are not the best at plug-and-play, too much variation in board types and shields. They are much better when one bites the bullet and learns the programming language and make your own great sketches and projects.

Lefty
Logged

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

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.

Code:
/*
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);
  }
}

Logged

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

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

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

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

Cheers,
John

Logged

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

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

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

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

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.
« Last Edit: January 30, 2013, 08:52:26 pm by OmegaRa » Logged

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

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
Logged

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

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

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

So...

If it's just "nothing happens", we're back to what I said, test some pieces
Code:
/*
   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);
}


« Last Edit: January 30, 2013, 10:23: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 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 smiley
Logged

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

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

Code:
pinMode(A2, INPUT_PULLUP);
pinMode(A3, INPUT_PULLUP);
John

   
Logged

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

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

Code:
pinMode(A2, INPUT_PULLUP);
pinMode(A3, INPUT_PULLUP);
John

   

I will do that smiley just for informational purposes what does INPUT_PULLUP do?
Logged

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