My question is how to control LED properly to meet this condition. I have my code below.
I use 4 components on Arduino:
Switches, LEDs, Potentiometer(POT) (adjusting resistance), and LDR (detecting ambient brightness level) on Arduino.
In my code, there are 2 functions: for Reading Potentiometer & for Reading LDR.
I'd like to store values from POT and LDR into 2 separate arrays
When Swtich1 == 1(On) for POT
Switch2 == 1(On) for LDR
When both Switches 1 & 2==1(On) for POT & LDR
Condition 1. For both functions, each time a number is read, the corresponding LED should light up. For example, when reading the first value, LED 1 should light up, when reading the second value, the second LED should light up, and so on. when 10 values have been read, all LEDs should be on.
Condition2. After the current is displayed, all LEDs should turn off.
First, in order to meet Condition2, I put this code to turn off all LEDs after every current is displayed.
for (int i=0; i<10; i++){
digitalWrite(LED, LOW);
LED = LED +1;
}//ends for-loop to turn off LEDs
LED=41;
So far, it seems OK, BUT!
Therefore, I need to have only code for turning on LEDs in each function because I will turn them off in Void Loop.
But If I do so, for when both switches are on. after POT values are read, all LEDs will be on. so when LDR values are read, I cannot turn on matching LEDs since they are already on.
Therefore, I put code for turning off LEDs in each function. But seems conflicting the "all LEDs should be on".
Is there any method? instead of turning back the LEDs on Void Loop?
For when Both Switch1&8 are1(ON), it should display 10 values from POT&LDR like this.
else if(Switch1==1&&Switch8==1){
BOTH_Average2= POT_Reading()+LDR_Reading();
delay(1000);//Delay to read 1 second
Serial.print("\nThe Average POT & LDR Value is: ");
Serial.print(BOTH_Average2, 1);
Serial.print(" ohm");
This is how I recalled the functions and put them together for Both switches are on
const int LED1 = 41;
const int LED2 = 42;
const int LED3 = 43;
const int LED4 = 44;
const int LED5 = 45;
const int LED6 = 46;
const int LED7 = 47;
const int LED8 = 48;
const int LED9 = 49;
const int LED10 = 50;
const int SW_1 = 31;
const int SW_2 = 31;
const int SW_3 = 31;
const int SW_4 = 31;
const int SW_5 = 31;
const int SW_6 = 31;
const int SW_7 = 31;
const int SW_8 = 38;
const float Voltage = 120;
float POT_NUM[10], LDR_NUM[10];
int Switch1, Switch8, LED=41;
float LDR;
float POT_Total, LDR_Total;
float POT_Average, LDR_Average, BOTH_Average;
float POT_Current, LDR_Current, BOTH_Current;
float POT_Average2, LDR_Average2, BOTH_Average2;
float Result1;//for pot
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
for(int i=31; i<39; i++){//Define digit pins as inputs/outputs
pinMode(i, INPUT);//Setup Switches INPUT
}//ends for-loop digital Switches
for(int i=41; i<51; i++){//Define digit pins as inputs/outputs
pinMode(i, OUTPUT);//Setup LEDs OUTPUT
}//ends for-loop digital LED
NAME();
}//ends Void Setup
void loop() {
// put your main code here, to run repeatedly:
Switch1 = digitalRead(31);
Switch8 = digitalRead(38);
if(Switch1==1&&Switch8==0){
POT_Average2 = POT_Reading();
Serial.print("\nThe Average POT Values is: ");
Serial.print(POT_Average2, 1);
Serial.print(" ohm");
POT_Current = Voltage/POT_Average2*1000;
Serial.print("\nThe Circuit Current with POT is: ");
Serial.print(POT_Current, 1);
Serial.print(" mA");
for (int i=0; i<10; i++){
digitalWrite(LED, LOW);
LED = LED +1;
}//ends for-loop to turn off LEDs
LED=41;
if(POT_Current <126||POT_Current>300){
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
delay(3000);//Delay to read 3 seconds
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
}
}//ends if for Switch1
else if (Switch1==0&&Switch8==1){
LDR_Average2 = LDR_Reading();
Serial.print("\nThe Average LDR Value is: ");
Serial.print(LDR_Average2, 1);
Serial.print(" ohm");
LDR_Current = Voltage/LDR_Average2*1000;
Serial.print("\nThe Circuit Current with LDR is: ");
Serial.print(LDR_Current, 1);
Serial.print(" mA");
for (int i=0; i<10; i++){
digitalWrite(LED, LOW);
LED = LED +1;
}//ends for-loop to turn off LEDs
LED=41;
if(LDR_Current <126||LDR_Current>300){
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
delay(3000);//Delay to read 3 seconds
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
}
}//ends if for Switch8
else if(Switch1 ==1 && Switch8 == 1){
BOTH_Average2= POT_Reading()+LDR_Reading();
Serial.print("\nThe Average POT & LDR Value is: ");
Serial.print(BOTH_Average2, 1);
Serial.print(" ohm");
BOTH_Current = Voltage/BOTH_Average2*1000;
Serial.print("\nThe Circuit Current with POT & LDR is: ");
Serial.print(BOTH_Current, 1);
Serial.print(" mA");
for (int i=0; i<10; i++){
digitalWrite(LED, LOW);
LED = LED +1;
}//ends for-loop to turn off LEDs
LED=41;
if(BOTH_Current <126||BOTH_Current>300){
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
delay(3000);//Delay to read every 2 seconds
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
}
}//ends if for Both Switch1 & Switch8
}//ends Void Loop
float POT_Reading(){
POT_Total = 0;//To set the total value 0 for repeating computation
for (int i=0; i<10; i++){
delay(2000);//Delay to read every 2 seconds
POT_NUM[i]= analogRead(A7);//Read&Store POT value 6 elements
digitalWrite(LED, HIGH);
LED = LED +1;
POT_Total = POT_Total + POT_NUM[i];
}//ends for POT read
LED=41;//Set back to 41
for (int i=0; i<10; i++){
delay(1000);//Delay to read every 2 seconds
Serial.print("\n");
Serial.print(i+1);
Serial.print(" The POT Value is: ");
Serial.print(POT_NUM[i], 1);
Serial.print(" ohm");
digitalWrite(LED, LOW);
LED = LED +1;
}//ends for POT print
LED=41;//Set back to 41
POT_Average = POT_Total /10;
return POT_Average;
}//ends POT function
float LDR_Reading(){
LDR_Total = 0;//To set the total value 0 for repeating computation
for (int i=0; i<10; i++){
delay(2000);//Delay to read every 2 seconds
LDR_NUM[i]= analogRead(A6);//Read&Store POT value 6 elements
digitalWrite(LED, HIGH);
LED = LED +1;
LDR_Total = LDR_Total + LDR_NUM[i];
}//ends for LDR read
LED=41;//Set back to 41
for (int i=0; i<10; i++){
delay(500);//1 Second-delay between printing the numbers
Serial.print("\n");
Serial.print(i+1);
Serial.print(" The LDR Value is: ");
Serial.print(LDR_NUM[i], 1);
Serial.print(" ohm");
digitalWrite(LED, LOW);
LED = LED +1;
}//ends for LDR print
LED=41;//Set back to 41
LDR_Average = LDR_Total /10;
return LDR_Average;
}//ends LDR function
void NAME(){//Begins NAME Fuction
String First ="";
String Last = "";
Serial.print("\nPlease enter your first name: ");
while(Serial.available()==0){};//waits for user input
First=Serial.readString();//User input stored in First
Serial.print("\nPlease enter your last name: ");
while(Serial.available()==0){};//waits for user input
Last=Serial.readString();//User input stored in Last
Serial.print("\n\tGood morning " +First);
}`Preformatted text`