Hi all! New to the community - I'm a professional engineer with programming experience, but that was long ago, and am starting to do arduino projects with my kids. Any help with my project below is appreciated, thank you!
5 LEDs each with a matching sensor.
LED5 lights up until Sensor5 goes HIGH, then LED5 goes off and another random LED lights up until that matching sensor goes HIGH.
int ledPin[] = {2, 3, 4, 5, 6};
int sensor[] = {7, 8, 9, 10, 11};
int n;
int sensorstate;
void setup()
{
Serial.begin(9600);
randomSeed(analogRead(A0));
// Use a for loop to set all LED pins as outputs
for (int i = 0; i < 5; i++) {
pinMode(ledPin[i], OUTPUT); // ledPins[i] dynamically accesses each pin number
pinMode(sensor[i], INPUT); // ledPins[i] dynamically accesses each pin number
}
int n = 2;
}
void loop() {
sensorstate = digitalRead(sensor[n]);
if(sensorstate == HIGH)
{
digitalWrite(ledPin[n], LOW);
int n = random(0, 5);
}
else
{
digitalWrite(ledPin[n], HIGH);
}
}
The first thing I'd suggest you do is reformat your code so your indentation is consistent.
The second thing is to turn your compiler warning level up to ALL to catch your "this doesn't do what you think it does" mistakes. 
int ledPin[] = { 2, 3, 4, 5, 6 };
int sensor[] = { 7, 8, 9, 10, 11 };
int n;
int sensorstate;
void setup() {
Serial.begin(9600);
randomSeed(analogRead(A0));
// Use a for loop to set all LED pins as outputs
for( int i = 0; i < 5; i++ ) {
pinMode(ledPin[i], OUTPUT); // ledPins[i] dynamically accesses each pin number
pinMode(sensor[i], INPUT); // ledPins[i] dynamically accesses each pin number
}
int n = 2;
}
void loop() {
sensorstate = digitalRead(sensor[n]);
if( sensorstate == HIGH ) {
digitalWrite(ledPin[n], LOW);
int n = random(0, 5);
} else {
digitalWrite(ledPin[n], HIGH);
}
}
arduino-cli compile -b arduino:avr:uno --warnings all --output-dir ~/tmp --no-color (in directory: /home/me/Documents/sketchbook/Uno_R3/test)
/home/me/Documents/sketchbook/Uno_R3/test/test.ino: In function 'void setup()':
/home/me/Documents/sketchbook/Uno_R3/test/test.ino:16:8: warning: unused variable 'n' [-Wunused-variable]
int n = 2;
^
/home/me/Documents/sketchbook/Uno_R3/test/test.ino: In function 'void loop()':
/home/me/Documents/sketchbook/Uno_R3/test/test.ino:22:11: warning: unused variable 'n' [-Wunused-variable]
int n = random(0, 5);
^
Sketch uses 5418 bytes (16%) of program storage space. Maximum is 32256 bytes.
Global variables use 208 bytes (10%) of dynamic memory, leaving 1840 bytes for local variables. Maximum is 2048 bytes.
Compilation finished successfully.
Yay! It works 100%! I have another layer of complexity that I will post on tomorrow.
Thank!
This is what I ended up doing btw.
int ledselect;
int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;
int led5 = 6;
int s1 = 8;
int s2 = 9;
int s3 = 10;
int s4 = 11;
int s5 = 12;
void setup() {
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(s1, INPUT);
pinMode(s2, INPUT);
pinMode(s3, INPUT);
pinMode(s4, INPUT);
pinMode(s5, INPUT);
}
void loop() {
int ledselect = random(5);
Serial.print("ledselect = ");
Serial.println(ledselect);
switch (ledselect) {
case 0:
//digitalWrite(led1, HIGH);
//digitalWrite(ir1, HIGH);
//delay(20);
Serial.print("digitalRead(s1) = ");
Serial.println(digitalRead(s1));
while (digitalRead(s1) == LOW) {
Serial.println("s1 = LOW");
digitalWrite(led1, HIGH);
delay(20);
}
digitalWrite(led1, LOW);
break;
case 1:
Serial.print("digitalRead(s2) = ");
Serial.println(digitalRead(s2));
while (digitalRead(s2) == LOW) {
Serial.println("s2 = LOW");
digitalWrite(led2, HIGH);
delay(20);
}
digitalWrite(led2, LOW);
break;
case 2:
Serial.print("digitalRead(s3) = ");
Serial.println(digitalRead(s3));
while (digitalRead(s3) == LOW) {
Serial.println("s3 = LOW");
digitalWrite(led3, HIGH);
delay(20);
}
digitalWrite(led3, LOW);
break;
case 3:
Serial.print("digitalRead(s4) = ");
Serial.println(digitalRead(s4));
while (digitalRead(s4) == LOW) {
Serial.println("s4 = LOW");
digitalWrite(led4, HIGH);
delay(20);
}
digitalWrite(led4, LOW);
break;
case 4:
Serial.print("digitalRead(s5) = ");
Serial.println(digitalRead(s5));
while (digitalRead(s5) == LOW) {
Serial.println("s5 = LOW");
digitalWrite(led5, HIGH);
delay(20);
}
digitalWrite(led5, LOW);
break;
default:
delay(1000);
break;
}
delay(500);
}
hi @84jdranger, welcome to the fora.
If the language you once used has arrays and functions, learn how they look (syntax) in C++, your old friends will be back.
All your cases have essentially the same code…
Also, if you are cool with 0 as a case, you might have named your variables xxxx0 .. xxxx4, so the case has the same digit.
It matches the strong idea in C that 0 works out well as a perfectly usable number. C++ too. Speaking of which, no need to worry about C++ until you get your arms around C. Opinions vary. I still haven't really used much C++, I get stuff done.
Things that don't change can be marked as such, check out
const byte led0 = 2;
But those will all go away. With the array that might hold all those pin numbers.
All this more for you now, and for the kids at some point in their near future.
HTH
a7