Trying to make multiple loops.

Hey, I am making a line follower and it needs to hit a box at the end of the line, drop a ball then turn around. I tried to make it so it goes straight to a loop with the line following at the beginning and then when the bumper is triggered to switch to a loop to turn it around and drop the ball. Instead it what it does is skip straight to the loop to turn around and it isn't even triggering the ball motor just completely skips that part of the code.

int leftmotor = 10; //Sets pin 10 to control Left Motors.
int rightmotor = 11; //Sets ping 11 to control Right Motor
int leftline = 5; //Left line follower input.
int middleline = 4; //Middle line follower input.
int rightline = 3; //Right line follower input.
int bumper = 7; //Bumber swithch input
int ball = 9; // ball motor output.
//add pins for for ping pong ball device.

void setup() {
  pinMode(leftmotor, OUTPUT);
  pinMode(rightmotor, OUTPUT);
  pinMode(leftline, INPUT); //Sets pin 5 as input.
  pinMode(middleline, INPUT); //Sets pin 4 as input.
  pinMode(rightline, INPUT); //Sets pin 3 as input.
  pinMode(bumper, INPUT_PULLUP); //Sets pin 7 as input.
  pinMode(ball, OUTPUT); //set ball motor as an output.
}

void loop() {
  follow();  
}

void follow(){
  // Main runtime instruction set.
  // Read sensor input.
  delay(2000);
  if (digitalRead(leftline) == HIGH) {
    analogWrite(leftmotor, 45); // Stops left motor.
    analogWrite(rightmotor, 235); //Starts right motor
    delay(1);
  }
  else {
    //if no input.
  }
  if (digitalRead(rightline) == HIGH) {
    analogWrite(leftmotor, 235); // Runs left motor.
    analogWrite(rightmotor, 45); // Stops left motor.
    delay(1);
  }
  else {
    // if no input.
  }
  if (digitalRead(middleline) == HIGH) {
    analogWrite(leftmotor, 235); //Runs left motor
    analogWrite(rightmotor, 235); //Runs right motor.
    delay(1);
  }
  else {
    //Do nothing.
  }
  if (digitalRead(bumper) == HIGH) {
    turn();
  }
  else {
   follow();
  }
}

 void turn(){
    delay(2000);
    analogWrite(rightmotor, 45);
    analogWrite(leftmotor, 45);
    delay(1);
    analogWrite(ball, 90);
    analogWrite(rightmotor, 45);
    analogWrite(leftmotor, 45);
    delay(100);
    analogWrite(ball, 250);
    analogWrite(rightmotor, 45);
    analogWrite(leftmotor, 45);
    delay(100);
    analogWrite(ball, 45);
    analogWrite(rightmotor, 45);
    analogWrite(leftmotor, 45);
    delay(1);
    analogWrite(rightmotor, 90);
    analogWrite(leftmotor, 90);
    delay(2000);
    analogWrite(leftmotor, 250);
    analogWrite(rightmotor, 90);
    delay(2000);
    follow();
    
  }

It skips to this part of the code.

 void turn(){
    delay(2000);
    analogWrite(rightmotor, 45);
    analogWrite(leftmotor, 45);
    delay(1);
    analogWrite(ball, 90);
    analogWrite(rightmotor, 45);
    analogWrite(leftmotor, 45);
    delay(100);
    analogWrite(ball, 250);
    analogWrite(rightmotor, 45);
    analogWrite(leftmotor, 45);
    delay(100);
    analogWrite(ball, 45);
    analogWrite(rightmotor, 45);
    analogWrite(leftmotor, 45);
    delay(1);
    analogWrite(rightmotor, 90);
    analogWrite(leftmotor, 90);
    delay(2000);
    analogWrite(leftmotor, 250);
    analogWrite(rightmotor, 90);
    delay(2000);
    follow();
    
  }

But is only executes this portion of that loop.

    analogWrite(rightmotor, 90);
    analogWrite(leftmotor, 90);
    delay(2000);
    analogWrite(leftmotor, 250);
    analogWrite(rightmotor, 90);
    delay(2000);
    follow();
    
  }

All help is greatly appreciated and if you need any more information just let me know. Also my motors use 45 to stop 235 for forward (ball motor is 250 for some reason) and 90 for backward.

How are these wired up? Any external pullups or pulldowns?

  pinMode(leftline, INPUT); //Sets pin 5 as input.
  pinMode(middleline, INPUT); //Sets pin 4 as input.
  pinMode(rightline, INPUT); //Sets pin 3 as input.
  pinMode(bumper, INPUT_PULLUP); //Sets pin 7 as input.

You need a while loop that keeps the robot moving until some useful event happens.

Then, you need a statement (or series of statements) to drop the ball.

Then, you need a while loop that makes the robot turn around.

Then, you need a while loop that makes the robot run until it gets back to the start point.

How are these wired up? Any external pullups or pulldowns?

There are no external pullups or pulldowns and there is a wiring diagram below but let me explain there were some changes since this was made. The transistors are no longer in the circuit and are now replaced with motor controllers that have a power in GND and a PWM input PS I am working on a new one in fritzing that will look better. |500x281

You need a while loop that keeps the robot moving until some useful event happens.

Could you explain and please provide some sample code?

Then, you need a while loop that makes the robot turn around.

Please not that the bumper will remain pressed until the robot backs up enough for it to unpress.

Could you explain and please provide some sample code?

// Do something to go forward

bool bumperSwitchPressed;
while(!bumperSwitchPressed)
{
   bumperSwitchPressed = CheckBumperSwitch();
}

Could you please put it in context of my application for the code this is still a bit grey to me.

Could you please put it in context of my application for the code this is still a bit grey to me.

Apparently not. I used names for functions that you need to write, and comments that describe what you need to do, that I thought made sense in terms of your problem statement.

If you are asking "Will you write the code for me?", the answer is no.

If you are asking "Will you write the code for me?", the answer is no.

I'm not at all asking that but I am new at this system entirely so I have a friend that is experienced so I will ask him. Thanks you have been very helpful.

Also here is an updated version of the schematic. |500x284

dsalveson: Also here is an updated version of the schematic.

The first one was much easier to understand. Go back to that style.

Don't use PP3 type 9v batteries - they cannot produce enough current.

I don't have time now but this

it needs to hit a box at the end of the line, drop a ball then turn around

is crying out for a state-based system.

It starts in the moving-forward state. Then it gets to the touched-box state, then the turning state and the then the facing-away state etct.

...R

is crying out for a state-based system.

It starts in the moving-forward state. Then it gets to the touched-box state, then the turning state and the then the facing-away state etct.

Could you explain this a bit more please.

Also I changed the code to try it and it now it is still doing the exact same thing. Here is my updated code.

int leftmotor = 10; //Sets pin 10 to control Left Motors.
int rightmotor = 11; //Sets ping 11 to control Right Motor
int leftline = 5; //Left line follower input.
int middleline = 4; //Middle line follower input.
int rightline = 3; //Right line follower input.
int bumper = 7; //Bumber swithch input
int ball = 9; // ball motor output.
//add pins for for ping pong ball device.

void setup() {
  pinMode(leftmotor, OUTPUT);
  pinMode(rightmotor, OUTPUT);
  pinMode(leftline, INPUT); //Sets pin 5 as input.
  pinMode(middleline, INPUT); //Sets pin 4 as input.
  pinMode(rightline, INPUT); //Sets pin 3 as input.
  pinMode(bumper, INPUT); //Sets pin 7 as input.
  pinMode(ball, OUTPUT); //set ball motor as an output.
}

void loop() {
  while (digitalRead(bumper) == HIGH) {
  delay(500);
    analogWrite(rightmotor, 45);
    analogWrite(leftmotor, 45);
    delay(1);
    analogWrite(ball, 90);
    analogWrite(rightmotor, 45);
    analogWrite(leftmotor, 45);
    delay(100);
    analogWrite(ball, 250);
    analogWrite(rightmotor, 45);
    analogWrite(leftmotor, 45);
    delay(100);
    analogWrite(ball, 45);
    analogWrite(rightmotor, 45);
    analogWrite(leftmotor, 45);
    delay(1);
    analogWrite(rightmotor, 90);
    analogWrite(leftmotor, 90);
    delay(2000);
    analogWrite(leftmotor, 250);
    analogWrite(rightmotor, 90);
    delay(2000);
  }
  while (digitalRead(bumper) == LOW) {
    if (digitalRead(leftline) == HIGH) {
    analogWrite(leftmotor, 45); // Stops left motor.
    analogWrite(rightmotor, 235); //Starts right motor
    delay(1);
  }
  else {
    //if no input.
  }
  if (digitalRead(rightline) == HIGH) {
    analogWrite(leftmotor, 235); // Runs left motor.
    analogWrite(rightmotor, 45); // Stops left motor.
    delay(1);
  }
  else {
    // if no input.
  }
  if (digitalRead(middleline) == HIGH) {
    analogWrite(leftmotor, 235); //Runs left motor
    analogWrite(rightmotor, 235); //Runs right motor.
    delay(1);
  }
  else {
    //Do nothing.
  }
  }
}

This is all very confusing to me and I have to have this done tomorrow sometime. Thanks everyone for helping I think it's getting closer and I know this is highly discouraged in C++ but is there anyway we can switch instructions with a goto?

Is this not the same as http://forum.arduino.cc/index.php?topic=389075.msg2689927#msg2689927

Is this not the same as http://forum.arduino.cc/index.php?topic=389075.msg2689927#msg2689927

It is similar but I wasn't doing loops so I decided a new thread would be best.

Ok I decided to try something along the lines of while var == 1 then do the turn around then at the end of that do var == 0 and change var to 1 by doing an if bumper == HIGH then var == 1. I will post the new code when I am done with this.

Ok well that didn't work it only looped the line follow part and wouldn't change the var. Any help with any way possible to get it to switch loops is greatly appreciated.

dsalveson: This is all very confusing to me and I have to have this done tomorrow sometime.

Not going to happen.

Have a look at Planning and Implementing a Program to see how to write your code in small single-purpose functions.

Then, like at the start of that Thread, write down a list of the different states that you line-follower can be in. When you have the list, think of the events that cause it to change from one state to the next.

Your code in loop() could look like this

void loop() {
   readDetetors();
   if (movingState = FORWARD) {
      moveForward();
   }
   else if (movingState = TOUCHED) {
      rotate();
   }
   // etc
}

Do NOT use delay() anywhere in your program. Use millis() to manage timing without blocking so that loop() can repeat hundreds or thousands of times per second. The use of millis() is illustrated in above Thread and in Several Things at a Time

...R

Ok, I will try this and some other things and get back to you. Thanks

Ok, it is back to following a line all I did was change my ifs to if digitalRead(anylinesensor) = HIGH && digitalRead (bumper) == LOW so then my if digtalRead (bumper) == HIGH now works. I will keep everyone updated