Need help understanding a loop I am trying to create.

I am creating a circuit where 5 leds consecutively light up. I am fairly new to arduino projects, but I am pretty sure my trouble lies with the if statements I've created.

The code is pretty long, but rudimentary in the statements. I'm pretty sure the best way to understand is just to take a look at it.

const int buttonPin = 12;
const int bPin = 1;
const int boPin = 2;
const int booPin = 3;
const int bootPin = 4;
const int bootyPin = 5;
int buttonState = 0, bState = 0, boState = 0, booState = 0, bootState = 0, bootyState = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(buttonPin, INPUT);
  pinMode(bPin, OUTPUT);
  pinMode(boPin, OUTPUT);
  pinMode(booPin, OUTPUT);
  pinMode(bootPin, OUTPUT);
  pinMode(bootyPin, OUTPUT);
  Serial.begin(9600);
  digitalWrite(bPin, LOW);  
  digitalWrite(boPin, LOW);
  digitalWrite(booPin, LOW);
  digitalWrite(bootPin, LOW);
  digitalWrite(bootyPin, LOW);
}

void loop() {
  // put your main code here, to run repeatedly:
  int buttonState = digitalRead(buttonPin);
  int bState = digitalRead(bPin);
  int boState = digitalRead(boPin);
  int booState = digitalRead(booPin);
  int bootState = digitalRea d(bootPin);
  int bootyState = digitalRead(bootyPin);
  Serial.println(buttonState);
  if (buttonState == HIGH && bState == LOW && boState == LOW && booState == LOW && bootState == LOW && bootyState == LOW){
    digitalWrite(bPin, HIGH);
  }
    if (buttonState == HIGH && bState == HIGH && boState == LOW && booState == LOW && bootState == LOW && bootyState == LOW){
     digitalWrite(boPin, HIGH);
  }
    if (buttonState == HIGH && bState == HIGH && boState == HIGH && booState == LOW && bootState == LOW && bootyState == LOW){
    digitalWrite(booPin, HIGH);
}
    if(buttonState == HIGH && bState == HIGH && boState == HIGH && booState == HIGH && bootState == LOW && bootyState == LOW){
    digitalWrite(bootPin, HIGH);
}
    if(buttonState == HIGH && bState == HIGH && boState == HIGH && booState == HIGH && bootState == HIGH && bootyState == LOW){
    digitalWrite(bootyPin, HIGH);
}
    if(buttonState == HIGH && bState == HIGH && boState == HIGH && booState == HIGH && bootState == HIGH && bootyState == HIGH){
    digitalWrite(bPin, LOW);  
    digitalWrite(boPin, LOW);
    digitalWrite(booPin, LOW);
    digitalWrite(bootPin, LOW);
    digitalWrite(bootyPin, LOW);
}
}

Line 32:

  int bootState = digitalRea d(bootPin);

can you see what's wrong?

@cubcrane

You have made the post at 4.04 am.

At 4.37 am @Goet has directed you to an apparently simple error.

It is now 5.10 am; you should give a reply telling that you have corrected the error or you don’t understand what Goet had wanted to mean.

The Forum is here to help you moving forward.

@Goet

Thanks for the catch! I should’ve been more thorough with my code before I posted, but the issue persists where the first LED in my circuit turns on and then stays lit even with input from the button. I have checked to make sure the button has a signal, and all appears to be working there.

@GolamMostafa

I was busy. I think this forum is extremely helpful and I appreciate any assistance I get with coding, but I am not able to check all the time.

use variables for the output pin state and not reading it back from the pin.

maybe your code works but you can't see it because the loop runs less then a millisecond

@Juraj

Oh that makes a lot of sense! I have a few approaches in mind for my code now, thanks for pointing out that my loop is running super fast.

use variables for the output pin state and not reading it back from the pin.

Apart from reasons of speed, which is not an issue here, why not read the state of the output pins directly ?

const int bPin = 1;

void setup() {
  pinMode(bPin, OUTPUT);
  Serial.begin(9600);
  digitalWrite(bPin, LOW);  
}

On the Arduino UNO, Serial uses Pin 0 and Pin 1. You can't use Pin 1 for other stuff when you use Serial.

It looks like what you want to do is more like:

void loop() {
  if (digitalRead(buttonPin)) {
    if (!digitalRead(bPin))
      digitalWrite(bPin, HIGH);
    else if (!digitalRead(boPin))
      digitalWrite(boPin, HIGH);
    else if (!digitalRead(booPin))
      digitalWrite(booPin, HIGH);
    else if (!digitalRead(bootPin))
      digitalWrite(bootPin, HIGH);
    else if (!digitalRead(bootyPin))
      digitalWrite(bootyPin, HIGH);
    delay(250); // 1/4 second between lights
  }
  else {  // Button released
    digitalWrite(bPin, LOW);
    digitalWrite(boPin, LOW);
    digitalWrite(booPin, LOW);
    digitalWrite(bootPin, LOW);
    digitalWrite(bootyPin, LOW);
  }
}

UKHeliBob:
Apart from reasons of speed, which is not an issue here, why not read the state of the output pins directly ?

ok, sorry, my intuition tells me to not use digitalRead on an output pin. When I think about the reason, it's an api function which is implemented on different platforms. Is there a specification that requires from digitalRead to work on an output pin?

GolamMostafa:
@cubcrane

You have made the post at 4.04 am.

At 4.37 am @Goet has directed you to an apparently simple error.

It is now 5.10 am; you should give a reply telling that you have corrected the error or you don't understand what Goet had wanted to mean.

The Forum is here to help you moving forward.

This is an international forum and members are in randomly distributed time zones. It is always possible that someone is just leaving for lunch, work, or going to sleep. There is no need to make a request like this.

my intuition tells me to not use digitalRead on an output pin. When I think about the reason, it's an api function which is implemented on different platforms. Is there a specification that requires from digitalRead to work on an output pin?

You are right to be suspicious about using digitalRead() on a pin defined as an output but the way that the function is implemented takes care of that by reading the port directly. It works for boards with ATmega processors but not with the Due.

int digitalRead(uint8_t pin)
{
	uint8_t timer = digitalPinToTimer(pin);
	uint8_t bit = digitalPinToBitMask(pin);
	uint8_t port = digitalPinToPort(pin);

	if (port == NOT_A_PIN) return LOW;

	// If the pin that support PWM output, we need to turn it off
	// before getting a digital reading.
	if (timer != NOT_ON_TIMER) turnOffPWM(timer);

	if (*portInputRegister(port) & bit) return HIGH;
	return LOW;
}