Let's Make Robots!

A Bit Confused

What am I missing?  I attempted to start using routines in my programming but I am obviously not quite there.  I have 2 programs that from what I understand do the same thing but the one using routines doesnt do anything. 

First program:

int motorSpeed = 200;  // motor speed

// Motor Pins

int motor1_Pin0 = 11;
int motor1_Pin1 = 6;

int motor2_Pin0 = 5;
int motor2_Pin1 = 3;
// Switch Pin
int bump1_Pin = 2;
int bump2_Pin = 7;
int leftval = 0;
int rightval = 0;

// - - - - - - - - - - - - - - - - - - -   SETUP
void setup() {
 
  // set Arduino pins as outputs
  pinMode(motor1_Pin0, OUTPUT);
  pinMode(motor1_Pin1, OUTPUT);
 
  pinMode(motor2_Pin0, OUTPUT);
  pinMode(motor2_Pin1, OUTPUT);
 
  pinMode(bump1_Pin,INPUT);   //Bumper switch pins as Inputs
  pinMode(bump2_Pin, INPUT);
 
  delay(500);
  
}

// - - - - - - - - - - - - - - - - - - - -  LOOP
void loop() {
  leftval = digitalRead(bump1_Pin);
  rightval = digitalRead(bump2_Pin);
 
  if (leftval == rightval){
     
  analogWrite(motor1_Pin0, motorSpeed);   //All Forward
  digitalWrite(motor1_Pin1, LOW);
     
  analogWrite(motor2_Pin0, motorSpeed);
  digitalWrite(motor2_Pin1, LOW);           //All Forwards end
 
  }else if (leftval > rightval) {
 
  digitalWrite(motor1_Pin0, LOW);          //All Backwards
  analogWrite(motor1_Pin1, motorSpeed);
     
  digitalWrite(motor2_Pin0, LOW);
  analogWrite(motor2_Pin1, motorSpeed);   //All  Backwards  end
 
  delay(2000);
 
  digitalWrite(motor1_Pin0, motorSpeed);   //Turn
  analogWrite(motor1_Pin1, LOW);
 
  digitalWrite(motor2_Pin0, LOW);
  analogWrite(motor2_Pin1, motorSpeed);   //End Turn
 
  delay(1000);
 
 
  }else if (rightval > leftval){
   
   digitalWrite(motor1_Pin0, LOW);    //Backwards
   analogWrite(motor1_Pin1, motorSpeed);
 
   digitalWrite(motor2_Pin0, LOW);
   analogWrite(motor2_Pin1, motorSpeed);
  
   delay(2000);
  
   digitalWrite(motor1_Pin0, LOW);    //Turn other way
   analogWrite(motor1_Pin1, motorSpeed);
  
   digitalWrite(motor2_Pin0, motorSpeed);
   analogWrite(motor2_Pin1, LOW);
  
   delay(1000);
}
 
}

So basically all I did was use the above code except after my void Setup() I attempted to create routines such as

AllForward() {

analogWrite(motor1_Pin0, motorSpeed);   //All Forward
  digitalWrite(motor1_Pin1, LOW);
     
  analogWrite(motor2_Pin0, motorSpeed);
  digitalWrite(motor2_Pin1, LOW);           //All Forward end

}

and use that in my code i.e.

 if(leftval==rightval){

AllForward()

}

I uploaded and my bot didnt move at all...What am I missing?
 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
I got the program working as written above...the problem was the batteries.  = / 
LOLGeek's picture

Always check phsyical connections (including bad or drained batteries) first. If you know the code should work then it is most likely a phsyical issue. :D

http://en.wikipedia.org/wiki/OSI_model

It was originally meant for network protocols like TCP/IP and the like but it also works for troubleshooting. :D

ezekiel181's picture

Try uploading the simplest sketch you can make that only turns on the motors to check it isn`t your wiring causing the problem.

I see you are comparing the switches to each other. If while in a delay where your bot is moving that both switches get triggered, it will make an endless loop of the AllForward function as it pushes against the wall. Of course it`s not a problem until the bot actually starts moving :)

TheCowGod's picture

Hmm, that program looks fine at first glance. Do you know how to use the Serial.println() functions to print text back to the computer? Use that and sprinkle some Serial.println()'s in each of your functions (like Serial.println("Running AllForward()..."); ) so you can follow the code that is actually being executed. That will help to figure out what's going on.

Dan

Mike's picture

In most languages there's some phrase that alerts the copiler to the function definition - this diferentiates it from a call to the function. Quite often it is the word "function". In wahtever language you are using it may be the type of the returned value - eg the "void"in front of the "loop() {". Summary, I guess you need someting, perhaps "void" in front of the first "AllForward() {".

Mike

You're correct and in my actual program I included it.  My program that doesnt work is below:

//Variables

int motorSpeed = 200;  //Motor Speed up to 200

//Motor Pins
int motor1_Pin0 = 11; 
int motor1_Pin1 = 6;

int motor2_Pin0 = 5;
int motor2_Pin1 = 3;
// Bump Switch Pins
int bump1_Pin = 2;  
int bump2_Pin = 7;
//Where to Store Switch Variables
int leftval = 0;
int rightval = 0;

//Setup
void setup() {
  //Motor Pins as Outputs
  pinMode(motor1_Pin0, OUTPUT);
  pinMode(motor1_Pin1, OUTPUT);
 
  pinMode(motor2_Pin0, OUTPUT);
  pinMode(motor2_Pin1, OUTPUT);
 
  //Bumper switch pins as Inputs
  pinMode(bump1_Pin,INPUT);  
  pinMode(bump2_Pin, INPUT);
 
  delay(500);
}

void AllForward(){
  analogWrite(motor1_Pin0, motorSpeed);   //All Motor Forward
  digitalWrite(motor1_Pin1, LOW);
  analogWrite(motor2_Pin0, motorSpeed);
  digitalWrite(motor2_Pin1, LOW);  
}

void AllReverseTurnLeft(){
  digitalWrite(motor1_Pin0, LOW);          //All Backwards
  analogWrite(motor1_Pin1, motorSpeed);
     
  digitalWrite(motor2_Pin0, LOW);
  analogWrite(motor2_Pin1, motorSpeed);   //All  Backwards  end
 
  delay(1000);
 
  digitalWrite(motor1_Pin0, motorSpeed);   //Turn
  analogWrite(motor1_Pin1, LOW);
 
  digitalWrite(motor2_Pin0, LOW);
  analogWrite(motor2_Pin1, motorSpeed);   //End Turn
 
  delay(1000);
}

void AllReverseTurnRight(){
  digitalWrite(motor1_Pin0, LOW);    //Backwards
   analogWrite(motor1_Pin1, motorSpeed);
 
   digitalWrite(motor2_Pin0, LOW);
   analogWrite(motor2_Pin1, motorSpeed);
  
   delay(1000);
  
   digitalWrite(motor1_Pin0, LOW);    //Turn other way
   analogWrite(motor1_Pin1, motorSpeed);
  
   digitalWrite(motor2_Pin0, motorSpeed);
   analogWrite(motor2_Pin1, LOW);
  
   delay(1000);
}

void loop(){
  leftval = digitalRead(bump1_Pin);
  rightval = digitalRead(bump2_Pin);
 
  if (leftval == rightval){
    AllForward();
   
  }else if(leftval > rightval){
    AllReverseTurnRight();
   
  }else if(rightval > leftval){
    AllReverseTurnLeft(); 
  }

Addy's picture

Where's the main function?

 

You can't forget int main()