The code works great for the red LED(>150) but not for the lower yelllow
>10. Battling this the entire weekend without success. Please help. Thanks.
int ledRed =12;
int ledYellow = 10;
int ledGreen = 8;
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = 0;
void setup()
{
pinMode(ledRed,OUTPUT);
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}
void loop() {
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(inputPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
if (average > 150)
{ digitalWrite(ledRed, HIGH);
//delay(1000);
//digitalWrite(ledRed,LOW);
}
else if (average >10)
digitalWrite(ledRed,LOW);
digitalWrite(ledYellow,HIGH);
Serial.println(average, DEC);
}
The yellow LED should come on when the analogRead is above 10. I have changed this value up and down. It will come on but does not follow the threshold. This is code that uses amplified/rectified sound to turn on a noise monitor. I am getting a good response from the sensor.
Your missing a pinMode(ledYellow,OUTPUT);
in your setup function.
What you are seeing is a very small pull-up voltage when you are writing to the yellow LED as that pin is still in the input mode by default. Also add a pinMode(ledGreen,OUTPUT); to the setup function if you ever plan on using it in the loop function in the future.
Thanks for the help. Hopefully this will present a better picture than the last post. I am still doing something dumb. The red works but yellow is on continuously and green is never on.
const int threshold1 = 100;
const int threshold2 = 50;
int ledRed =11;
int ledYellow = 13;
int ledGreen = 8;
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = 0;
void setup()
{
pinMode(ledRed,OUTPUT);
pinMode(ledYellow,OUTPUT);
pinMode(ledGreen,OUTPUT);
pinMode(inputPin, INPUT);
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}
void loop() {
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(inputPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
if (average > 100)
{
digitalWrite(ledRed,HIGH);
digitalWrite(ledYellow,LOW);
digitalWrite(ledGreen,LOW);
delay(1000);
}
else if (average >80 <99)
{
digitalWrite(ledYellow,HIGH);
digitalWrite(ledRed,LOW);
digitalWrite(ledGreen,LOW);
delay(1000);
}
else if (average >0 <79)
{
digitalWrite(ledGreen,HIGH);
digitalWrite(ledRed,LOW);
digitalWrite(ledYellow,LOW);
delay(1000);
}
Serial.println(average, DEC);
}
First, it computes "average > 80", which evaluates to 0 (false) or 1 (true), depending on the value of 'average'. Then it computes "0 < 99" or "1 < 99", depending on the result of the first computation. Both 0 and 1 are less than 99, so the result is 0 (false), so the if always fails.
As you point out, that was not the original intent. I've always said that some programming languages will allow you to shoot yourself in the foot, but C hands you a loaded gun.
One last problem to solve: How to run the millis() while counting the number of times the noise rises to the red threshold. We would like to keep a running count of the times the racket surpasses the red LED parameter. This code will only give us one HIGH and then runs the on/off again sequence. When println val, the total hits will pause until the flashing ceases, but does not reset val to zero for the next count and continues to add all red hits, ignoring the "if val > than 5".
int val;
int ledRed =12;
int ledYellow = 13;
int ledGreen = 8;
unsigned long starTime;
int flashRed = 200000;
const int numReadings = 3;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = 0;
void setup()
{
val = 0;
pinMode(ledRed,OUTPUT);
pinMode(ledYellow,OUTPUT);
pinMode(ledGreen,OUTPUT);
pinMode(inputPin, INPUT);
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
starTime = millis(); //timer
}
void loop() {
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(inputPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
starTime = millis(); //timer
// if ((average >200) && (average < 300)) {
if (average > 200) {
digitalWrite(ledRed,HIGH);
digitalWrite(ledYellow,LOW);
digitalWrite(ledGreen,LOW);
delay(1000);
val= val +1;
if((val >=3) && (millis()> flashRed )) {
digitalWrite(ledRed,HIGH);
delay(1000);
digitalWrite(ledRed,LOW);
delay(1000);
digitalWrite(ledRed,HIGH);
delay(1000);
digitalWrite(ledRed,LOW);
delay(1000);
digitalWrite(ledRed,HIGH);
delay(1000);
digitalWrite(ledRed,LOW);
delay(1000);
digitalWrite(ledRed,HIGH);
delay(1000);
digitalWrite(ledRed,LOW);
delay(1000);
val = 0;
}
}
else if ((average > 100) && (average < 199)) { // code here will execute if average <= 100 and average > 80
digitalWrite(ledRed,LOW);
digitalWrite(ledYellow,HIGH);
digitalWrite(ledGreen,LOW);
delay(100);
}
else { // code here will execute if average <= 80
digitalWrite(ledRed,LOW);
digitalWrite(ledYellow,LOW);
digitalWrite(ledGreen,HIGH);
delay (100); // no point repeating this call 3 times
}
// Serial.println(average, DEC);
Serial.println(val);
//val = 0;
}