I have an Arduino hooked up to two sensors (with analog outputs) and an LED.
What I want to do, is have the LED light up for a few seconds on the conditions that Sensor A and Sensor B both have yielded a value greater than X. The conditions do not have to be met simultaneously or in any particular order and once a sensor has a value greater than X, its condition is fulfilled even if it drops below X directly after.
Once both conditions are met and the LED lights up for a few seconds, there is a brief pause before the conditions are reset and the LED can be lit again.
Additionally, I want the Arduino to ALWAYS display the value of the sensors input on the serial monitor at any point in the program (is this possible just by stating this outside of the loop or setup?)
Here's what I have so far:
const int X = 20; // value that our sensors must beat
int sensor_A = analogRead(A1); // sensor A to analog pin 0
int sensor_B = analogRead(A0); // sensor B to analog pin 1
int victory_LED = 13;
void setup()
{
Serial.begin(9600); // initialise the serial monitor
}
void loop()
{
while((sensor_A < X ) && (sensor_B < X)); // run the loop until sensor A and B both have values greater than X
{
Serial.print("Sensor A: ");
Serial.println(sensor_A); // print sensor A value in monitor
Serial.print("Sensor B: ");
Serial.println(sensor_B); // print sensor B value in monitor
}
digitalWrite(victory_LED, HIGH); // turn on LED
delay(3000); // wait for 3 seconds
digitalWrite(victory_LED, LOW); // turn off LED
delay(3000); // pause before starting over again
}
I'm not sure how to write it so that the conditions don't have to happen simultaneously. I'm not sure if the use of the while loop is the best method either - once the conditions are met, does the program just stop altogether or does it continue past the while loop to the digitalWrite parts of the program?
I would use two boolean variables such as 'sensor_A_value_met' and 'sensor_B_value_met'. Program so that when the value of a sensor has reached a threshold, set the respective boolean variable to TRUE. If both boolean variables are TRUE, turn on led and mark the time (millis). Keep checking the time. When the difference between the current time and the time marked when the two boolean values went TRUE is equal to or greater than 'pause', turn the led off and set the two boolean values to FALSE.
const int X = 20; // value that our sensors must beat
int sensor_A = analogRead(A1); // sensor A to analog pin 0
int sensor_B = analogRead(A0); // sensor B to analog pin 1
int victory_LED = 13;
void setup()
{
Serial.begin(9600); // initialise the serial monitor
}
void loop()
{
if(sensor_A < X && sensor_B < X); // run the loop until sensor A and B both have values greater than X
{
Serial.print("Sensor A: ");
Serial.println(sensor_A); // print sensor A value in monitor
Serial.print("Sensor B: ");
Serial.println(sensor_B); // print sensor B value in monitor
digitalWrite(victory_LED, HIGH); // turn on LED
delay(3000); // wait for 3 seconds
digitalWrite(victory_LED, LOW); // turn off LED
delay(3000); // pause before starting over again
}
}
As scottyjr wrote, you need (boolean) variables to remember that the sensor was above the 'X'.
You could try without the use of millis(), but you need that anyway later on, when you want good timing.
You also need to re-organize your sketch. You need to call "analogRead()" everytime that you want to read the analog value.
The loop() function is an endless loop in itself. You can use that, keep reading the sensors, remember if they got above 'X' and check the (boolean) variables if they both got above 'X'. Do that over and over again with the loop().
My bad
I ignored the abscence of the analogread command
but you dont need to make such a simple program complicated
just use this
const int X = 20; // value that our sensors must beat
int sensor_A; // sensor A
int sensor_B; // sensor B
int victory_LED = 13;
void setup()
{
pinMode(A0,INPUT);
pinMode(A1,INPUT);
Serial.begin(9600); // initialise the serial monitor
}
void loop()
{
sensor_A = analogRead(A1);
sensor_B = analogRead(A0);
if(sensor_A < X && sensor_B < X); // run the loop until sensor A and B both have values greater than X
{
Serial.print("Sensor A: ");
Serial.println(sensor_A); // print sensor A value in monitor
Serial.print("Sensor B: ");
Serial.println(sensor_B); // print sensor B value in monitor
digitalWrite(victory_LED, HIGH); // turn on LED
delay(3000); // wait for 3 seconds
digitalWrite(victory_LED, LOW); // turn off LED
delay(3000); // pause before starting over again
}
}
Peter_n:
The loop() function is an endless loop in itself. You can use that, keep reading the sensors, remember if they got above 'X' and check the (boolean) variables if they both got above 'X'. Do that over and over again with the loop().
Can you try to make a new sketch with this ?
Hmmm, I was under the misconception that things cannot be "remembered", so to speak, once a new iteration of loop() begins. I'll keep trying.
I think it would be best not to refer to them as superfluous. If the pinMode() function were being called for a pin involved in an analogWrite() call, the use of pinMode() would be superfluous.
In this case, setting the mode of the digital pin at some location on the board, and then using the pin as an analog pin, isn't superfluous. It's wrong.
const int X = 20; // value that our sensors must beat
int victory_LED = 13;
boolean A = false;
boolean B = false;
void setup()
{
Serial.begin(9600); // initialise the serial monitor
}
void loop()
{
int sensor_A = analogRead(A1); // sensor A to analog pin 0
int sensor_B = analogRead(A0); // sensor B to analog pin 1
if (sensor_A > X)
{
A = true;
}
if (sensor_B > X)
{
B = true;
}
Serial.print("Sensor A: ");
Serial.println(sensor_A); // print sensor A value in monitor
Serial.print("Sensor B: ");
Serial.println(sensor_B); // print sensor B value in monitor
delay(100);
if ((A == true) && (B == true))
{
digitalWrite(victory_LED, HIGH);
delay(3000);
digitalWrite(victory_LED, LOW);
delay(3000);
A = false;
B = false;
}
}
I can't seem to get the LED to light up. What am I missing?
EDIT: Added booleans setting to false after LED is supposed to light up.
Code that actually compiles. What you posted is missing ; on some statements, so it will not compile. There is no definition of work that makes sense when the code won't even compile.
Code that actually compiles. What you posted is missing ; on some statements, so it will not compile. There is no definition of work that makes sense when the code won't even compile.
I already fixed that a minute before you posted.
EDIT: Hmm, it worked fine ONCE. But after that it wouldn't trigger the LED again.
Serial.print("Sensor A: ");
Serial.println(sensor_A); // print sensor A value in monitor
Serial.print("Sensor B: ");
Serial.println(sensor_B); // print sensor B value in monitor
Serial.println(sensor_A); // print sensor A value in monitor
Serial.print("Sensor B: ");
Serial.println(sensor_B); // print sensor B value in monitor
What output is this stuff giving you?
...R
The sensors are photodiodes. The output on the serial monitor is nothing but 0s until I pass an IR LED over each of them. That part is working fine. I managed to trigger the LED once I got over X on both sensors, but after that, I can't trigger the LED ever again.
I'm missing something that's preventing it from allowing the LED to trigger again because the sensors and the output still show readings.
EDIT: Nevermind, it is working fine now after I repositioned the reset-to-false statements before digitalWrite (why would this even matter?).