Using if...else in void setup()

My small robot employs 4 LED's for program status reporting. I have been using the following code to hold the robot in place. The Red LED signifies the robot is ready. Pressing a switch starts robot after one second delay.

//PROGRAM #1***************************

void setup() {
 // Other setup code here
 
  RedOn();                  //Red LED says robot is ready
  
	  while (digitalRead(1) == HIGH)  {   //Wait for start switch
 }

  LEDoff();                 //Turn LED
  delay(1000);              //Wait to get hand clear of robot
}
void loop() {
 // Robot starts running here
}

I want to use the LED's as a bar chart to report battery input voltage So I added this code. But the switch is no longer active. The robot just starts moving. Comparing the two program segments the if...else segments are only difference. What's wrong here?
<
//PROGRAM #2***************************

void setup() {
 // Other setup code here
 
BatV = analogRead(7);    // Read battery voltage; new battery=4.85 volts
          
  if (BatV > 940) {RedOn(); }           // Battery voltage > 4.6 volts
    else if (BatV > 900) {YellowOn(); } // Battery voltage > 4.4 volts
    else if (BatV > 850) {BlueOn(); }   // Battery voltage > 4.2 volts
    else WhiteOn();                     // Battery low, replace if robot misbehaves

   while (digitalRead(1) == HIGH)  {   //Wait for start switch
 }

  LEDoff();                 //Turn LED
  delay(1000);              //Wait to get hand clear of robot
}

void loop() {
 // Robot starts running here
}

I don't see anything wrong with the logic.

Do you have a pull-up resistor on the switch?

1 Like

Please post your WHOLE program.
The problem is almost always in the code you think is ok.

Bit hard to say with ALL the code.

Usually not a good idea to use pins 0 & 1... they are used by the hardware serial port.

D0, D1 are pulled-up in hardware - even if through the RX/TX LEDs.

Which pins control the robot, and do those conflict with any of the LED pins?

Robot uses a Nano.

From the Arduino reference for "Digital-Pins:" Arduino (Atmega) pins default to inputs, so they don't need to be explicitly declared as inputs with pinMode() when you're using them as inputs.

I've used this code for three years on numerous robots and the switch was always functional.

Thanks for your input

That doesn't answer the question asked

1 Like

lastchancename: The program is for a two path line maze solving robot. It consists of over 250 lines of code--to much to post and ask you to interpret.

MY Maze Solver

The code segment from //PROGRAM #1 is taken from a sketch that has worked for over three years.

The only change made to make //PROGRAM #2 is the code segment change above.

Semperidem: I do not have a pull-up resistor on the switch. It has worked without it for three years, which is consistent with the manual.

The real change is adding "if-else" statements into void setup(). These statements do work when included in void loop(). I don't want them in loop because I want the battery voltage test performed only once, at the start of the program.

Thanks for your feedback.

The problem is without all the code we are guessing. You haven't posted enough to even compile the code so that we can try and debug.

The variable definitions are missing, and we can't see what the ledOn() routines do. For example one of them could be writing serial output that is being written to pin 1... who knows?

250 lines is not a lot... those here will quickly work out which pieces are relevant.

Here is the code for a single run. The robot runs the maze then stops. I have additional software where it eliminates dead ends and makes an optimal run. Note the code for PROGRAM #2, above is now at the end of the Goal subroutine.

12-2022MazeB.ino (8.5 KB)

What does the manual say about floating pins? (I genuinely don't know - I don't have your manual)

What does the manual say about floating pins? (I genuinely don't know - I don't have your manual)

Go here:

I suspect the issue is related to the use of pin 1 for your button... as mentioned in post #4 this is used by the serial communication...

This statement uses that communication...

  Serial.print(BatV);

The only other place I could see print statements were inside routines that are never called.

I'll move the switch from D1 to D2 to test your idea, but I have low confidence it will work. here is why:

I use only the serial monitor for my testing. For example, I measured the analog value out for each sensor as I moved the black line, 50mm from robot center to each side. To do this, I would build a small loop to ReadSensors() followed by PrintS(). When this sketch runs, it first stops at line 38, waiting for the switch, D1, to be pressed. After that the serial monitor display continuously prints the values of the six sensors across the screen. So the switch at D1 does not interfere with serial data out.

Am I missing something here? Thanks for your comments.

Without a pulldown/pullup resistor you will also get random values from the digital pin, when the button is not pressed. It may work it may not... you are basically leaving it to chance.

The easiest way is just to define the pin as INPUT_PULLUP, and then look for == LOW when checking the switch.

Ok, I'll update the software. Thanks.

Everybody else is going down the wrong rabbit hole....

Look at the if line again.

This is a complete statement and the else and while following it will never be executed. But I am surprised there is no compile errors from all the excess } characters.

Huh?
I think you have been misled by the coding/indention style.
Here is the identical code using allman style, which IMO, is the easiest to follow:

	if(BatV > 940)
	{
		RedOn ();
	}			// Battery voltage > 4.6 volts
	else if(BatV > 880)
	{
		YellowOn ();
	}			// Battery voltage > 4.4 volts
	else if(BatV > 850)
	{
		BlueOn ();
	}			// Battery voltage > 4.2 volts
	else
		WhiteOn ();	// Battery low, replace if robot misbehaves

--- bill