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.
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.
// https://forum.arduino.cc/t/randomSelectly-control-leds-with-sensors/1428928/
// random LED lights until matching sensor triggered
int randomSelect; // variable for random number
int led[] = {2, 3, 4, 5, 6}; // LED pins
int sensor[] = {8, 9, 10, 11, 12}; // sensor pins
void setup() {
randomSeed(analogRead(A0)); // seed pseudorandom generator
for (int i = 0; i < 6; i++) { // count to six...
pinMode(led[i], OUTPUT); // configure LEDs as output
pinMode(sensor[i], INPUT); // configure sensors as input with PULL-DOWN
}
}
void loop() {
randomSelect = random(sizeof(led) / sizeof(led[0])); // size of led[] array
lightandsense(randomSelect); // call LED/sensor function with random value
}
void lightandsense (int value) { // receive random value
while (digitalRead(sensor[value]) == LOW) { // assuming sensor PULLDOWN, NOT SENSED = LOW
digitalWrite(led[value], HIGH); // LED ON
}
digitalWrite(led[value], LOW); // LED OFF
}