0

So, I'm trying to read the voltage on analog pin A0 and make a "regulation", where if the voltage will be lower than 2.5V (analog value 512), then it should be output pin 9 used as PWM with duty cycle of 50% ( analogWrite(ledPin, 128); ) unless the voltage rises again higher than 2.5V.

Where/how am I supposed to use mentioned line ( analogWrite(ledPin, 128); in IF sentence, so it will be used continuous untill it is reached the limit?

PS: I just want to refresh LCD with frequency of 1 Hz while PWM works independently of delay(1000). How is that even possible?

How am I able to do that?

#include <LiquidCrystal.h>

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

const int numRows = 2;
const int numCols = 16;

void setup() {
  lcd.begin(numCols, numRows);
}

void loop() {

    int sensorValue = analogRead(A0);
    float voltage = sensorValue * (5.0 / 1023.0);
        lcd.setCursor(0, 0);
        lcd.write(voltage);

        delay(1000);
}
3
  • you make no progress. you ask always a variation of the same thing. study the BlinkWithoutDelay example
    – Juraj
    Commented Sep 20, 2018 at 13:36
  • You didn't understood the question. I'm not asking for finished code, I'm asking for an explanation on how-to.
    – Jakey
    Commented Sep 20, 2018 at 15:16
  • read this post .... it is one of yours .... arduino.stackexchange.com/questions/56061/…
    – jsotola
    Commented Sep 21, 2018 at 0:03

3 Answers 3

2

The PWM started by analogWrite will not be influenced be the delay, because the analogWrite function only sets the registers and starts the PWM. The PWM generator is independent from the CPU on AVR microcontrollers. On esp8266 for example the PWM is generated with use of interrupt and it is too not influenced by the delay function. To stop the PWM you must call analogWrite with 0 or change the pin mode.

0

You don't need an if-statement here. You can simply contineously take the measurements and act accordingly.

About the delay(1000):

It is considered a bad coding practice to use delay(), especially for longer periods, because it's just busy waiting. In real life you wouldn't just sit fiddling your thumbs, until a task is done. You would do something else in the intermediate time. Here you can reach this by using non-blocking code like in the BlinkWithoutDelay example of the Arduino IDE.

About PWM:

PWM is not handled by the software, but by the hardware. The Atmega on the Arduino has dedicated PWM hardware included in the package, which will handle the control of the PWM pin, so that the CPU can do other things.

2
  • Okay, but is there then a line, which WILL refresh the state shown on LCD? I can't figure out this part of the job.
    – Jakey
    Commented Sep 20, 2018 at 13:23
  • the OP has posted a question here ...arduino.stackexchange.com/questions/56061/… .... the use of delay() was addressed ..... it appears that the OP is unable to learn from experience
    – jsotola
    Commented Sep 21, 2018 at 0:02
0

I think you need something like this, with a flag so you are not contantly restarting the PWM on pin 8 and making the output erratic:

void loop(){
if (analogRead(A0)<512  && halfLevel9 == 0){
analogWrite (9, 128);
halfLevel9 = 1; // flag to let you know it's been set
}
else {
analogWrite (9, withWhat?);
halfLevel9 = 0;
}
// use blink without delay to determine if 1 second has passed
{
// write whatever is going to the LCD
}
// do other stuff while the 1 second passes
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.