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);
}
}
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.
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?
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;
}