Blink Without Delay

Hi all

I'v have a small problem. I want to read a value from an analog pin but with a delay of 500ms how do I manege that without delay ?

int sensorValue = analogRead(vPin); int sensorValue1 = analogRead(aPin);

Thanks

I think you answered the question in the title.

But how do I blink a variable ? ( it may seem like a stupid question but I don't get it) :D

AWOL: I think you answered the question in the title.

Every post i see AWOL posting is sarcastic and unhelpful.

Perhaps look at this?

http://popdevelop.com/2010/04/mastering-timer-interrupts-on-the-arduino/

But how do I blink a variable ?

The "blink" is an arbitrary action. It doesn't matter if it is a read or a write.

Every post i see AWOL posting is sarcastic and unhelpful.

Every signature I see from Beakie contains an offensive word.

If he doesn't get simple timers, he's not going to get interrupts and all that they entail,

That could be helpful but im not sure if it would be any good here please correct me if i’m wrong.

#include <LiquidCrystal.h> 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int vPin = A0;
const int aPin = A2;
int out = 13;
int in = A1;
int val = 0;

void setup () {
  

  lcd.begin(16,2); 
  pinMode(13,OUTPUT); 
  pinMode(out, OUTPUT);
  pinMode(in, INPUT);
 
}

void loop() {
 
  int sensorValue = analogRead(vPin);
  int sensorValue1 = analogRead(aPin);
   
  
  lcd.setCursor(0,1);
  lcd.print(voltage(sensorValue)); 
  lcd.print("V" );  


  lcd.setCursor(8,1);
  lcd.print(current(sensorValue1)); 
  lcd.print("A" );  
  

val = analogRead(in);

 lcd.setCursor(5,0);
 lcd.print(val);
 lcd.setCursor(9,0);
 lcd.print("Hz");
   digitalWrite (out, HIGH);
   delayMicroseconds(val);
   digitalWrite (out, LOW);
   delayMicroseconds(val);
delay(500);    
}

double voltage(int analogIn){

  double voltage = (analogIn/1023.)*34.7;

  return voltage;
}

double current(int analogIn1){

  double current = (analogIn1/1023.)*34.7;

  return current;

}

This is my basic code for a V/A meter and frequency output device.

You don't say why "delay" is a bad idea here - what else are you doing at the same time?

Every post i see AWOL posting is sarcastic and unhelpful.

I have three suggestions...

  1. If you have a problem with AWOL's posting style, feel free to discuss your objections with him, [u]IN PRIVATE[/u]. This forum has "Personal Messages". Use them.

  2. If you have a problem with AWOL's posting style and you believe his posts are a detriment to the forum, bring the matter to the attention of a moderator, [u]IN PRIVATE[/u]. This forum has a "Report to moderator". Use it.

  3. If you have a problem with AWOL's posting style, feel free to ignore his posts. The Earth will continue spinning whether or not you read his posts.

[quote author=Coding Badly link=topic=56179.msg402725#msg402725 date=1300816175]

Every post i see AWOL posting is sarcastic and unhelpful.

I have three suggestions...

  1. If you have a problem with AWOL's posting style, feel free to discuss your objections with him, [u]IN PRIVATE[/u]. This forum has "Personal Messages". Use them.

  2. If you have a problem with AWOL's posting style and you believe his posts are a detriment to the forum, bring the matter to the attention of a moderator, [u]IN PRIVATE[/u]. This forum has a "Report to moderator". Use it.

  3. If you have a problem with AWOL's posting style, feel free to ignore his posts. The Earth will continue spinning whether or not you read his posts.

[/quote]

You should take your own advice

Becouse this is the basic code and I will ad some other things like setting the frequency output and curentMax cuttoff... I still dont get how I can make it to read the input every 500ms.

The bit in Blink Without Delay inside the conditional, where it toggles the LED state -that's where you read your values. Set the delay to 500 milliseconds.

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

This is the loop from Blink Without Delay, to read your analog pin instead of blinking a LED change

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);

To

 // Read Analog input
analogRead(AnalogInputPin); // I didn't read your sketch so I don't know what pin it was, sorry

Also you'd have to change the 1 second to .5 seconds, but that's easy enough and I assume you figured that out

if(currentMillis - previousMillis >= interval) {

Pfft, I just quoted directly from the sample :confused: it’s only a 1 millisecond difference, for something as noncritical as delaying the meter update frequency I think it’ll be ok

for something as noncritical as delaying the meter update frequency I think it'll be ok

I agree.

But what about the future person who finds your post and does need the delay to be accurate?

Yes ok but I will put buttons in the code so for every state change i will have to wait half a second right?

Thanks Valalvax that really worked.

No, you can read the buttons as often as you like - every time through "loop" if you like.

but only if I dont't have any delays ?

You've got rid of the delays haven't you?

Why don't you tell us what you're trying to do?

Yes

Sorry I messed up some of the replies.