Hi there. I'm working on a project where I need to control a digital RGBW LED strip using Arduino. I have 5 different relay inputs to read.
To setup some program for each of the combinations of input the 5 relays are giving me, I'm gonna use switch().
My problem with the program is that I'm testing some control structure to return a value to a variable based on those 5 input pins.
Instead of using a different if statement for the 31 combinations, I wanna use a for loop to return the status of the inputs to an array.
I've wrote some code and can't get things to work. My code looks like this:
int kombination[] = {1, 2, 3, 4, 5};
int inputpin[] = {18, 19, 21, 22, 23};
int scenarie[] = {0, 0, 0, 0, 0};
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(18, INPUT);
pinMode(19, INPUT);
pinMode(21, INPUT);
pinMode(22, INPUT);
pinMode(23, INPUT);
}
void loop() {
for(int i=0; i<5; i++){
digitalRead(inputpin[i]);
if (inputpin[i] == HIGH) {
scenarie[i] = 1;
}
else {
scenarie[i] = 0;
}
Serial.println(scenarie[i]);
}
}
The serial monitor returns only zeros no matter wich input pin is connected to 3,3 v. And all inputs have a pulldown resistor attached.
Can someone better than me guide me in the right direction? I really wanna understand why this code isn't working and get better at this
Your digitalRead returns a value but you throw it away. Store it in a variable instead and use that in your subsequent if.
The serial monitor keeps returning only 0 even when 1 of the pins gets 3,3 v input. I wanna have the for loop return 1 when 1 of the inputs is high
How would I go about that, wildbill?
Just after the digitalRead line?
Even simpler
for (int i = 0; i < 5; i++)
{
scenarie[i] = digitalRead(inputpin[i]);
Serial.println(scenarie[i]);
}
However, have you got pulldown resistors on the input pins to keep them LOW when not taken HIGH ?
Which Arduino board are you using ?
This will never, ever be true.
Store the value returned by digitalRead in "scenarie[i]", and test that instread
Take a look at the button example sketch that comes with the IDE.
Thank you!
It's working now... Modified it after your answer and ended up with this code:
int kombination[] = {1, 2, 3, 4, 5};
int inputpin[] = {18, 19, 21, 22, 23};
int scenarie[] = {0, 0, 0, 0, 0};
void setup() {
Serial.begin(9600);
pinMode(18, INPUT);
pinMode(19, INPUT);
pinMode(21, INPUT);
pinMode(22, INPUT);
pinMode(23, INPUT);
}
void loop() {
for(int i=0; i<5; i++){
scenarie[i] = digitalRead(inputpin[i]);
}
Serial.println(scenarie[0]);
Serial.println(scenarie[1]);
Serial.println(scenarie[2]);
Serial.println(scenarie[3]);
Serial.println(scenarie[4]);
delay(1000);
}
This returns the correct values in the serial monitor now and I can use that to write my control structure. Thank you so much!
However, I can not understand why the if statement inside the for loop can't work. It's not that important though because it's working now. Just for learning, I'll try to figure it out!
The if will work, if it is written correctly.
HIGH has the value 1, so it will never equal a value in your list of pin numbers
Thank you! It's working now! So dead simple
Rookie mistake I guess! Returns the right values in the serial monitor like a charm..
Yes, I am using pulldown resistors connected to gnd
Don't do that - use the free built-in pullup resistors.
Oh darn
Of course it won't! I can see what it checks now.. Brainfart I guess - or rookie mistake
Thank you for your help!
Huh?
I'm using a DOIT devkit ESP32 board - should have clarified that I guess. Sorry about that!
Also
int inputpin[] = {18, 19, 21, 22, 23};
. . .
void setup() {
. . .
for (auto& pin: inputpin) {
pinMode(pin, INPUT);
}
}
system
Closed
September 6, 2021, 3:29pm
16
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.