tom321
March 11, 2022, 3:26am
1
Hi
I don't have a tone on the first if, on the second if I have it ?
#include <LiquidCrystal.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//LiquidCrystal_I2C lcd(0x3F, 16, 2);
LiquidCrystal_I2C lcd(0x27 , 16, 2);
float fvout = 0.0;
float fvin = 0.0;
float fVo;
int LED = (PB14, PC13);
int fvalue = 0;
int buzzer = PB5;
int (clear);
const int ResetPin = PB12;
void setup()
{
pinMode(ResetPin, INPUT_PULLUP);
pinMode(PB5, OUTPUT);
pinMode(PC13, OUTPUT); // LED1
pinMode(PB14, OUTPUT); // LED2
lcd.begin();
lcd.backlight();
}
void loop()
{
float delta = fvin - fVo;
if (digitalRead(ResetPin) == LOW)
{
fVo = fvin;
}
else
{
fvalue = analogRead(PA7);
fvout = (fvalue * 3.3) / 4190.0; // 4200
fvin = fvout;
}
lcd.setCursor(0, 0);
lcd.print(fvin);
lcd.setCursor(5, 0);
lcd.print("o");
lcd.print(fVo, 2);
lcd.setCursor(11, 0);
lcd.print (delta);
if (- 0.2 < delta)
{
noTone(buzzer); //Ok
digitalWrite(PC13, 1); //Ok
}
else
{
tone(buzzer, 2000 ); // not Ok
digitalWrite(PC13, 0); //Ok
}
if ( 0.2 > delta)
{
noTone(buzzer); // Ok
digitalWrite(PB14, 1); //Ok
}
else
{
tone(buzzer, 2000 ); // Ok
digitalWrite(PB14, 0); //Ok
}
}
Add the ability to Serial.print() the values being used in the "if" statements you are referring to. Then you can read the actual numbers being compared. This is the FIRST step in debugging your program.
1 Like
tom321
March 11, 2022, 3:42am
3
The LEDs are working properly.
Then what is the problem?
tom321
March 11, 2022, 3:49am
5
I marked the line with "No ok "= no sound on PB5 from this line
Assuming that 1 turns on your LEDs, you are saying that the LED on PC13 is off and the LED on PB14 is off and there is no tone?
Put your finger on the logic and okay dumb computer. I don’t think the code is doing what you want, but is doing what you observe…
a7
tom321
March 11, 2022, 4:50am
7
alto777:
1 turns on your LEDs
0 turns LEDs on, they are connected to +
OK, so both LEDs are illuminated, and you get no tone.
Is that what you observe? Is that not what you want?
a7
Perhaps you meant
// TURN TONE OFF...
noTone();
if (- 0.2 < delta)
{
// noTone(buzzer);
digitalWrite(PC13, 1);
}
else
{
tone(buzzer, 2000 ); // ... UNLESS YOU WANT THE TONE
digitalWrite(PC13, 0);
}
if ( 0.2 > delta)
{
// noTone(buzzer);
digitalWrite(PB14, 1);
}
else
{
tone(buzzer, 2000 ); // ... UNLESS YOU WANT THE TONE
digitalWrite(PB14, 0);
}
HTH
a7
tom321
March 11, 2022, 1:22pm
12
"maybe software isn't for you" = you are not my boss to tell me that
tom321:
THANKS it is working
If my fix was the solution to your problem, mark it so. TIA.
It is a good case for study. Besides being a bit verbose, your original take had a fatal flaw.
We see sometimes
if (x == y) {
// do the x equals y thing
}
else if (x != y) {
// do the x does not equal y thing
}
and point out, to whomever, that the second bit of logic is unnecessary, as we already have tested x against y… if it wasn't true, it must be false.
You made the opposite mistake. Just because you want to turn ON a buzzer doesn't necessarily mean you want to turn it OFF otherwise.
Also, I see you do
if (0.2 > delta)...
which of course is correct and makes sense, but it is enough backwards to normal that it alone made reading your code take 28 percent longer than if you'd stuck to convention. Just sayin.'
I know the trick of using
if (0 == n)…
which can help catch the error of using = where == was meant.
This is not very popular and sticks in the brain of a long time code reader, slowing down the process.
Since compilers can warn about such errors, writing it backwards as any kind of protection is a step in the wrong direction for readability.
Rant off.
a7
Did I ever say I was?
What on Earth would make you make such a bizarre statement?
You'll notice that the compiler didn't point out your boo-boo, because the compiler knows it isn't an error as far as it is concerned.
Edit: I see you have previous form about misunderstanding employment relationships.
tom321
March 11, 2022, 2:08pm
15
Why are you picking on me? Just leave me alone, and disappear from my posts.
tom321:
Just leave me alone
Oh..if only I had a pithy come-back
tom321
March 11, 2022, 2:23pm
17
Without noTone the buzzer is not turning off.
Hi,
FYI
How your code works first code:
That make sense. If you don't call noTone , the buzzer will not turn off.
But you said thanks it works, what works? Post the code.
I put noTone at the top of my adjustment to your code with a huge comment, did you leave that off?
If you still have a problem, post the code.
If you fixed the problem, post the code.
a7
tom321
March 11, 2022, 2:39pm
20
This one is working OK, I have sound when voltage is dropping more than delta or is increasing more than delta.
#include <LiquidCrystal.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//LiquidCrystal_I2C lcd(0x3F, 16, 2);
LiquidCrystal_I2C lcd(0x27 , 16, 2);
float fvout2 = 0.0;
float fvin2 = 0.0;
float fVo2;
int LED = (PB14, PC13);
int fvalue2 = 0;
int buzzer = PB5;
int (clear);
const int ResetPin = PB12;
void setup()
{
pinMode(ResetPin, INPUT_PULLUP);
pinMode(PB5, OUTPUT);
pinMode(PC13, OUTPUT); // LED1
pinMode(PB14, OUTPUT); // LED2
lcd.begin();
lcd.backlight();
}
void loop()
{
float delta2 = fvin2 - fVo2;
if (digitalRead(ResetPin) == LOW)
{
fVo2 = fvin2;
}
else
{
fvalue2 = analogRead(PA7);
fvout2 = (fvalue2 * 3.3) / 4190.0; // 4200
fvin2 = fvout2;
}
lcd.setCursor(0, 0);
lcd.print(fvin2);
lcd.setCursor(5, 0);
lcd.print("o");
lcd.print(fVo2, 2);
lcd.setCursor(11, 0);
lcd.print (delta);
// TURN TONE OFF...
noTone(buzzer);
if (- 0.2 < delta2)
{
// noTone(buzzer);
digitalWrite(PC13, 1); // green
}
else
{
tone(buzzer, 2000 );
digitalWrite(PC13, 0);
}
if ( 0.2 > delta2)
{
// noTone(buzzer);
digitalWrite(PB14, 1); // blue
}
else
{
tone(buzzer, 800 );
digitalWrite(PB14, 0);
}
}