I have been working on a robot for a while. Can someone help me my code?

I built a robot with an arduino and a thunder tumbler rc. Can someone help me with my code? I will post a video on youtube of what it does. I want the tumbler to drive straight and when the bump switch gets hit it will back up and turn left or right(can remember) and then drive straight again. Here is the code. There might be a few stray bits from a ping ultrasonic sensor in there as well. I dont want that but i didnt see anything left.




int switchPin= 2; // Right bump switch on pin 2
int swval;
int ledPin = 13; // LED connected to digital pin 13
int motorpinright = 11; // pin for left motor reverse
int motorpinleft = 9; // pin for left motor forward
int motorpinrevright = 3; // pin for right motor reverse
int motorpinrevleft = 10; // pin for right motor forward


void setup() {
pinMode(switchPin, INPUT); // Sets the digital pin as input
pinMode(ledPin, OUTPUT); // Sets the digital pin as output
pinMode(motorpinright, OUTPUT); // Motor drives-----------
pinMode(motorpinleft, OUTPUT); //------------------------
pinMode(motorpinrevright, OUTPUT); //------------------------
pinMode(motorpinrevleft, OUTPUT); //------------------------
}

void loop() {




delay(100);

analogWrite(motorpinleft, 255); //100% speed
analogWrite(motorpinright, 255); //100% speed0



analogWrite(motorpinleft, 0); //stop left motor
analogWrite(motorpinright, 0); //stop right motor
analogWrite(motorpinrevright, 0); // stop right rev motor
analogWrite(motorpinrevleft, 0); // stop left rev motor
analogWrite(motorpinrevright, 255); //100% speed
analogWrite(motorpinleft, 255); //100% speed
delay(380); //380 milliseconds
analogWrite(motorpinrevright, 0); // off
analogWrite(motorpinleft, 0); // off


digitalWrite(switchPin, HIGH); // Sets the pin to high
swval = digitalRead(switchPin); // Read input value and store it
if (swval == LOW) {
analogWrite(motorpinleft, 0); //stop left motor
analogWrite(motorpinright, 0); //stop right motor
analogWrite(motorpinrevleft, 0); // stop left rev motor
analogWrite(motorpinrevright, 0); // stop right rev motor
analogWrite(motorpinrevleft, 255); //100% speed
analogWrite(motorpinrevright, 255); //100% speed
delay(800); //800 milliseconds
analogWrite(motorpinrevleft, 0); // off
analogWrite(motorpinrevright, 0); // off
analogWrite(motorpinrevright, 255); //100% reverse speed
analogWrite(motorpinleft, 255); //100% forward speed
delay(700); //700 milliseconds
analogWrite(motorpinrevright, 0); // off
analogWrite(motorpinleft, 0); // off
delay(50); //50 milliseconds
}

/*------------------------------------------------
*/


}
 

sort by: active | newest | oldest
frollard6 years ago
I would recommend to simplify your code, make a bunch of voids to control the motors:

void StopRobot() { //all motors off
//do whatever analogwrites it takes to stop the robot.
}

void GoForward() {
//motor commands to go forward
}
void TurnLeft(){ // make the robot turn left
// etc etc
}

void TurnRight(){ //make the robot turn right
//you get the idea
}

void CheckSensors(){
//any sensing of the outside environment in here
swval = digitalRead(switchPin); // Read input value and store it
}

void AvoidObstacle(){
StopRobot();
reverserobot;
delay;
stoprobot;
turn left;
delay;
stoprobot;
GoForward;
}


Then in loop() you want code like:

GoForward;
CheckSensors;
if swval = true then AvoidObstacle;
swval = false;



Compartmentalize your code as much as possible -- that way editing becomes a lot easier.
TOCO (author)  frollard6 years ago
okay, I tried what you said and I get an error. The error says, error expected unqualified-id before 'if' in function 'void GoForward()':
in function 'void AvoidObstacle()':
at global scope:

Here is the code, Thanks for your help sofar


int switchPin= 2; // Right bump switch on pin 2
int swval;
int ledPin = 13; // LED connected to digital pin 13
int motorpinright = 11; // pin for left motor reverse
int motorpinleft = 9; // pin for left motor forward
int motorpinrevright = 3; // pin for right motor reverse
int motorpinrevleft = 10; // pin for right motor forward
int speedval = 140;
if swval = true then AvoidObstacle;
swval = false;


void setup() {
pinMode(switchPin, INPUT); // Sets the digital pin as input
pinMode(ledPin, OUTPUT); // Sets the digital pin as output
pinMode(motorpinright, OUTPUT); // Motor drives-----------
pinMode(motorpinleft, OUTPUT); //------------------------
pinMode(motorpinrevright, OUTPUT); //------------------------
pinMode(motorpinrevleft, OUTPUT); //------------------------

}

void StopRobot() { //all motors off
analogWrite(motorpinright, 0);
analogWrite(motorpinleft, 0);
analogWrite(motorpinrevright, 0);
analogWrite(motorpinrevleft, 0);
}

void GoForward() {
//motor commands to go forward
analogWrite(motorpinrightrev, 0);
analogWrite(motorpinleftrev, 0);
analogWrite(motorpinright, speedval);
analogWrite(motorpinleft, speedval);
}


void CheckSensors(){
//any sensing of the outside environment in here
swval = digitalRead(switchPin); // Read input value and store it
}

void AvoidObstacle(){
analogWrite(motorpinright, 0);
analogWrite(motorpinleft, 0);
analogWrite(motorpinrightrev, 0);
analogWrite(motorpinleftrev, 0)
//reverserobot;
analogWrite(motorpinright, 0);
analogWrite(motorpinleft, 0);
analogWrite(motorpinrightrev, speedval);
analogWrite(motorpinleftrev, speedval);
delay(400);
//stoprobot
analogWrite(motorpinright, 0);
analogWrite(motorpinleft, 0);
analogWrite(motorpinrightrev, 0);
analogWrite(motorpinleftrev, 0)
//turn left
analogWrite(motorpinright, 50);
analogWrite(motorpinleft, 0);
analogWrite(motorpinrightrev, 0);
analogWrite(motorpinleftrev, 50)
delay(400);
//stoprobot
analogWrite(motorpinright, 0);
analogWrite(motorpinleft, 0);
analogWrite(motorpinrightrev, 0);
analogWrite(motorpinleftrev, 0)
//GoForward
analogWrite(motorpinright, speedval);
analogWrite(motorpinleft, speedval);
analogWrite(motorpinrightrev, 0);
analogWrite(motorpinleftrev, 0)
}


}

maewert TOCO6 years ago

Toco,

Please find my annotated code here:

And my offering of new resulting code here:
 

TOCO (author)  maewert6 years ago
Thank you for the help. When I verify it an error comes up saying that the TRUE part wasnt declared as a scope or something.
maewert TOCO6 years ago
Sorry about that, just declare TRUE as an int whose value is -1 and FALSE as an int whose value is 0.
So up top add:
int TRUE = -1;
int FALSE = 0;

Best Wishes
TOCO (author)  maewert6 years ago
When I upload it to the board it seems to work perfectly until the bumpswitch gets hit. Nothing happens it still drives forward. I dont want to bug you about it but if you could i would like help. If not I might be able to bring it into one of my computer programming teachers and ask for help. Here is the code after a few minor changes.

int TRUE = -1;
int FALSE = 0;
int switchPin= 2; // Right bump switch on pin 2
int swval;
int ledPin = 13; // LED connected to digital pin 13
int motorpinright = 3; // pin for left motor reverse
int motorpinleft = 9; // pin for left motor forward
int motorpinrevright = 11; // pin for right motor reverse
int motorpinrevleft = 10; // pin for right motor forward


void setup() {
pinMode(switchPin, INPUT); // Sets the digital pin as input
pinMode(ledPin, OUTPUT); // Sets the digital pin as output
pinMode(motorpinright, OUTPUT); // Motor drives-----------
pinMode(motorpinleft, OUTPUT); //------------------------
pinMode(motorpinrevright, OUTPUT); //------------------------
pinMode(motorpinrevleft, OUTPUT); //------------------------

digitalWrite(switchPin, HIGH); // Sets the pin to high

// Start with all motors OFF.
analogWrite(motorpinleft, 0); //stop left motor
analogWrite(motorpinright, 0); //stop right motor
analogWrite(motorpinrevleft, 0); // stop left rev motor
analogWrite(motorpinrevright, 0); // stop right rev motor

}

void GoForward()
{
analogWrite(motorpinleft, 255); //100% speed
analogWrite(motorpinright, 255); //100% speed0
}

int BumpingIntoSomething()
{
if (digitalRead(switchPin)== LOW)
return TRUE;
else
return FALSE;
}

void ChangeDirections()
{
// Full Stop
analogWrite(motorpinleft, 0); //stop left motor
analogWrite(motorpinright, 0); //stop right motor
analogWrite(motorpinrevleft, 0); // stop left rev motor
analogWrite(motorpinrevright, 0); // stop right rev motor

// Stop Rolling
delay(50); //50 milliseconds

// Now we go Full speed in reverse for 800 milliseconds then turn right:
analogWrite(motorpinrevleft, 255); //100% speed in Reverse
analogWrite(motorpinrevright, 255); //100% speed in Reverse
delay(800); //for 800 milliseconds
analogWrite(motorpinrevleft, 0); //Stop Left Motor
delay(50); //Wait for it to stop rolling for 50 milliseconds
analogWrite(motorpinleft, 255); //Left 100% forward speed so we turn to the right
delay(700); //for 700 milliseconds
analogWrite(motorpinrevright, 0); //now stop the right motor
analogWrite(motorpinleft, 0); //now stop the left motor
delay(50); //50 milliseconds to stop rolling
}

void loop() {

GoForward();
if (BumpingIntoSomething())
{
ChangeDirections();
}
}
maewert TOCO6 years ago

TOCO, 
We love to help out where we can.

Lets make a change to light the LED also when we bump into something.  You might have a problem with the switch that detects the bump:

int BumpingIntoSomething()
{
if (digitalRead(switchPin)== LOW)
{
  digitalWrite(ledpin,HIGH);
  return TRUE;
}
else
{
  digitalWrite(ledpin,LOW);
  return FALSE;
}


With this change you should see the LED change colors when a bump is detected.  At least then you know whether it is the detection of the bump or the response to the bump.

Besy Wishes.

TOCO (author)  maewert6 years ago
Sorry to bug you this long but when I put that in instead of what was there I get an error saying that ledpin was not declared in this scope. I tried making a 2nd variable for the led but that didnt work and I got the same error.
maewert TOCO6 years ago
I think the variables are case sensitive, try 'ledPin' everywhere. also I left off the closing '}', be sure they are balanced.
TOCO (author)  maewert6 years ago
Thanks again for all of the help so far but I get an error when I compile this code.
int TRUE = -1;
int FALSE = 0;
int switchPin= 2; // Right bump switch on pin 2
int swval;
int ledPin = 13; // LED connected to digital pin 13
int motorpinright = 3; // pin for left motor reverse
int motorpinleft = 9; // pin for left motor forward
int motorpinrevright = 11; // pin for right motor reverse
int motorpinrevleft = 10; // pin for right motor forward


void setup() {
pinMode(switchPin, INPUT); // Sets the digital pin as input
pinMode(ledPin, OUTPUT); // Sets the digital pin as output
pinMode(motorpinright, OUTPUT); // Motor drives-----------
pinMode(motorpinleft, OUTPUT); //------------------------
pinMode(motorpinrevright, OUTPUT); //------------------------
pinMode(motorpinrevleft, OUTPUT); //------------------------

digitalWrite(switchPin, HIGH); // Sets the pin to high

// Start with all motors OFF.
analogWrite(motorpinleft, 0); //stop left motor
analogWrite(motorpinright, 0); //stop right motor
analogWrite(motorpinrevleft, 0); // stop left rev motor
analogWrite(motorpinrevright, 0); // stop right rev motor

}

void GoForward()
{
analogWrite(motorpinleft, 255); //100% speed
analogWrite(motorpinright, 255); //100% speed0
}


int BumpingIntoSomething()
{
if (digitalRead(switchPin)== LOW)
{
digitalWrite(ledPin,HIGH);
return TRUE;
}
else
{
digitalWrite(ledPin,LOW);
return FALSE;
}
}
ardinoerror.bmp
TOCO (author)  TOCO6 years ago
That was the biggest that I could get the picture but if you click on it the picture will get much bigger.
maewert TOCO6 years ago
Did you miss this code:
void ChangeDirections()
{
// Full Stop
analogWrite(motorpinleft, 0); //stop left motor
analogWrite(motorpinright, 0); //stop right motor
analogWrite(motorpinrevleft, 0); // stop left rev motor
analogWrite(motorpinrevright, 0); // stop right rev motor

// Stop Rolling
delay(50); //50 milliseconds

// Now we go Full speed in reverse for 800 milliseconds then turn right:
analogWrite(motorpinrevleft, 255); //100% speed in Reverse
analogWrite(motorpinrevright, 255); //100% speed in Reverse
delay(800); //for 800 milliseconds
analogWrite(motorpinrevleft, 0); //Stop Left Motor
delay(50); //Wait for it to stop rolling for 50 milliseconds
analogWrite(motorpinleft, 255); //Left 100% forward speed so we turn to the right
delay(700); //for 700 milliseconds
analogWrite(motorpinrevright, 0); //now stop the right motor
analogWrite(motorpinleft, 0); //now stop the left motor
delay(50); //50 milliseconds to stop rolling
}

void loop() {

GoForward();
if (BumpingIntoSomething())
{
ChangeDirections();
}
}


TOCO (author)  maewert6 years ago
I guess I did. I will try it out with that in the code. Thanks.
maewert TOCO6 years ago
Add one more '}' before the void ChangeDirections like so:
...
else
{
digitalWrite(ledPin,LOW);
return FALSE;
}
} // second one added here!
void ChangeDirections();
{
// Full Stop
...
TOCO (author)  maewert6 years ago
Thank you so much! It is finality working the way that I want it to. The backing up and turning part might need some adjustment but thats easy. Thank you again for helping me for this long. I have never had anyone do that for me on this site.
maewert TOCO6 years ago
I am here to serve :-)

Post a video of it moving!  That would be cool.

Best Wishes
TOCO (author)  maewert6 years ago
Hey, would you have any ideas for a new bump switch. I am using the thunder tumber rc. If you search thunder tumber on this site you get 2 results for instrucables. I followed the one that uses an arduino not the other one. I need something that could be bumped from just about any angle that this car can hit something at. It needs to be in front of the wheels. If you cant i am sure that I can figure something out at radio shack or my school computer club.
maewert TOCO6 years ago
TOCO.  If you used Two switches then you could change the code so that it turns in the opposite direction as the switch that bumped.  The code you have turns only one way.

They sell microswitches that have a little spring inside that keeps the switch open unless it is pressed.  If you used two of these and hot glued a bumper to the switch points then it might hold up.  If you hit things hard then the glue might break away.

Here is a simple sketch:
Simple  bumper.JPG
TOCO (author)  maewert6 years ago
The problem is the glue on everything else that I have made as a bumper has broken off easily.I might be able to get a couple of the same buttons and use those. I have one sort of telegraph looking switch that has a metal thing that pushes in the switch. Maybe i could get one of those and make a metal bumper like the one in your sketch and possibly solder it to the switches. I dont know if solder would hold or not.
TOCO (author)  maewert6 years ago
I will post a video. I need to get a new bump switch or use 2. The car can go in at an angle and the switch wont get hit. (the switch is just a button).
TOCO (author)  maewert6 years ago
Okay
TOCO (author)  maewert6 years ago
okay, I added that back in and after I fixed all of the other errors from putting it in the wrong place I still get the same error in the picture I posted. Here is the code.


int TRUE = -1;
int FALSE = 0;
int switchPin= 2; // Right bump switch on pin 2
int swval;
int ledPin = 13; // LED connected to digital pin 13
int motorpinright = 3; // pin for left motor reverse
int motorpinleft = 9; // pin for left motor forward
int motorpinrevright = 11; // pin for right motor reverse
int motorpinrevleft = 10; // pin for right motor forward


void setup() {
pinMode(switchPin, INPUT); // Sets the digital pin as input
pinMode(ledPin, OUTPUT); // Sets the digital pin as output
pinMode(motorpinright, OUTPUT); // Motor drives-----------
pinMode(motorpinleft, OUTPUT); //------------------------
pinMode(motorpinrevright, OUTPUT); //------------------------
pinMode(motorpinrevleft, OUTPUT); //------------------------

digitalWrite(switchPin, HIGH); // Sets the pin to high

// Start with all motors OFF.
analogWrite(motorpinleft, 0); //stop left motor
analogWrite(motorpinright, 0); //stop right motor
analogWrite(motorpinrevleft, 0); // stop left rev motor
analogWrite(motorpinrevright, 0); // stop right rev motor

}

void GoForward()
{
analogWrite(motorpinleft, 255); //100% speed
analogWrite(motorpinright, 255); //100% speed0
}


int BumpingIntoSomething()
{
if (digitalRead(switchPin)== LOW)
{
digitalWrite(ledPin,HIGH);
return TRUE;
}
else
{
digitalWrite(ledPin,LOW);
return FALSE;
}
void ChangeDirections();
{
// Full Stop

analogWrite(motorpinleft, 0); //stop left motor
analogWrite(motorpinright, 0); //stop right motor
analogWrite(motorpinrevleft, 0); // stop left rev motor
analogWrite(motorpinrevright, 0); // stop right rev motor

// Stop Rolling
delay(50); //50 milliseconds

// Now we go Full speed in reverse for 800 milliseconds then turn right:
analogWrite(motorpinrevleft, 255); //100% speed in Reverse
analogWrite(motorpinrevright, 255); //100% speed in Reverse
delay(800); //for 800 milliseconds
analogWrite(motorpinrevleft, 0); //Stop Left Motor
delay(50); //Wait for it to stop rolling for 50 milliseconds
analogWrite(motorpinleft, 255); //Left 100% forward speed so we turn to the right
delay(700); //for 700 milliseconds
analogWrite(motorpinrevright, 0); //now stop the right motor
analogWrite(motorpinleft, 0); //now stop the left motor
delay(50); //50 milliseconds to stop rolling
}

void loop();
{


GoForward();
if (BumpingIntoSomething())
{
ChangeDirections();
}
}
}
TOCO (author)  maewert6 years ago
Thanks for the help. I will change it and see if it works.
TOCO (author)  maewert6 years ago
I looked and i couldnt find anyting unless it is a { that is causing the problem. I dont really know much about it.
frollard6 years ago
only thing I would change is use the calls you already made to do the 'changedirections' function -- instead of hard coding the motor pin instructions, use the 'stop, reverse, turn, stop' functions you already made.

Hope you get it going! -- and make sure to document document document so it goes into an ible!